4 use Symfony\Component\Console\Helper\QuestionHelper;
5 use Symfony\Component\Console\Question\ConfirmationQuestion;
6 use Symfony\Component\Console\Question\Question;
7 use Symfony\Component\Console\Style\SymfonyStyle;
15 * @var \Symfony\Component\Console\Style\SymfonyStyle
20 * Provide access to SymfonyStyle object.
22 * @return SymfonyStyle
24 * @see http://symfony.com/blog/new-in-symfony-2-8-console-style-guide
26 protected function io()
29 $this->io = new SymfonyStyle($this->input(), $this->output());
35 * @param string $nonDecorated
36 * @param string $decorated
40 protected function decorationCharacter($nonDecorated, $decorated)
42 if (!$this->output()->isDecorated() || (strncasecmp(PHP_OS, 'WIN', 3) == 0)) {
51 protected function say($text)
53 $char = $this->decorationCharacter('>', '➜');
54 $this->writeln("$char $text");
60 * @param string $color
62 protected function yell($text, $length = 40, $color = 'green')
64 $char = $this->decorationCharacter(' ', '➜');
65 $format = "$char <fg=white;bg=$color;options=bold>%s</fg=white;bg=$color;options=bold>";
66 $this->formattedOutput($text, $length, $format);
72 * @param string $format
74 protected function formattedOutput($text, $length, $format)
76 $lines = explode("\n", trim($text, "\n"));
77 $maxLineLength = array_reduce(array_map('strlen', $lines), 'max');
78 $length = max($length, $maxLineLength);
80 $space = str_repeat(' ', $len);
81 $this->writeln(sprintf($format, $space));
82 foreach ($lines as $line) {
83 $line = str_pad($line, $length, ' ', STR_PAD_BOTH);
84 $this->writeln(sprintf($format, " $line "));
86 $this->writeln(sprintf($format, $space));
90 * @param string $question
91 * @param bool $hideAnswer
95 protected function ask($question, $hideAnswer = false)
98 return $this->askHidden($question);
100 return $this->doAsk(new Question($this->formatQuestion($question)));
104 * @param string $question
108 protected function askHidden($question)
110 $question = new Question($this->formatQuestion($question));
111 $question->setHidden(true);
112 return $this->doAsk($question);
116 * @param string $question
117 * @param string $default
121 protected function askDefault($question, $default)
123 return $this->doAsk(new Question($this->formatQuestion("$question [$default]"), $default));
127 * @param string $question
131 protected function confirm($question)
133 return $this->doAsk(new ConfirmationQuestion($this->formatQuestion($question . ' (y/n)'), false));
137 * @param \Symfony\Component\Console\Question\Question $question
141 protected function doAsk(Question $question)
143 return $this->getDialog()->ask($this->input(), $this->output(), $question);
147 * @param string $message
151 protected function formatQuestion($message)
153 return "<question>? $message</question> ";
157 * @return \Symfony\Component\Console\Helper\QuestionHelper
159 protected function getDialog()
161 return new QuestionHelper();
167 protected function writeln($text)
169 $this->output()->writeln($text);