TYPO3  7.6
SymfonyQuestionHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Symfony\Component\Console\Helper;
13 
20 
27 {
31  public function ask(InputInterface $input, OutputInterface $output, Question $question)
32  {
33  $validator = $question->getValidator();
34  $question->setValidator(function ($value) use ($validator) {
35  if (null !== $validator && is_callable($validator)) {
36  $value = $validator($value);
37  }
38 
39  // make required
40  if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) {
41  throw new \Exception('A value is required.');
42  }
43 
44  return $value;
45  });
46 
47  return parent::ask($input, $output, $question);
48  }
49 
53  protected function writePrompt(OutputInterface $output, Question $question)
54  {
55  $text = $question->getQuestion();
56  $default = $question->getDefault();
57 
58  switch (true) {
59  case null === $default:
60  $text = sprintf(' <info>%s</info>:', $text);
61 
62  break;
63 
64  case $question instanceof ConfirmationQuestion:
65  $text = sprintf(' <info>%s (yes/no)</info> [<comment>%s</comment>]:', $text, $default ? 'yes' : 'no');
66 
67  break;
68 
69  case $question instanceof ChoiceQuestion:
70  $choices = $question->getChoices();
71  $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $choices[$default]);
72 
73  break;
74 
75  default:
76  $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $default);
77  }
78 
79  $output->writeln($text);
80 
81  if ($question instanceof ChoiceQuestion) {
82  $width = max(array_map('strlen', array_keys($question->getChoices())));
83 
84  foreach ($question->getChoices() as $key => $value) {
85  $output->writeln(sprintf(" [<comment>%-${width}s</comment>] %s", $key, $value));
86  }
87  }
88 
89  $output->write(' > ');
90  }
91 
95  protected function writeError(OutputInterface $output, \Exception $error)
96  {
97  if ($output instanceof SymfonyStyle) {
98  $output->newLine();
99  $output->error($error->getMessage());
100 
101  return;
102  }
103 
104  parent::writeError($output, $error);
105  }
106 }