You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.2 KiB

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