SymfonyQuestionHelper.php 3.0 KB

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