SymfonyQuestionHelper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. protected function writePrompt(OutputInterface $output, Question $question): void
  25. {
  26. $text = OutputFormatter::escapeTrailingBackslash($question->getQuestion());
  27. $default = $question->getDefault();
  28. if ($question->isMultiline()) {
  29. $text .= \sprintf(' (press %s to continue)', $this->getEofShortcut());
  30. }
  31. switch (true) {
  32. case null === $default:
  33. $text = \sprintf(' <info>%s</info>:', $text);
  34. break;
  35. case $question instanceof ConfirmationQuestion:
  36. $text = \sprintf(' <info>%s (yes/no)</info> [<comment>%s</comment>]:', $text, $default ? 'yes' : 'no');
  37. break;
  38. case $question instanceof ChoiceQuestion && $question->isMultiselect():
  39. $choices = $question->getChoices();
  40. $default = explode(',', $default);
  41. foreach ($default as $key => $value) {
  42. $default[$key] = $choices[trim($value)];
  43. }
  44. $text = \sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(implode(', ', $default)));
  45. break;
  46. case $question instanceof ChoiceQuestion:
  47. $choices = $question->getChoices();
  48. $text = \sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($choices[$default] ?? $default));
  49. break;
  50. default:
  51. $text = \sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($default));
  52. }
  53. $output->writeln($text);
  54. $prompt = ' > ';
  55. if ($question instanceof ChoiceQuestion) {
  56. $output->writeln($this->formatChoiceQuestionChoices($question, 'comment'));
  57. $prompt = $question->getPrompt();
  58. }
  59. $output->write($prompt);
  60. }
  61. protected function writeError(OutputInterface $output, \Exception $error): void
  62. {
  63. if ($output instanceof SymfonyStyle) {
  64. $output->newLine();
  65. $output->error($error->getMessage());
  66. return;
  67. }
  68. parent::writeError($output, $error);
  69. }
  70. private function getEofShortcut(): string
  71. {
  72. if ('Windows' === \PHP_OS_FAMILY) {
  73. return '<comment>Ctrl+Z</comment> then <comment>Enter</comment>';
  74. }
  75. return '<comment>Ctrl+D</comment>';
  76. }
  77. }