Skip to content

Commit 8e8f3f0

Browse files
authored
Merge pull request #32 from dennisinteractive/27689_remove_hardcoded_web_folder
27689 remove hardcoded web folder
2 parents 0c30d0d + 731ec13 commit 8e8f3f0

16 files changed

+200
-235
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ e.g. https://raw.githubusercontent.com/dennisinteractive/drupal_console_commands
4444
- drupal **site:settings:memcache** *site-name*
4545
Creates *settings.memcache.php* in the *web/sites/default* folder. This file contains Memcache configuration and should not be committed.
4646

47-
- drupal **site:drush:alias** *site-name*
48-
Sets up drush aliases
49-
5047
- drupal **site:phpunit:setup** *site-name*
5148
Creates *phpunit.xml* in the root. This file contains PHPUnit configuration and should not be committed.
5249

@@ -81,7 +78,6 @@ Chains that can be reused on various environments
8178
- site:settings:db
8279
- site:settings:local
8380
- site:settings:memcache
84-
- site:drush:alias
8581

8682
- drupal **site:test:setup** Sets the test suites
8783
- site:phpunit:setup

chain/chain-site-configure.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,3 @@ commands:
1515
- command: site:settings:memcache
1616
arguments:
1717
name: '%{{name}}'
18-
# Create the drush alias
19-
- command: site:drush:alias
20-
arguments:
21-
name: '%{{name}}'

console.config.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ application:
1616
class: \DennisDigital\Drupal\Console\Command\Site\Settings\MemcacheCommand
1717
'site:settings:local':
1818
class: \DennisDigital\Drupal\Console\Command\Site\Settings\LocalCommand
19-
'site:drush:alias':
20-
class: \DennisDigital\Drupal\Console\Command\Site\DrushAliasCommand
2119
'site:db:import':
2220
class: \DennisDigital\Drupal\Console\Command\Site\DbImportCommand
2321
'site:behat:setup':

src/Command/Site/AbstractCommand.php

Lines changed: 118 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

src/Command/Site/AbstractConfigCommand.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ protected function configure() {
7979
*/
8080
protected function interact(InputInterface $input, OutputInterface $output) {
8181
parent::interact($input, $output);
82-
8382
}
8483

8584
/**
@@ -97,8 +96,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
9796
* @throws \DennisDigital\Drupal\Console\Exception\CommandException
9897
*/
9998
protected function generateConfigFile() {
100-
$this->template = $this->destination . $this->template;
101-
$this->filename = $this->destination . $this->filename;
99+
$this->template = $this->getRoot() . $this->template;
100+
$this->filename = $this->getRoot() . $this->filename;
102101

103102
// Validation.
104103
if (!$this->fileExists($this->template)) {

src/Command/Site/Checkout/AbstractRefCommand.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ protected function execute(InputInterface $input, OutputInterface $output) {
7373
$this->io->comment(sprintf('Checking out %s (%s) on %s',
7474
$this->siteName,
7575
$this->ref,
76-
$this->destination
76+
$this->getRoot()
7777
));
7878

7979
switch ($this->repo['type']) {
8080
case 'git':
8181
// Check if repo exists and has any changes.
82-
if ($this->fileExists($this->destination) &&
83-
$this->fileExists($this->destination . '.' . $this->repo['type'])
82+
if ($this->fileExists($this->getRoot()) &&
83+
$this->fileExists($this->getRoot() . '.' . $this->repo['type'])
8484
) {
8585
if ($input->hasOption('force') &&
8686
!$input->getOption('force')
@@ -133,7 +133,7 @@ protected function validateRepo() {
133133
protected function gitDiff() {
134134
$command = sprintf(
135135
'cd %s && git diff-files --name-status -r --ignore-submodules',
136-
$this->shellPath($this->destination)
136+
$this->shellPath($this->getRoot())
137137
);
138138

139139
$shellProcess = $this->getShellProcess();
@@ -143,7 +143,7 @@ protected function gitDiff() {
143143
$message = sprintf('You have uncommitted changes on %s' . PHP_EOL .
144144
'Please commit or revert your changes before checking out the site.' . PHP_EOL .
145145
'If you want to wipe your local changes use --force.',
146-
$this->destination
146+
$this->getRoot()
147147
);
148148
throw new CommandException($message);
149149
}
@@ -165,14 +165,14 @@ protected function gitDiff() {
165165
protected function gitClone() {
166166
$command = sprintf('git clone %s %s',
167167
$this->repo['url'],
168-
$this->shellPath($this->destination)
168+
$this->shellPath($this->getRoot())
169169
);
170170
$this->io->commentBlock($command);
171171

172172
$shellProcess = $this->getShellProcess();
173173

174174
if ($shellProcess->exec($command, TRUE)) {
175-
$this->io->success(sprintf('Repo cloned on %s', $this->destination));
175+
$this->io->success(sprintf('Repo cloned on %s', $this->getRoot()));
176176
}
177177
else {
178178
throw new CommandException($shellProcess->getOutput());
@@ -192,15 +192,15 @@ protected function gitClone() {
192192
* @throws CommandException
193193
*/
194194
protected function gitCheckout() {
195-
$command = sprintf(
196-
'cd %s && ' .
197-
'git fetch --all && ' .
198-
'chmod 777 web/sites/default && ' .
199-
'chmod 777 web/sites/default/settings.php && ' .
200-
'git checkout %s --force',
201-
$this->shellPath($this->destination),
202-
$this->ref
203-
);
195+
196+
$commands = [];
197+
198+
// Checkout commands.
199+
$commands[] = sprintf('cd %s', $this->shellPath($this->getRoot()));
200+
$commands[] = 'git fetch --all';
201+
$commands[] = sprintf('git checkout %s --force', $this->ref);
202+
203+
$command = implode(' && ', $commands);
204204

205205
$shellProcess = $this->getShellProcess();
206206

@@ -221,10 +221,10 @@ protected function gitCheckout() {
221221
* @return mixed
222222
*/
223223
protected function getCurrentRef() {
224-
if ($this->fileExists($this->destination)) {
224+
if ($this->fileExists($this->getRoot())) {
225225
// Get branch from site directory.
226226
$command = sprintf('cd %s && git branch',
227-
$this->shellPath($this->destination)
227+
$this->shellPath($this->getRoot())
228228
);
229229

230230
$shellProcess = $this->getShellProcess()->printOutput(FALSE);

0 commit comments

Comments
 (0)