1818use Drupal \Console \Core \Style \DrupalStyle ;
1919use Drupal \Console \Core \Command \Shared \CommandTrait ;
2020use DennisDigital \Drupal \Console \Command \Exception \CommandException ;
21+ use DennisDigital \Drupal \Console \Utils \ShellProcess ;
2122
2223/**
2324 * Class AbstractCommand
@@ -69,11 +70,29 @@ abstract class AbstractCommand extends Command {
6970 protected $ profile = NULL ;
7071
7172 /**
72- * Stores the destination directory.
73+ * The root directory.
7374 *
7475 * @var string
7576 */
76- 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 ;
7796
7897 /**
7998 * Stores the site url.
@@ -179,13 +198,58 @@ protected function validateSiteParams(InputInterface $input, OutputInterface $ou
179198 // Validate profile.
180199 $ this ->validateProfile ($ input );
181200
182- // Validate destination.
183- $ 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 ();
184209
185210 // Validate url.
186211 $ this ->validateUrl ($ input );
187212 }
188213
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+
189253 /**
190254 * Helper to check that the config file exits and load the configuration.
191255 *
@@ -252,54 +316,51 @@ protected function validateProfile(InputInterface $input) {
252316 }
253317
254318 /**
255- * 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.
256328 *
257329 * @param InputInterface $input
258- *
259- * @throws CommandException
260- *
261- * @return string Destination
330+ * @return string
262331 */
263- protected function validateDestination (InputInterface $ input ) {
332+ protected function validateRoot (InputInterface $ input ) {
264333 if ($ input ->hasOption ('destination-directory ' ) &&
265334 !is_null ($ input ->getOption ('destination-directory ' ))
266335 ) {
267336 // Use config from parameter.
268- $ this ->destination = $ input ->getOption ('destination-directory ' );
337+ $ this ->root = $ input ->getOption ('destination-directory ' );
269338 }
270339 elseif (isset ($ this ->config ['root ' ])) {
271340 // Use config from sites.yml.
272- $ this ->destination = $ this ->config ['root ' ];
341+ $ this ->root = $ this ->config ['root ' ];
273342 }
274343 else {
275- $ this ->destination = '/tmp/ ' . $ this ->siteName ;
344+ $ this ->root = '/tmp/ ' . $ this ->siteName ;
276345 }
277346
278347 // Allow destination to be overriden by environment variable. i.e.
279348 // export site_destination_directory="/directory/"
280349 if (!getenv ('site_destination_directory ' )) {
281- putenv ("site_destination_directory= $ this ->destination " );
350+ putenv ("site_destination_directory= $ this ->root " );
282351 }
283352 else {
284- $ this ->destination = getenv ('site_destination_directory ' );
285- }
286-
287- // Make sure we have a slash at the end.
288- if (substr ($ this ->destination , -1 ) != '/ ' ) {
289- $ this ->destination .= '/ ' ;
353+ $ this ->root = getenv ('site_destination_directory ' );
290354 }
291355
292- return $ this ->destination ;
356+ $ this -> root = rtrim ( $ this ->root , ' / ' ) . ' / ' ;
293357 }
294358
295359 /**
296- * Helper to validate destination parameter .
360+ * Helper to validate URL .
297361 *
298362 * @param InputInterface $input
299- *
300- * @throws CommandException
301- *
302- * @return string Destination
363+ * @return string
303364 */
304365 protected function validateUrl (InputInterface $ input ) {
305366 $ scheme = isset ($ this ->config ['scheme ' ]) && !empty ($ this ->config ['scheme ' ]) ? $ this ->config ['scheme ' ] : 'http ' ;
@@ -317,16 +378,25 @@ protected function validateUrl(InputInterface $input) {
317378 }
318379
319380 /**
320- * 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+ *
321385 * It will try to match a folder with same name as site name
322386 * If not found, it will try to match a folder called "default".
323387 *
324388 * @return string Path
325389 */
326- public function settingsPhpDirectory () {
327- $ webSitesPath = $ this ->destination . 'web/ sites/ ' ;
390+ public function validateSiteRoot () {
391+ $ webSitesPath = $ this ->getWebRoot () . 'sites/ ' ;
328392 $ settingsPath = $ webSitesPath . 'default ' ;
329393
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+
330400 $ command = sprintf (
331401 'cd %s && find . -name settings.php ' ,
332402 $ this ->shellPath ($ webSitesPath )
@@ -359,16 +429,36 @@ public function settingsPhpDirectory() {
359429 $ settingsPath .= '/ ' ;
360430 }
361431
362- 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+ }
363452 }
364453
365454 /**
366455 * Get the shell process.
367456 *
368- * @return Drupal\Console\Core\Command\Exec\ExecCommand
457+ * @return ShellProcess
369458 */
370459 protected function getShellProcess () {
371- return $ this ->container ->get ('console.shell_process ' );
460+ $ app_root = $ this ->container ->get ('app.root ' );
461+ return new ShellProcess ($ app_root );
372462 }
373463
374464 /**
0 commit comments