4 * Contains \Drupal\Console\Core\Style\DrupalStyle.
7 namespace Drupal\Console\Core\Style;
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Output\OutputInterface;
11 use Symfony\Component\Console\Question\ChoiceQuestion;
12 use Symfony\Component\Console\Question\Question;
13 use Symfony\Component\Console\Style\SymfonyStyle;
14 use Symfony\Component\Console\Helper\Table;
15 use Drupal\Console\Core\Helper\DrupalChoiceQuestionHelper;
20 * @package Drupal\Console\Core\Style
22 class DrupalStyle extends SymfonyStyle
30 * @param InputInterface $input
31 * @param OutputInterface $output
33 public function __construct(InputInterface $input, OutputInterface $output)
35 $this->input = $input;
36 parent::__construct($input, $output);
40 * @param string $question
41 * @param array $choices
42 * @param mixed $default
43 * @param bool $skipValidation
47 public function choiceNoList(
51 $skipValidation = false
53 if (is_null($default)) {
54 $default = current($choices);
57 if (!in_array($default, $choices)) {
58 $choices[] = $default;
61 if (null !== $default) {
62 $values = array_flip($choices);
63 $default = $values[$default];
66 $choiceQuestion = new ChoiceQuestion($question, $choices, $default);
67 if ($skipValidation) {
68 $choiceQuestion->setValidator(
75 return trim($this->askChoiceQuestion($choiceQuestion));
79 * @param string $question
80 * @param array $choices
81 * @param null $default
82 * @param bool $multiple
86 public function choice($question, array $choices, $default = null, $multiple = false)
88 if (null !== $default) {
89 $values = array_flip($choices);
90 $default = $values[$default];
93 $choiceQuestion = new ChoiceQuestion($question, $choices, $default);
94 $choiceQuestion->setMultiselect($multiple);
96 return $this->askQuestion($choiceQuestion);
100 * @param ChoiceQuestion $question
104 public function askChoiceQuestion(ChoiceQuestion $question)
106 $questionHelper = new DrupalChoiceQuestionHelper();
107 $answer = $questionHelper->ask($this->input, $this, $question);
116 public function askHiddenEmpty($question)
118 $question = new Question($question, '');
119 $question->setHidden(true);
120 $question->setValidator(
126 return trim($this->askQuestion($question));
130 * @param string $question
131 * @param string $default
132 * @param null|callable $validator
136 public function askEmpty($question, $default = '', $validator = null)
138 $question = new Question($question, $default);
140 $validator = function ($answer) {
144 $question->setValidator($validator);
146 return trim($this->askQuestion($question));
151 * @param bool $newLine
153 public function info($message, $newLine = true)
155 $message = sprintf('<info> %s</info>', $message);
157 $this->writeln($message);
159 $this->write($message);
164 * @param array|string $message
165 * @param bool $newLine
167 public function comment($message, $newLine = true)
169 $message = sprintf('<comment> %s</comment>', $message);
171 $this->writeln($message);
173 $this->write($message);
180 public function commentBlock($message)
184 'bg=yellow;fg=black',
191 * @param array $headers
193 * @param string $style
195 public function table(array $headers, array $rows, $style = 'symfony-style-guide')
197 $headers = array_map(
199 return sprintf('<info>%s</info>', $value);
203 if (!is_array(current($rows))) {
212 $table = new Table($this);
213 $table->setHeaders($headers);
214 $table->setRows($rows);
215 $table->setStyle($style);
223 * @param bool $newLine
225 public function simple($message, $newLine = true)
227 $message = sprintf(' %s', $message);
229 $this->writeln($message);
231 $this->write($message);
238 public function warning($message)
240 $this->block($message, 'WARNING', 'fg=white;bg=yellow', ' ', true);
244 * @param array|string $message
246 public function text($message)
248 $message = sprintf('// %s', $message);
249 parent::text($message);
252 public function successLite($message, $newLine = false)
254 $message = sprintf('<info>✔</info> %s', $message);
255 parent::text($message);
261 public function errorLite($message, $newLine = false)
263 $message = sprintf('<fg=red>✘</> %s', $message);
264 parent::text($message);
270 public function warningLite($message, $newLine = false)
272 $message = sprintf('<comment>!</comment> %s', $message);
273 parent::text($message);
279 public function customLite($message, $prefix = '*', $style = '', $newLine = false)
296 parent::text($message);
303 * @return InputInterface
305 public function getInput()