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;
19 * @package Drupal\Console\Core\Style
21 class DrupalStyle extends SymfonyStyle
29 * @param InputInterface $input
30 * @param OutputInterface $output
32 public function __construct(InputInterface $input, OutputInterface $output)
34 $this->input = $input;
35 parent::__construct($input, $output);
39 * @param string $question
40 * @param array $choices
41 * @param mixed $default
42 * @param bool $allowEmpty
46 public function choiceNoList($question, array $choices, $default = null, $allowEmpty = false)
52 if (is_null($default)) {
53 $default = current($choices);
56 if (!in_array($default, $choices)) {
57 $choices[] = $default;
60 if (null !== $default) {
61 $values = array_flip($choices);
62 $default = $values[$default];
65 return trim($this->askChoiceQuestion(new ChoiceQuestion($question, $choices, $default)));
69 * @param string $question
70 * @param array $choices
71 * @param null $default
72 * @param bool $multiple
76 public function choice($question, array $choices, $default = null, $multiple = false)
78 if (null !== $default) {
79 $values = array_flip($choices);
80 $default = $values[$default];
83 $choiceQuestion = new ChoiceQuestion($question, $choices, $default);
84 $choiceQuestion->setMultiselect($multiple);
86 return $this->askQuestion($choiceQuestion);
90 * @param ChoiceQuestion $question
94 public function askChoiceQuestion(ChoiceQuestion $question)
96 $questionHelper = new DrupalChoiceQuestionHelper();
97 $answer = $questionHelper->ask($this->input, $this, $question);
107 public function askHiddenEmpty($question)
109 $question = new Question($question, ' ');
110 $question->setHidden(true);
112 return trim($this->askQuestion($question));
116 * @param string $question
117 * @param null|callable $validator
121 public function askEmpty($question, $validator = null)
123 $question = new Question($question, ' ');
124 $question->setValidator($validator);
126 return trim($this->askQuestion($question));
131 * @param bool $newLine
133 public function info($message, $newLine = true)
135 $message = sprintf('<info> %s</info>', $message);
137 $this->writeln($message);
139 $this->write($message);
144 * @param array|string $message
145 * @param bool $newLine
147 public function comment($message, $newLine = true)
149 $message = sprintf('<comment> %s</comment>', $message);
151 $this->writeln($message);
153 $this->write($message);
160 public function commentBlock($message)
164 'bg=yellow;fg=black',
171 * @param array $headers
173 * @param string $style
175 public function table(array $headers, array $rows, $style = 'symfony-style-guide')
177 $headers = array_map(
179 return sprintf('<info>%s</info>', $value);
183 if (!is_array(current($rows))) {
192 $table = new Table($this);
193 $table->setHeaders($headers);
194 $table->setRows($rows);
195 $table->setStyle($style);
203 * @param bool $newLine
205 public function simple($message, $newLine = true)
207 $message = sprintf(' %s', $message);
209 $this->writeln($message);
211 $this->write($message);
216 * @param array|string $message
218 public function text($message)
220 $message = sprintf('// %s', $message);
221 parent::text($message);
224 public function successLite($message, $newLine = false) {
225 $message = sprintf('<info>✔</info> %s', $message);
226 parent::text($message);
232 public function errorLite($message, $newLine = false) {
233 $message = sprintf('<fg=red>✘</> %s', $message);
234 parent::text($message);
240 public function warningLite($message, $newLine = false) {
241 $message = sprintf('<comment>!</comment> %s', $message);
242 parent::text($message);
248 public function customLite($message, $prefix = '*', $style = '', $newLine = false) {
264 parent::text($message);