Skip to content

Commit 3495d6a

Browse files
committed
Case 27689; replaced direct access to properties with getters
1 parent 62e990a commit 3495d6a

14 files changed

+78
-47
lines changed

src/Command/Site/AbstractCommand.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ abstract class AbstractCommand extends Command {
7474
*
7575
* @var string
7676
*/
77-
protected $root = NULL;
77+
private $root = NULL;
7878

7979
/**
8080
* The web root directory.
@@ -83,7 +83,7 @@ abstract class AbstractCommand extends Command {
8383
*
8484
* @var string
8585
*/
86-
protected $webRoot = NULL;
86+
private $webRoot = NULL;
8787

8888
/**
8989
* The site root directory.
@@ -92,7 +92,7 @@ abstract class AbstractCommand extends Command {
9292
*
9393
* @var string
9494
*/
95-
protected $siteRoot = NULL;
95+
private $siteRoot = NULL;
9696

9797
/**
9898
* Stores the site url.
@@ -211,6 +211,36 @@ protected function validateSiteParams(InputInterface $input, OutputInterface $ou
211211
$this->validateUrl($input);
212212
}
213213

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+
214244
/**
215245
* Helper to check that the config file exits and load the configuration.
216246
*
@@ -281,7 +311,7 @@ protected function validateProfile(InputInterface $input) {
281311
*/
282312
protected function validateWebRoot() {
283313
$web_directory = empty($this->config['web_directory']) ? 'web' : $this->config['web_directory'];
284-
$this->webRoot = $this->root . trim($web_directory, '/') . '/';
314+
$this->webRoot = $this->getRoot() . trim($web_directory, '/') . '/';
285315
}
286316

287317
/**
@@ -349,7 +379,7 @@ protected function validateUrl(InputInterface $input) {
349379
* @return string Path
350380
*/
351381
public function validateSiteRoot() {
352-
$webSitesPath = $this->webRoot . 'sites/';
382+
$webSitesPath = $this->getWebRoot() . 'sites/';
353383
$settingsPath = $webSitesPath . 'default';
354384

355385
// It's possible that a command is run before the site is available. e.g. checkout

src/Command/Site/AbstractConfigCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
9696
* @throws \DennisDigital\Drupal\Console\Exception\CommandException
9797
*/
9898
protected function generateConfigFile() {
99-
$this->template = $this->root . $this->template;
100-
$this->filename = $this->root . $this->filename;
99+
$this->template = $this->getRoot() . $this->template;
100+
$this->filename = $this->getRoot() . $this->filename;
101101

102102
// Validation.
103103
if (!$this->fileExists($this->template)) {

src/Command/Site/Checkout/AbstractRefCommand.php

Lines changed: 12 additions & 12 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->root
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->root) &&
83-
$this->fileExists($this->root . '.' . $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->root)
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->root
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->root)
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->root));
175+
$this->io->success(sprintf('Repo cloned on %s', $this->getRoot()));
176176
}
177177
else {
178178
throw new CommandException($shellProcess->getOutput());
@@ -193,10 +193,10 @@ protected function gitClone() {
193193
*/
194194
protected function gitCheckout() {
195195
$commands = [
196-
sprintf('cd %s', $this->shellPath($this->root)),
196+
sprintf('cd %s', $this->shellPath($this->getRoot())),
197197
'git fetch --all',
198-
sprintf('chmod 777 %ssites/default', $this->webRoot),
199-
sprintf('chmod 777 %ssites/default/settings.php', $this->webRoot),
198+
sprintf('chmod 777 %ssites/default', $this->getWebRoot()),
199+
sprintf('chmod 777 %ssites/default/settings.php', $this->getWebRoot()),
200200
sprintf('git checkout %s --force', $this->ref),
201201
];
202202
$command = implode(' && ', $commands);
@@ -220,10 +220,10 @@ protected function gitCheckout() {
220220
* @return mixed
221221
*/
222222
protected function getCurrentRef() {
223-
if ($this->fileExists($this->root)) {
223+
if ($this->fileExists($this->getRoot())) {
224224
// Get branch from site directory.
225225
$command = sprintf('cd %s && git branch',
226-
$this->shellPath($this->root)
226+
$this->shellPath($this->getRoot())
227227
);
228228

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

src/Command/Site/ComposeCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ protected function interact(InputInterface $input, OutputInterface $output) {
4444
protected function execute(InputInterface $input, OutputInterface $output) {
4545
parent::execute($input, $output);
4646

47-
if (!$this->fileExists($this->root . 'composer.json')) {
47+
if (!$this->fileExists($this->getRoot() . 'composer.json')) {
4848
$message = sprintf('The file composer.json is missing on %s',
49-
$this->root
49+
$this->getRoot()
5050
);
5151
throw new CommandException($message);
5252
}
@@ -67,7 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
6767
protected function runCommand($command) {
6868
$command = sprintf(
6969
'cd %s && composer %s',
70-
$this->shellPath($this->root),
70+
$this->shellPath($this->getRoot()),
7171
$command
7272
);
7373
$this->io->commentBlock($command);
@@ -76,7 +76,7 @@ protected function runCommand($command) {
7676

7777
//@todo Show a progress bar.
7878
if ($shellProcess->exec($command, TRUE)) {
79-
$this->io->success(sprintf('Composer installed on %s', $this->root));
79+
$this->io->success(sprintf('Composer installed on %s', $this->getRoot()));
8080
}
8181
else {
8282
throw new CommandException($shellProcess->getOutput());

src/Command/Site/DbImportCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
155155
'chmod 777 settings.php && ' .
156156
'drush si -y %s %s && ' .
157157
'drush cset "system.site" uuid "$(drush cget system.site uuid --source=sync --format=list)" -y',
158-
$this->shellPath($this->siteRoot),
158+
$this->shellPath($this->getSiteRoot()),
159159
$this->profile,
160160
$options
161161
);
@@ -169,7 +169,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
169169
'cd %s; ' .
170170
'drush sql-create %s -y; ' .
171171
'drush sql-cli < %s; ',
172-
$this->siteRoot,
172+
$this->getSiteRoot(),
173173
$input->getOption('db-name'),
174174
$this->filename
175175
);
@@ -185,7 +185,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
185185
$this->io->writeln($shellProcess->getOutput());
186186
$this->io->success(sprintf(
187187
"Site installed on %s\nURL %s",
188-
$this->siteRoot,
188+
$this->getSiteRoot(),
189189
$this->config['host']
190190
));
191191
}

src/Command/Site/DrushAliasCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
6565
parent::execute($input, $output);
6666

6767
// Remove existing file.
68-
$file = $this->webRoot . $this->dir . $this->filename;
68+
$file = $this->getWebRoot() . $this->dir . $this->filename;
6969
if ($this->fileExists($file)) {
7070
$this->fileUnlink($file);
7171
}
@@ -79,7 +79,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
7979
* the generic Drush alias.
8080
*/
8181
\$aliases["site"] = array (
82-
'root' => '{$this->webRoot}',
82+
'root' => '{$this->getWebRoot()}',
8383
'uri' => '{$this->url}',
8484
'user' => 1,
8585
);

src/Command/Site/GruntCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ protected function execute(InputInterface $input, OutputInterface $output) {
4545
parent::execute($input, $output);
4646

4747
$this->io->comment(sprintf('Running Grunt on %s',
48-
$this->webRoot
48+
$this->getWebRoot()
4949
));
5050

5151
$command = sprintf(
5252
'cd %s && ' .
5353
'find . -type d \( -name node_modules -o -name contrib -o -path ./core \) -prune -o -name Gruntfile.js -execdir sh -c "grunt" \;',
54-
$this->shellPath($this->webRoot)
54+
$this->shellPath($this->getWebRoot())
5555
);
5656

5757
// Run.

src/Command/Site/NPMCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ protected function execute(InputInterface $input, OutputInterface $output) {
4545
parent::execute($input, $output);
4646

4747
$this->io->comment(sprintf('Running NPM on %s',
48-
$this->webRoot
48+
$this->getWebRoot()
4949
));
5050

5151
$command = sprintf(
5252
'cd %s && ' .
5353
'find . -type d \( -name node_modules -o -name contrib -o -path ./core \) -prune -o -name package.json -execdir sh -c "npm install" \;',
54-
$this->shellPath($this->webRoot)
54+
$this->shellPath($this->getWebRoot())
5555
);
5656

5757
// Run.

src/Command/Site/Settings/DbCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ protected function execute(InputInterface $input, OutputInterface $output) {
5959
parent::execute($input, $output);
6060

6161
// Validation.
62-
if (!$this->fileExists($this->siteRoot . 'settings.php')) {
62+
if (!$this->fileExists($this->getSiteRoot() . 'settings.php')) {
6363
$message = sprintf('Could not find %s',
64-
$this->siteRoot . 'settings.php'
64+
$this->getSiteRoot() . 'settings.php'
6565
);
6666
throw new CommandException($message);
6767
}
@@ -83,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
8383
}
8484

8585
// Remove existing file.
86-
$file = $this->siteRoot . $this->filename;
86+
$file = $this->getSiteRoot() . $this->filename;
8787
if ($this->fileExists($file)) {
8888
$this->fileUnlink($file);
8989
}

src/Command/Site/Settings/LocalCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,22 @@ protected function execute(InputInterface $input, OutputInterface $output) {
5454
parent::execute($input, $output);
5555

5656
// Validation.
57-
if (!$this->fileExists($this->siteRoot . '../example.' . $this->filename)) {
57+
if (!$this->fileExists($this->getSiteRoot() . '../example.' . $this->filename)) {
5858
$message = sprintf('The file example.settings.local.php is missing.',
59-
$this->siteRoot
59+
$this->getSiteRoot()
6060
);
6161
throw new CommandException($message);
6262
}
6363

6464
// Remove existing file.
65-
$file = $this->siteRoot . $this->filename;
65+
$file = $this->getSiteRoot() . $this->filename;
6666
if ($this->fileExists($file)) {
6767
$this->fileUnlink($file);
6868
}
6969

7070
// Copy example.
7171
$command = sprintf('cd %s && cp -n ../example.%s %s',
72-
$this->shellPath($this->siteRoot),
72+
$this->shellPath($this->getSiteRoot()),
7373
$this->filename,
7474
$this->filename
7575
);

0 commit comments

Comments
 (0)