18
18
use Drupal \Console \Core \Style \DrupalStyle ;
19
19
use Drupal \Console \Core \Command \Shared \CommandTrait ;
20
20
use DennisDigital \Drupal \Console \Command \Exception \CommandException ;
21
+ use DennisDigital \Drupal \Console \Utils \ShellProcess ;
21
22
22
23
/**
23
24
* Class AbstractCommand
@@ -69,11 +70,29 @@ abstract class AbstractCommand extends Command {
69
70
protected $ profile = NULL ;
70
71
71
72
/**
72
- * Stores the destination directory.
73
+ * The root directory.
73
74
*
74
75
* @var string
75
76
*/
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 ;
77
96
78
97
/**
79
98
* Stores the site url.
@@ -179,13 +198,58 @@ protected function validateSiteParams(InputInterface $input, OutputInterface $ou
179
198
// Validate profile.
180
199
$ this ->validateProfile ($ input );
181
200
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 ();
184
209
185
210
// Validate url.
186
211
$ this ->validateUrl ($ input );
187
212
}
188
213
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
+
189
253
/**
190
254
* Helper to check that the config file exits and load the configuration.
191
255
*
@@ -252,54 +316,51 @@ protected function validateProfile(InputInterface $input) {
252
316
}
253
317
254
318
/**
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.
256
328
*
257
329
* @param InputInterface $input
258
- *
259
- * @throws CommandException
260
- *
261
- * @return string Destination
330
+ * @return string
262
331
*/
263
- protected function validateDestination (InputInterface $ input ) {
332
+ protected function validateRoot (InputInterface $ input ) {
264
333
if ($ input ->hasOption ('destination-directory ' ) &&
265
334
!is_null ($ input ->getOption ('destination-directory ' ))
266
335
) {
267
336
// Use config from parameter.
268
- $ this ->destination = $ input ->getOption ('destination-directory ' );
337
+ $ this ->root = $ input ->getOption ('destination-directory ' );
269
338
}
270
339
elseif (isset ($ this ->config ['root ' ])) {
271
340
// Use config from sites.yml.
272
- $ this ->destination = $ this ->config ['root ' ];
341
+ $ this ->root = $ this ->config ['root ' ];
273
342
}
274
343
else {
275
- $ this ->destination = '/tmp/ ' . $ this ->siteName ;
344
+ $ this ->root = '/tmp/ ' . $ this ->siteName ;
276
345
}
277
346
278
347
// Allow destination to be overriden by environment variable. i.e.
279
348
// export site_destination_directory="/directory/"
280
349
if (!getenv ('site_destination_directory ' )) {
281
- putenv ("site_destination_directory= $ this ->destination " );
350
+ putenv ("site_destination_directory= $ this ->root " );
282
351
}
283
352
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 ' );
290
354
}
291
355
292
- return $ this ->destination ;
356
+ $ this -> root = rtrim ( $ this ->root , ' / ' ) . ' / ' ;
293
357
}
294
358
295
359
/**
296
- * Helper to validate destination parameter .
360
+ * Helper to validate URL .
297
361
*
298
362
* @param InputInterface $input
299
- *
300
- * @throws CommandException
301
- *
302
- * @return string Destination
363
+ * @return string
303
364
*/
304
365
protected function validateUrl (InputInterface $ input ) {
305
366
$ scheme = isset ($ this ->config ['scheme ' ]) && !empty ($ this ->config ['scheme ' ]) ? $ this ->config ['scheme ' ] : 'http ' ;
@@ -317,16 +378,25 @@ protected function validateUrl(InputInterface $input) {
317
378
}
318
379
319
380
/**
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
+ *
321
385
* It will try to match a folder with same name as site name
322
386
* If not found, it will try to match a folder called "default".
323
387
*
324
388
* @return string Path
325
389
*/
326
- public function settingsPhpDirectory () {
327
- $ webSitesPath = $ this ->destination . 'web/ sites/ ' ;
390
+ public function validateSiteRoot () {
391
+ $ webSitesPath = $ this ->getWebRoot () . 'sites/ ' ;
328
392
$ settingsPath = $ webSitesPath . 'default ' ;
329
393
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
+
330
400
$ command = sprintf (
331
401
'cd %s && find . -name settings.php ' ,
332
402
$ this ->shellPath ($ webSitesPath )
@@ -359,16 +429,36 @@ public function settingsPhpDirectory() {
359
429
$ settingsPath .= '/ ' ;
360
430
}
361
431
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
+ }
363
452
}
364
453
365
454
/**
366
455
* Get the shell process.
367
456
*
368
- * @return Drupal\Console\Core\Command\Exec\ExecCommand
457
+ * @return ShellProcess
369
458
*/
370
459
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 );
372
462
}
373
463
374
464
/**
0 commit comments