-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ACMS-3507: Added SettingsBaseDrush command feature.
- Loading branch information
1 parent
ee41783
commit 350a8c2
Showing
4 changed files
with
593 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Acquia\Drupal\RecommendedSettings\Drush\Commands; | ||
|
||
use Acquia\Drupal\RecommendedSettings\Exceptions\SettingsException; | ||
use Acquia\Drupal\RecommendedSettings\Robo\Config\ConfigAwareTrait; | ||
use Acquia\Drupal\RecommendedSettings\Robo\Tasks\LoadTasks; | ||
use Consolidation\AnnotatedCommand\AnnotationData; | ||
use Consolidation\AnnotatedCommand\Hooks\HookManager; | ||
use Drush\Attributes as Cli; | ||
use Drush\Commands\DrushCommands; | ||
use Drush\Drush; | ||
use League\Container\ContainerAwareTrait; | ||
use Robo\Contract\ConfigAwareInterface; | ||
use Psr\Log\LoggerAwareInterface; | ||
use Robo\Contract\BuilderAwareInterface; | ||
use Robo\Contract\IOAwareInterface; | ||
use League\Container\ContainerAwareInterface; | ||
use Robo\LoadAllTasks; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Process\Process; | ||
|
||
/** | ||
* A Drush command to generate settings.php for Multisite. | ||
*/ | ||
class BaseDrushCommands extends DrushCommands implements ConfigAwareInterface, LoggerAwareInterface, BuilderAwareInterface, IOAwareInterface, ContainerAwareInterface { | ||
|
||
use ContainerAwareTrait; | ||
use LoadAllTasks; | ||
use ConfigAwareTrait; | ||
use LoadTasks; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
#[CLI\Hook(type: HookManager::INITIALIZE)] | ||
public function init(InputInterface $input, AnnotationData $annotationData): void { | ||
if ($this->getConfig()->get("options.uri")) { | ||
$this->getConfig()->setDefault('drush.uri', $this->getConfig()->get("options.uri")); | ||
} | ||
if ($this->getConfig()->get("options.ansi")) { | ||
$this->getConfig()->setDefault('drush.ansi', $this->getConfig()->get("options.ansi")); | ||
} | ||
$this->getConfig()->setDefault('drush.bin', $this->getConfig()->get("runtime.drush-script")); | ||
} | ||
|
||
/** | ||
* Invokes an array of Drush commands. | ||
* | ||
* @param array $commands | ||
* An array of Symfony commands to invoke, e.g., 'tests:behat:run'. | ||
* | ||
* @throws \Acquia\Drupal\RecommendedSettings\Exceptions\SettingsException | ||
*/ | ||
protected function invokeCommands(array $commands) { | ||
foreach ($commands as $key => $value) { | ||
if (is_numeric($key)) { | ||
$command = $value; | ||
$args = []; | ||
} | ||
else { | ||
$command = $key; | ||
$args = $value; | ||
} | ||
$this->invokeCommand($command, $args); | ||
} | ||
} | ||
|
||
/** | ||
* Invokes a single Drush command. | ||
* | ||
* @param string $command_name | ||
* The name of the command, e.g., 'tests:behat:run'. | ||
* @param array $args | ||
* An array of arguments to pass to the command. | ||
* | ||
* @throws \Acquia\Drupal\RecommendedSettings\Exceptions\SettingsException | ||
*/ | ||
protected function invokeCommand(string $command_name, array $args = []): void { | ||
$options = $this->input()->getOptions(); | ||
foreach ($options as $key => $value) { | ||
if ($value === NULL || $key === "define") { | ||
unset($options[$key]); | ||
} | ||
} | ||
$process = Drush::drush(Drush::aliasManager()->getSelf(), $command_name, $args, $options); | ||
$this->output->writeln("<comment> > " . $process->getCommandLine() . "</comment>"); | ||
$process->run($process->showRealtime()->hideStderr()); | ||
$errorOutput = $process->getErrorOutput(); | ||
if ($errorOutput) { | ||
if (!$process->isSuccessful()) { | ||
$errorOutput = preg_replace("/^\s+|\s+$/", "", $errorOutput); | ||
throw new SettingsException($errorOutput); | ||
} | ||
else { | ||
$errorOutput = str_replace("[success]", "<bg=green;options=bold;fg=white>[success]</>", $errorOutput); | ||
$this->io()->write($errorOutput); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Acquia\Drupal\RecommendedSettings\Robo\Config; | ||
|
||
use Robo\Common\ConfigAwareTrait as RoboConfigAwareTrait; | ||
|
||
/** | ||
* Adds custom methods to RoboConfigAwareTrait. | ||
*/ | ||
trait ConfigAwareTrait { | ||
|
||
use RoboConfigAwareTrait; | ||
|
||
/** | ||
* Gets a config value for a given key. | ||
* | ||
* @param string $key | ||
* The config key. | ||
* @param mixed|null $default | ||
* The default value if the key does not exist in config. | ||
* | ||
* @return mixed|null | ||
* The config value, or else the default value if they key does not exist. | ||
*/ | ||
protected function getConfigValue($key, $default = NULL) { | ||
if (!$this->getConfig()) { | ||
return $default; | ||
} | ||
return $this->getConfig()->get($key, $default); | ||
} | ||
|
||
// @todo add hasConfigValue(). | ||
} |
Oops, something went wrong.