SymfonyQuestionHelper.php 3.0 KB

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