SymfonyQuestionHelper.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Exception\LogicException;
  12. use Symfony\Component\Console\Formatter\OutputFormatter;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Question\ChoiceQuestion;
  16. use Symfony\Component\Console\Question\ConfirmationQuestion;
  17. use Symfony\Component\Console\Question\Question;
  18. use Symfony\Component\Console\Style\SymfonyStyle;
  19. /**
  20. * Symfony Style Guide compliant question helper.
  21. *
  22. * @author Kevin Bond <kevinbond@gmail.com>
  23. */
  24. class SymfonyQuestionHelper extends QuestionHelper
  25. {
  26. /**
  27. * {@inheritdoc}
  28. *
  29. * To be removed in 4.0
  30. */
  31. public function ask(InputInterface $input, OutputInterface $output, Question $question)
  32. {
  33. $validator = $question->getValidator();
  34. $question->setValidator(function ($value) use ($validator) {
  35. if (null !== $validator) {
  36. $value = $validator($value);
  37. } else {
  38. // make required
  39. if (!\is_array($value) && !\is_bool($value) && 0 === \strlen($value)) {
  40. @trigger_error('The default question validator is deprecated since Symfony 3.3 and will not be used anymore in version 4.0. Set a custom question validator if needed.', E_USER_DEPRECATED);
  41. throw new LogicException('A value is required.');
  42. }
  43. }
  44. return $value;
  45. });
  46. return parent::ask($input, $output, $question);
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. protected function writePrompt(OutputInterface $output, Question $question)
  52. {
  53. $text = OutputFormatter::escapeTrailingBackslash($question->getQuestion());
  54. $default = $question->getDefault();
  55. switch (true) {
  56. case null === $default:
  57. $text = sprintf(' <info>%s</info>:', $text);
  58. break;
  59. case $question instanceof ConfirmationQuestion:
  60. $text = sprintf(' <info>%s (yes/no)</info> [<comment>%s</comment>]:', $text, $default ? 'yes' : 'no');
  61. break;
  62. case $question instanceof ChoiceQuestion && $question->isMultiselect():
  63. $choices = $question->getChoices();
  64. $default = explode(',', $default);
  65. foreach ($default as $key => $value) {
  66. $default[$key] = $choices[trim($value)];
  67. }
  68. $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(implode(', ', $default)));
  69. break;
  70. case $question instanceof ChoiceQuestion:
  71. $choices = $question->getChoices();
  72. $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($choices[$default]));
  73. break;
  74. default:
  75. $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($default));
  76. }
  77. $output->writeln($text);
  78. if ($question instanceof ChoiceQuestion) {
  79. $width = max(array_map('strlen', array_keys($question->getChoices())));
  80. foreach ($question->getChoices() as $key => $value) {
  81. $output->writeln(sprintf(" [<comment>%-${width}s</comment>] %s", $key, $value));
  82. }
  83. }
  84. $output->write(' > ');
  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. return;
  95. }
  96. parent::writeError($output, $error);
  97. }
  98. }