SymfonyQuestionHelper.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Helper;
  11. use Symfony\Component\Console\Formatter\OutputFormatter;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Symfony\Component\Console\Question\ChoiceQuestion;
  14. use Symfony\Component\Console\Question\ConfirmationQuestion;
  15. use Symfony\Component\Console\Question\Question;
  16. use Symfony\Component\Console\Style\SymfonyStyle;
  17. /**
  18. * Symfony Style Guide compliant question helper.
  19. *
  20. * @author Kevin Bond <kevinbond@gmail.com>
  21. */
  22. class SymfonyQuestionHelper extends QuestionHelper
  23. {
  24. /**
  25. * @return void
  26. * @param \Symfony\Component\Console\Output\OutputInterface $output
  27. * @param \Symfony\Component\Console\Question\Question $question
  28. */
  29. protected function writePrompt($output, $question)
  30. {
  31. $text = OutputFormatter::escapeTrailingBackslash($question->getQuestion());
  32. $default = $question->getDefault();
  33. if ($question->isMultiline()) {
  34. $text .= sprintf(' (press %s to continue)', $this->getEofShortcut());
  35. }
  36. switch (true) {
  37. case null === $default:
  38. $text = sprintf(' <info>%s</info>:', $text);
  39. break;
  40. case $question instanceof ConfirmationQuestion:
  41. $text = sprintf(' <info>%s (yes/no)</info> [<comment>%s</comment>]:', $text, $default ? 'yes' : 'no');
  42. break;
  43. case $question instanceof ChoiceQuestion && $question->isMultiselect():
  44. $choices = $question->getChoices();
  45. $default = explode(',', $default);
  46. foreach ($default as $key => $value) {
  47. $default[$key] = $choices[trim($value)];
  48. }
  49. $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(implode(', ', $default)));
  50. break;
  51. case $question instanceof ChoiceQuestion:
  52. $choices = $question->getChoices();
  53. $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($choices[$default] ?? $default));
  54. break;
  55. default:
  56. $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($default));
  57. }
  58. $output->writeln($text);
  59. $prompt = ' > ';
  60. if ($question instanceof ChoiceQuestion) {
  61. $output->writeln($this->formatChoiceQuestionChoices($question, 'comment'));
  62. $prompt = $question->getPrompt();
  63. }
  64. $output->write($prompt);
  65. }
  66. /**
  67. * @return void
  68. * @param \Symfony\Component\Console\Output\OutputInterface $output
  69. * @param \Exception $error
  70. */
  71. protected function writeError($output, $error)
  72. {
  73. if ($output instanceof SymfonyStyle) {
  74. $output->newLine();
  75. $output->error($error->getMessage());
  76. return;
  77. }
  78. parent::writeError($output, $error);
  79. }
  80. private function getEofShortcut()
  81. {
  82. if ('Windows' === \PHP_OS_FAMILY) {
  83. return '<comment>Ctrl+Z</comment> then <comment>Enter</comment>';
  84. }
  85. return '<comment>Ctrl+D</comment>';
  86. }
  87. }