-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ACMS-3507: Refactor BaseDrushCommand feature.
- Loading branch information
1 parent
d3bb8e2
commit 24ab153
Showing
6 changed files
with
268 additions
and
56 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,135 @@ | ||
<?php | ||
|
||
namespace Acquia\Drupal\RecommendedSettings\Common; | ||
|
||
use Symfony\Component\Console\Helper\Table; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\ChoiceQuestion; | ||
use Symfony\Component\Console\Question\Question; | ||
|
||
/** | ||
* An extension of \Robo\Common\IO. | ||
*/ | ||
trait IO { | ||
|
||
/** | ||
* Writes text to screen, without decoration. | ||
* | ||
* @param string $text | ||
* The text to write. | ||
*/ | ||
protected function say($text) { | ||
$this->writeln($text); | ||
} | ||
|
||
/** | ||
* Writes text to screen with big, loud decoration. | ||
* | ||
* @param string $text | ||
* The text to write. | ||
* @param int $length | ||
* The length at which text should be wrapped. | ||
* @param string $color | ||
* The color of the text. | ||
*/ | ||
protected function yell($text, $length = 40, $color = 'green') { | ||
$format = "<fg=white;bg=$color;options=bold>%s</fg=white;bg=$color;options=bold>"; | ||
$this->formattedOutput($text, $length, $format); | ||
} | ||
|
||
/** | ||
* Format text as a question. | ||
* | ||
* @param string $message | ||
* The question text. | ||
* | ||
* @return string | ||
* The formatted question text. | ||
*/ | ||
protected function formatQuestion($message) { | ||
return "<question> $message</question> "; | ||
} | ||
|
||
/** | ||
* Asks the user a multiple-choice question. | ||
* | ||
* @param string $question | ||
* The question text. | ||
* @param array $options | ||
* An array of available options. | ||
* @param mixed $default | ||
* Default. | ||
* | ||
* @return string | ||
* The chosen option. | ||
*/ | ||
protected function askChoice($question, array $options, $default = NULL) { | ||
return $this->doAsk(new ChoiceQuestion($this->formatQuestion($question), | ||
$options, $default)); | ||
} | ||
|
||
/** | ||
* Asks a required question. | ||
* | ||
* @param string $message | ||
* The question text. | ||
* | ||
* @return string | ||
* The response. | ||
*/ | ||
protected function askRequired($message) { | ||
$question = new Question($this->formatQuestion($message)); | ||
$question->setValidator(function ($answer) { | ||
if (empty($answer)) { | ||
throw new \RuntimeException( | ||
'You must enter a value!' | ||
); | ||
} | ||
|
||
return $answer; | ||
}); | ||
return $this->doAsk($question); | ||
} | ||
|
||
/** | ||
* Writes an array to the screen as a formatted table. | ||
* | ||
* @param array $array | ||
* The unformatted array. | ||
* @param array $headers | ||
* The headers for the array. Defaults to ['Property','Value']. | ||
*/ | ||
protected function printArrayAsTable( | ||
array $array, | ||
array $headers = ['Property', 'Value'] | ||
) { | ||
$table = new Table($this->output); | ||
$table->setHeaders($headers) | ||
->setRows(ArrayManipulator::convertArrayToFlatTextArray($array)) | ||
->render(); | ||
} | ||
|
||
/** | ||
* Writes a particular configuration key's value to the log. | ||
* | ||
* @param array $array | ||
* The configuration. | ||
* @param string $prefix | ||
* A prefix to add to each row in the configuration. | ||
* @param int $verbosity | ||
* The verbosity level at which to display the logged message. | ||
*/ | ||
protected function logConfig(array $array, $prefix = '', $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE) { | ||
if ($this->output()->getVerbosity() >= $verbosity) { | ||
if ($prefix) { | ||
$this->output()->writeln("<comment>Configuration for $prefix:</comment>"); | ||
foreach ($array as $key => $value) { | ||
$array["$prefix.$key"] = $value; | ||
unset($array[$key]); | ||
} | ||
} | ||
$this->printArrayAsTable($array); | ||
} | ||
} | ||
|
||
} |
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
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,30 @@ | ||
<?php | ||
|
||
namespace Acquia\Drupal\RecommendedSettings\Config; | ||
|
||
use Consolidation\Config\Config; | ||
use Grasmash\YamlExpander\YamlExpander; | ||
use Psr\Log\NullLogger; | ||
|
||
/** | ||
* The configuration for settings. | ||
*/ | ||
class DefaultConfig extends Config { | ||
|
||
/** | ||
* Config Constructor. | ||
* | ||
* @param string[] $data | ||
* Data array, if available. | ||
*/ | ||
public function __construct(string $drupal_root) { | ||
parent::__construct(); | ||
$repo_root = dirname($drupal_root); | ||
$this->set('repo.root', $repo_root); | ||
$this->set('docroot', $drupal_root); | ||
$this->set('composer.bin', $repo_root . '/vendor/bin'); | ||
$this->set('site', 'default'); | ||
$this->set('tmp.dir', sys_get_temp_dir()); | ||
} | ||
|
||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace Acquia\Drupal\RecommendedSettings\Config; | ||
|
||
use Consolidation\Config\Config; | ||
use Drush\Config\DrushConfig; | ||
use Grasmash\YamlExpander\YamlExpander; | ||
use Psr\Log\NullLogger; | ||
|
||
/** | ||
* The configuration for settings. | ||
*/ | ||
class DefaultDrushConfig extends Config { | ||
|
||
/** | ||
* Config Constructor. | ||
* | ||
* @param string[] $data | ||
* Data array, if available. | ||
*/ | ||
public function __construct(DrushConfig $config) { | ||
$uri = $config->get("options.uri") ?? "default"; | ||
$config->set('repo.root', $config->get("runtime.project")); | ||
$config->set('docroot', $config->get("options.root")); | ||
$config->set('composer.bin', $config->get("drush.vendor-dir") . '/bin'); | ||
$config->set('drush.uri', $config->get("options.uri")); | ||
$config->set('site', $config->get("options.uri")); | ||
if ($config->get("options.ansi")) { | ||
$config->set('drush.ansi', $config->get("options.ansi")); | ||
} | ||
$config->set('drush.bin', $config->get("runtime.drush-script")); | ||
$config->setDefault('drush.alias', "self"); | ||
parent::__construct($config->export()); | ||
} | ||
|
||
} |
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
Oops, something went wrong.