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 if ($question->isMultiline()) { 37 $text .= sprintf(' (press %s to continue)', $this->getEofShortcut()); 38 } 39 40 switch (true) { 41 case null === $default: 42 $text = sprintf(' <info>%s</info>:', $text); 43 44 break; 45 46 case $question instanceof ConfirmationQuestion: 47 $text = sprintf(' <info>%s (yes/no)</info> [<comment>%s</comment>]:', $text, $default ? 'yes' : 'no'); 48 49 break; 50 51 case $question instanceof ChoiceQuestion && $question->isMultiselect(): 52 $choices = $question->getChoices(); 53 $default = explode(',', $default); 54 55 foreach ($default as $key => $value) { 56 $default[$key] = $choices[trim($value)]; 57 } 58 59 $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(implode(', ', $default))); 60 61 break; 62 63 case $question instanceof ChoiceQuestion: 64 $choices = $question->getChoices(); 65 $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(isset($choices[$default]) ? $choices[$default] : $default)); 66 67 break; 68 69 default: 70 $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($default)); 71 } 72 73 $output->writeln($text); 74 75 $prompt = ' > '; 76 77 if ($question instanceof ChoiceQuestion) { 78 $output->writeln($this->formatChoiceQuestionChoices($question, 'comment')); 79 80 $prompt = $question->getPrompt(); 81 } 82 83 $output->write($prompt); 84 } 85 86 /** 87 * {@inheritdoc} 88 */ 89 protected function writeError(OutputInterface $output, \Exception $error) 90 { 91 if ($output instanceof SymfonyStyle) { 92 $output->newLine(); 93 $output->error($error->getMessage()); 94 95 return; 96 } 97 98 parent::writeError($output, $error); 99 } 100 101 private function getEofShortcut(): string 102 { 103 if (false !== strpos(\PHP_OS, 'WIN')) { 104 return '<comment>Ctrl+Z</comment> then <comment>Enter</comment>'; 105 } 106 107 return '<comment>Ctrl+D</comment>'; 108 } 109} 110