cf071d594d44ab5c4b93a9726b20ebd9b4a87e84
[yaffs-website] / vendor / symfony / console / Helper / SymfonyQuestionHelper.php
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
14 use Symfony\Component\Console\Exception\LogicException;
15 use Symfony\Component\Console\Input\InputInterface;
16 use Symfony\Component\Console\Output\OutputInterface;
17 use Symfony\Component\Console\Question\ChoiceQuestion;
18 use Symfony\Component\Console\Question\ConfirmationQuestion;
19 use Symfony\Component\Console\Question\Question;
20 use Symfony\Component\Console\Style\SymfonyStyle;
21 use Symfony\Component\Console\Formatter\OutputFormatter;
22
23 /**
24  * Symfony Style Guide compliant question helper.
25  *
26  * @author Kevin Bond <kevinbond@gmail.com>
27  */
28 class SymfonyQuestionHelper extends QuestionHelper
29 {
30     /**
31      * {@inheritdoc}
32      *
33      * To be removed in 4.0
34      */
35     public function ask(InputInterface $input, OutputInterface $output, Question $question)
36     {
37         $validator = $question->getValidator();
38         $question->setValidator(function ($value) use ($validator) {
39             if (null !== $validator) {
40                 $value = $validator($value);
41             } else {
42                 // make required
43                 if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) {
44                     @trigger_error('The default question validator is deprecated since Symfony 3.3 and will not be used anymore in version 4.0. Set a custom question validator if needed.', E_USER_DEPRECATED);
45
46                     throw new LogicException('A value is required.');
47                 }
48             }
49
50             return $value;
51         });
52
53         return parent::ask($input, $output, $question);
54     }
55
56     /**
57      * {@inheritdoc}
58      */
59     protected function writePrompt(OutputInterface $output, Question $question)
60     {
61         $text = OutputFormatter::escapeTrailingBackslash($question->getQuestion());
62         $default = $question->getDefault();
63
64         switch (true) {
65             case null === $default:
66                 $text = sprintf(' <info>%s</info>:', $text);
67
68                 break;
69
70             case $question instanceof ConfirmationQuestion:
71                 $text = sprintf(' <info>%s (yes/no)</info> [<comment>%s</comment>]:', $text, $default ? 'yes' : 'no');
72
73                 break;
74
75             case $question instanceof ChoiceQuestion && $question->isMultiselect():
76                 $choices = $question->getChoices();
77                 $default = explode(',', $default);
78
79                 foreach ($default as $key => $value) {
80                     $default[$key] = $choices[trim($value)];
81                 }
82
83                 $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(implode(', ', $default)));
84
85                 break;
86
87             case $question instanceof ChoiceQuestion:
88                 $choices = $question->getChoices();
89                 $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($choices[$default]));
90
91                 break;
92
93             default:
94                 $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($default));
95         }
96
97         $output->writeln($text);
98
99         if ($question instanceof ChoiceQuestion) {
100             $width = max(array_map('strlen', array_keys($question->getChoices())));
101
102             foreach ($question->getChoices() as $key => $value) {
103                 $output->writeln(sprintf("  [<comment>%-${width}s</comment>] %s", $key, $value));
104             }
105         }
106
107         $output->write(' > ');
108     }
109
110     /**
111      * {@inheritdoc}
112      */
113     protected function writeError(OutputInterface $output, \Exception $error)
114     {
115         if ($output instanceof SymfonyStyle) {
116             $output->newLine();
117             $output->error($error->getMessage());
118
119             return;
120         }
121
122         parent::writeError($output, $error);
123     }
124 }