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 $allowEmpty
47 public function choiceNoList($question, array $choices, $default = null, $allowEmpty = 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 return trim($this->askChoiceQuestion(new ChoiceQuestion($question, $choices, $default)));
70 * @param string $question
71 * @param array $choices
72 * @param null $default
73 * @param bool $multiple
77 public function choice($question, array $choices, $default = null, $multiple = false)
79 if (null !== $default) {
80 $values = array_flip($choices);
81 $default = $values[$default];
84 $choiceQuestion = new ChoiceQuestion($question, $choices, $default);
85 $choiceQuestion->setMultiselect($multiple);
87 return $this->askQuestion($choiceQuestion);
91 * @param ChoiceQuestion $question
95 public function askChoiceQuestion(ChoiceQuestion $question)
97 $questionHelper = new DrupalChoiceQuestionHelper();
98 $answer = $questionHelper->ask($this->input, $this, $question);
108 public function askHiddenEmpty($question)
110 $question = new Question($question, ' ');
111 $question->setHidden(true);
113 return trim($this->askQuestion($question));
117 * @param string $question
118 * @param null|callable $validator
122 public function askEmpty($question, $validator = null)
124 $question = new Question($question, ' ');
125 $question->setValidator($validator);
127 return trim($this->askQuestion($question));
132 * @param bool $newLine
134 public function info($message, $newLine = true)
136 $message = sprintf('<info> %s</info>', $message);
138 $this->writeln($message);
140 $this->write($message);
145 * @param array|string $message
146 * @param bool $newLine
148 public function comment($message, $newLine = true)
150 $message = sprintf('<comment> %s</comment>', $message);
152 $this->writeln($message);
154 $this->write($message);
161 public function commentBlock($message)
165 'bg=yellow;fg=black',
172 * @param array $headers
174 * @param string $style
176 public function table(array $headers, array $rows, $style = 'symfony-style-guide')
178 $headers = array_map(
180 return sprintf('<info>%s</info>', $value);
184 if (!is_array(current($rows))) {
193 $table = new Table($this);
194 $table->setHeaders($headers);
195 $table->setRows($rows);
196 $table->setStyle($style);
204 * @param bool $newLine
206 public function simple($message, $newLine = true)
208 $message = sprintf(' %s', $message);
210 $this->writeln($message);
212 $this->write($message);
217 * @param array|string $message
219 public function text($message)
221 $message = sprintf('// %s', $message);
222 parent::text($message);
225 public function successLite($message, $newLine = false)
227 $message = sprintf('<info>✔</info> %s', $message);
228 parent::text($message);
234 public function errorLite($message, $newLine = false)
236 $message = sprintf('<fg=red>✘</> %s', $message);
237 parent::text($message);
243 public function warningLite($message, $newLine = false)
245 $message = sprintf('<comment>!</comment> %s', $message);
246 parent::text($message);
252 public function customLite($message, $prefix = '*', $style = '', $newLine = false)
269 parent::text($message);