|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Acquia\Drupal\RecommendedSettings\Tests\Traits; |
| 4 | + |
| 5 | +use Acquia\Drupal\RecommendedSettings\Common\ArrayManipulator; |
| 6 | +use Symfony\Component\Console\Helper\Table; |
| 7 | +use Symfony\Component\Console\Output\NullOutput; |
| 8 | +use Symfony\Component\Console\Output\OutputInterface; |
| 9 | +use Symfony\Component\Console\Question\ChoiceQuestion; |
| 10 | +use Symfony\Component\Console\Question\Question; |
| 11 | + |
| 12 | +/** |
| 13 | + * An extension of \Robo\Common\IO. |
| 14 | + * |
| 15 | + * @phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint |
| 16 | + */ |
| 17 | +trait DrsIO { |
| 18 | + |
| 19 | + /** |
| 20 | + * Writes text to screen, without decoration. |
| 21 | + * |
| 22 | + * @param string $text |
| 23 | + * The text to write. |
| 24 | + */ |
| 25 | + protected function say($text): void { |
| 26 | + $this->writeln($text); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Writes text to screen with big, loud decoration. |
| 31 | + * |
| 32 | + * @param string $text |
| 33 | + * The text to write. |
| 34 | + * @param int $length |
| 35 | + * The length at which text should be wrapped. |
| 36 | + * @param string $color |
| 37 | + * The color of the text. |
| 38 | + */ |
| 39 | + protected function yell($text, $length = 40, $color = 'green'): void { |
| 40 | + $format = "<fg=white;bg=$color;options=bold>%s</fg=white;bg=$color;options=bold>"; |
| 41 | + $this->formattedOutput($text, $length, $format); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Prints the message to terminal. |
| 46 | + * |
| 47 | + * @param string $message |
| 48 | + * Given message to print. |
| 49 | + * @param string $type |
| 50 | + * Type of message. Ex: error, success, info etc. |
| 51 | + * @param string $color |
| 52 | + * Given background color. |
| 53 | + */ |
| 54 | + protected function print(string $message, string $type = "success", string $color = ""): void { |
| 55 | + if (!$color) { |
| 56 | + $color = match ($type) { |
| 57 | + "warning" => "yellow", |
| 58 | + "notice" => "cyan", |
| 59 | + "error" => "red", |
| 60 | + default => "green", |
| 61 | + }; |
| 62 | + } |
| 63 | + $message = " <fg=white;bg=$color;options=bold>[$type]</fg=white;bg=$color;options=bold> " . $message; |
| 64 | + $this->say($message); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Format text as a question. |
| 69 | + * |
| 70 | + * @param string $message |
| 71 | + * The question text. |
| 72 | + * |
| 73 | + * @return string |
| 74 | + * The formatted question text. |
| 75 | + */ |
| 76 | + protected function formatQuestion($message): string { |
| 77 | + return "<question> $message</question> "; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Asks the user a multiple-choice question. |
| 82 | + * |
| 83 | + * @param string $question |
| 84 | + * The question text. |
| 85 | + * @param string[] $options |
| 86 | + * An array of available options. |
| 87 | + * @param mixed $default |
| 88 | + * Default. |
| 89 | + * |
| 90 | + * @return string |
| 91 | + * The chosen option. |
| 92 | + */ |
| 93 | + protected function askChoice(string $question, array $options, mixed $default = NULL): string { |
| 94 | + return $this->doAsk(new ChoiceQuestion($this->formatQuestion($question), |
| 95 | + $options, $default)); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Asks a required question. |
| 100 | + * |
| 101 | + * @param string $message |
| 102 | + * The question text. |
| 103 | + * |
| 104 | + * @return string |
| 105 | + * The response. |
| 106 | + */ |
| 107 | + protected function askRequired(string $message): string { |
| 108 | + $question = new Question($this->formatQuestion($message)); |
| 109 | + $question->setValidator(function ($answer) { |
| 110 | + if (empty($answer)) { |
| 111 | + throw new \RuntimeException( |
| 112 | + 'You must enter a value!' |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + return $answer; |
| 117 | + }); |
| 118 | + return $this->doAsk($question); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Writes an array to the screen as a formatted table. |
| 123 | + * |
| 124 | + * @param string[] $array |
| 125 | + * The unformatted array. |
| 126 | + * @param string[] $headers |
| 127 | + * The headers for the array. Defaults to ['Property','Value']. |
| 128 | + */ |
| 129 | + protected function printArrayAsTable( |
| 130 | + array $array, |
| 131 | + array $headers = ['Property', 'Value'] |
| 132 | + ): void { |
| 133 | + $table = new Table($this->output); |
| 134 | + $table->setHeaders($headers) |
| 135 | + ->setRows(ArrayManipulator::convertArrayToFlatTextArray($array)) |
| 136 | + ->render(); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Writes a particular configuration key's value to the log. |
| 141 | + * |
| 142 | + * @param string[] $array |
| 143 | + * The configuration. |
| 144 | + * @param string $prefix |
| 145 | + * A prefix to add to each row in the configuration. |
| 146 | + * @param int $verbosity |
| 147 | + * The verbosity level at which to display the logged message. |
| 148 | + */ |
| 149 | + protected function logConfig(array $array, string $prefix = '', int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE): void { |
| 150 | + if ($this->getOutput()->getVerbosity() >= $verbosity) { |
| 151 | + if ($prefix) { |
| 152 | + $this->getOutput()->writeln("<comment>Configuration for $prefix:</comment>"); |
| 153 | + foreach ($array as $key => $value) { |
| 154 | + $array["$prefix.$key"] = $value; |
| 155 | + unset($array[$key]); |
| 156 | + } |
| 157 | + } |
| 158 | + $this->printArrayAsTable($array); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Asks a required question. |
| 164 | + * |
| 165 | + * @param string $text |
| 166 | + * Prints the output text. |
| 167 | + */ |
| 168 | + protected function writeln($text): void { |
| 169 | + $this->getOutput()->writeln($text); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Formats the output message to terminal. |
| 174 | + * |
| 175 | + * @param string $text |
| 176 | + * Output message to format. |
| 177 | + * @param int $length |
| 178 | + * Length of the output. |
| 179 | + * @param string $format |
| 180 | + * Given string format. |
| 181 | + */ |
| 182 | + protected function formattedOutput($text, $length, $format): void { |
| 183 | + $lines = explode("\n", trim($text, "\n")); |
| 184 | + $maxLineLength = array_reduce(array_map('strlen', $lines), 'max'); |
| 185 | + $length = max($length, $maxLineLength); |
| 186 | + $len = $length + 2; |
| 187 | + $space = str_repeat(' ', $len); |
| 188 | + $this->writeln(sprintf($format, $space)); |
| 189 | + foreach ($lines as $line) { |
| 190 | + $line = str_pad($line, $length, ' ', STR_PAD_BOTH); |
| 191 | + $this->writeln(sprintf($format, " $line ")); |
| 192 | + } |
| 193 | + $this->writeln(sprintf($format, $space)); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * |
| 198 | + * @return $this |
| 199 | + * |
| 200 | + * @see \Robo\Contract\OutputAwareInterface::setOutput() |
| 201 | + */ |
| 202 | + public function setOutput(OutputInterface $output) { |
| 203 | + $this->output = $output; |
| 204 | + |
| 205 | + return $this; |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * |
| 210 | + * @return $this |
| 211 | + * |
| 212 | + * @see \Robo\Contract\OutputAwareInterface::getOutput() |
| 213 | + */ |
| 214 | + public function getOutput(): OutputInterface { |
| 215 | + if (!isset($this->output)) { |
| 216 | + $this->setOutput(new NullOutput()); |
| 217 | + } |
| 218 | + return $this->output; |
| 219 | + } |
| 220 | + |
| 221 | +} |
0 commit comments