Skip to content

Commit

Permalink
ACMS-3507: Added SettingsBaseDrush command feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalkhode1 committed Jan 24, 2024
1 parent ee41783 commit 350a8c2
Show file tree
Hide file tree
Showing 4 changed files with 593 additions and 0 deletions.
104 changes: 104 additions & 0 deletions src/Drush/Commands/BaseDrushCommands.php
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);
}
}
}

}
33 changes: 33 additions & 0 deletions src/Robo/Config/ConfigAwareTrait.php
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().
}
Loading

0 comments on commit 350a8c2

Please sign in to comment.