@@ -70,11 +70,29 @@ abstract class AbstractCommand extends Command {
7070 protected $ profile = NULL ;
7171
7272 /**
73- * Stores the destination directory.
73+ * The root directory.
7474 *
7575 * @var string
7676 */
77- protected $ destination = NULL ;
77+ private $ root = NULL ;
78+
79+ /**
80+ * The web root directory.
81+ *
82+ * This is the web directory within the root.
83+ *
84+ * @var string
85+ */
86+ private $ webRoot = NULL ;
87+
88+ /**
89+ * The site root directory.
90+ *
91+ * This is where we put settings.php
92+ *
93+ * @var string
94+ */
95+ private $ siteRoot = NULL ;
7896
7997 /**
8098 * Stores the site url.
@@ -180,13 +198,58 @@ protected function validateSiteParams(InputInterface $input, OutputInterface $ou
180198 // Validate profile.
181199 $ this ->validateProfile ($ input );
182200
183- // Validate destination.
184- $ this ->validateDestination ($ input );
201+ // Validate root.
202+ $ this ->validateRoot ($ input );
203+
204+ // Validate web root.
205+ $ this ->validateWebRoot ();
206+
207+ // Validate settings.php directory.
208+ $ this ->validateSiteRoot ();
185209
186210 // Validate url.
187211 $ this ->validateUrl ($ input );
188212 }
189213
214+ /**
215+ * Getter for the root directory property.
216+ */
217+ protected function getRoot () {
218+ if (is_null ($ this ->root )) {
219+ throw new CommandException ('Root directory is not available. ' );
220+ }
221+ return $ this ->root ;
222+ }
223+
224+ /**
225+ * Getter for the web root directory property.
226+ */
227+ protected function getWebRoot () {
228+ if (is_null ($ this ->webRoot )) {
229+ throw new CommandException ('Web root directory is not available. ' );
230+ }
231+ return $ this ->webRoot ;
232+ }
233+
234+ /**
235+ * Getter for the site root directory property.
236+ */
237+ protected function getSiteRoot () {
238+ if (is_null ($ this ->siteRoot )) {
239+ throw new CommandException ('Site root directory is not available. ' );
240+ }
241+ return $ this ->siteRoot ;
242+ }
243+
244+ /**
245+ * Check if the current build has a site root directory.
246+ *
247+ * @return bool
248+ */
249+ protected function hasSiteRoot () {
250+ return !is_null ($ this ->siteRoot );
251+ }
252+
190253 /**
191254 * Helper to check that the config file exits and load the configuration.
192255 *
@@ -253,54 +316,51 @@ protected function validateProfile(InputInterface $input) {
253316 }
254317
255318 /**
256- * Helper to validate destination parameter.
319+ * Validate and set the web root directory.
320+ */
321+ protected function validateWebRoot () {
322+ $ web_directory = empty ($ this ->config ['web_directory ' ]) ? 'web ' : $ this ->config ['web_directory ' ];
323+ $ this ->webRoot = $ this ->getRoot () . trim ($ web_directory , '/ ' ) . '/ ' ;
324+ }
325+
326+ /**
327+ * Validate and set the root directory.
257328 *
258329 * @param InputInterface $input
259- *
260- * @throws CommandException
261- *
262- * @return string Destination
330+ * @return string
263331 */
264- protected function validateDestination (InputInterface $ input ) {
332+ protected function validateRoot (InputInterface $ input ) {
265333 if ($ input ->hasOption ('destination-directory ' ) &&
266334 !is_null ($ input ->getOption ('destination-directory ' ))
267335 ) {
268336 // Use config from parameter.
269- $ this ->destination = $ input ->getOption ('destination-directory ' );
337+ $ this ->root = $ input ->getOption ('destination-directory ' );
270338 }
271339 elseif (isset ($ this ->config ['root ' ])) {
272340 // Use config from sites.yml.
273- $ this ->destination = $ this ->config ['root ' ];
341+ $ this ->root = $ this ->config ['root ' ];
274342 }
275343 else {
276- $ this ->destination = '/tmp/ ' . $ this ->siteName ;
344+ $ this ->root = '/tmp/ ' . $ this ->siteName ;
277345 }
278346
279347 // Allow destination to be overriden by environment variable. i.e.
280348 // export site_destination_directory="/directory/"
281349 if (!getenv ('site_destination_directory ' )) {
282- putenv ("site_destination_directory= $ this ->destination " );
350+ putenv ("site_destination_directory= $ this ->root " );
283351 }
284352 else {
285- $ this ->destination = getenv ('site_destination_directory ' );
286- }
287-
288- // Make sure we have a slash at the end.
289- if (substr ($ this ->destination , -1 ) != '/ ' ) {
290- $ this ->destination .= '/ ' ;
353+ $ this ->root = getenv ('site_destination_directory ' );
291354 }
292355
293- return $ this ->destination ;
356+ $ this -> root = rtrim ( $ this ->root , ' / ' ) . ' / ' ;
294357 }
295358
296359 /**
297- * Helper to validate destination parameter .
360+ * Helper to validate URL .
298361 *
299362 * @param InputInterface $input
300- *
301- * @throws CommandException
302- *
303- * @return string Destination
363+ * @return string
304364 */
305365 protected function validateUrl (InputInterface $ input ) {
306366 $ scheme = isset ($ this ->config ['scheme ' ]) && !empty ($ this ->config ['scheme ' ]) ? $ this ->config ['scheme ' ] : 'http ' ;
@@ -318,16 +378,25 @@ protected function validateUrl(InputInterface $input) {
318378 }
319379
320380 /**
321- * Helper to return the path to settings.php
381+ * Helper to set the site root.
382+ *
383+ * This is where we place settings.php
384+ *
322385 * It will try to match a folder with same name as site name
323386 * If not found, it will try to match a folder called "default".
324387 *
325388 * @return string Path
326389 */
327- public function settingsPhpDirectory () {
328- $ webSitesPath = $ this ->destination . 'web/ sites/ ' ;
390+ public function validateSiteRoot () {
391+ $ webSitesPath = $ this ->getWebRoot () . 'sites/ ' ;
329392 $ settingsPath = $ webSitesPath . 'default ' ;
330393
394+ // It's possible that a command is run before the site is available. e.g. checkout
395+ // We will skip setting in this situation, but throw an Exception in the site root getter to prevent any unpredictable behaviour.
396+ if (!file_exists ($ settingsPath )) {
397+ return ;
398+ }
399+
331400 $ command = sprintf (
332401 'cd %s && find . -name settings.php ' ,
333402 $ this ->shellPath ($ webSitesPath )
@@ -360,7 +429,26 @@ public function settingsPhpDirectory() {
360429 $ settingsPath .= '/ ' ;
361430 }
362431
363- return $ settingsPath ;
432+ // Fix folder permissions.
433+ $ this ->fixSiteFolderPermissions ();
434+
435+ $ this ->siteRoot = $ settingsPath ;
436+ }
437+
438+ /**
439+ * Fixes the site folder permissions which is often changed by Drupal core.
440+ */
441+ protected function fixSiteFolderPermissions () {
442+ if ($ this ->hasSiteRoot ()) {
443+ $ commands [] = sprintf ('chmod 777 %s ' , $ this ->getSiteRoot ());
444+ $ commands [] = sprintf ('chmod 777 %ssettings.php ' , $ this ->getSiteRoot ());
445+ $ command = implode (' && ' , $ commands );
446+ $ this ->io ->commentBlock ($ command );
447+ $ shellProcess = $ this ->getShellProcess ();
448+ if (!$ shellProcess ->exec ($ command , TRUE )) {
449+ throw new CommandException ($ shellProcess ->getOutput ());
450+ }
451+ }
364452 }
365453
366454 /**
0 commit comments