Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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