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
12namespace Symfony\Component\Console\Helper;
13
14use Symfony\Component\Console\Formatter\OutputFormatter;
15use Symfony\Component\Console\Output\OutputInterface;
16use Symfony\Component\Console\Question\ChoiceQuestion;
17use Symfony\Component\Console\Question\ConfirmationQuestion;
18use Symfony\Component\Console\Question\Question;
19use Symfony\Component\Console\Style\SymfonyStyle;
20
21/**
22 * Symfony Style Guide compliant question helper.
23 *
24 * @author Kevin Bond <kevinbond@gmail.com>
25 */
26class SymfonyQuestionHelper extends QuestionHelper
27{
28    /**
29     * {@inheritdoc}
30     */
31    protected function writePrompt(OutputInterface $output, Question $question)
32    {
33        $text = OutputFormatter::escapeTrailingBackslash($question->getQuestion());
34        $default = $question->getDefault();
35
36        switch (true) {
37            case null === $default:
38                $text = sprintf(' <info>%s</info>:', $text);
39
40                break;
41
42            case $question instanceof ConfirmationQuestion:
43                $text = sprintf(' <info>%s (yes/no)</info> [<comment>%s</comment>]:', $text, $default ? 'yes' : 'no');
44
45                break;
46
47            case $question instanceof ChoiceQuestion && $question->isMultiselect():
48                $choices = $question->getChoices();
49                $default = explode(',', $default);
50
51                foreach ($default as $key => $value) {
52                    $default[$key] = $choices[trim($value)];
53                }
54
55                $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(implode(', ', $default)));
56
57                break;
58
59            case $question instanceof ChoiceQuestion:
60                $choices = $question->getChoices();
61                $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(isset($choices[$default]) ? $choices[$default] : $default));
62
63                break;
64
65            default:
66                $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($default));
67        }
68
69        $output->writeln($text);
70
71        $prompt = ' > ';
72
73        if ($question instanceof ChoiceQuestion) {
74            $output->writeln($this->formatChoiceQuestionChoices($question, 'comment'));
75
76            $prompt = $question->getPrompt();
77        }
78
79        $output->write($prompt);
80    }
81
82    /**
83     * {@inheritdoc}
84     */
85    protected function writeError(OutputInterface $output, \Exception $error)
86    {
87        if ($output instanceof SymfonyStyle) {
88            $output->newLine();
89            $output->error($error->getMessage());
90
91            return;
92        }
93
94        parent::writeError($output, $error);
95    }
96}
97