SymfonyQuestionHelper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 && $question->isMultiselect():
  58. $choices = $question->getChoices();
  59. $default = explode(',', $default);
  60. foreach ($default as $key => $value) {
  61. $default[$key] = $choices[trim($value)];
  62. }
  63. $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, implode(', ', $default));
  64. break;
  65. case $question instanceof ChoiceQuestion:
  66. $choices = $question->getChoices();
  67. $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $choices[$default]);
  68. break;
  69. default:
  70. $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $default);
  71. }
  72. $output->writeln($text);
  73. if ($question instanceof ChoiceQuestion) {
  74. $width = max(array_map('strlen', array_keys($question->getChoices())));
  75. foreach ($question->getChoices() as $key => $value) {
  76. $output->writeln(sprintf(" [<comment>%-${width}s</comment>] %s", $key, $value));
  77. }
  78. }
  79. $output->write(' > ');
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. protected function writeError(OutputInterface $output, \Exception $error)
  85. {
  86. if ($output instanceof SymfonyStyle) {
  87. $output->newLine();
  88. $output->error($error->getMessage());
  89. return;
  90. }
  91. parent::writeError($output, $error);
  92. }
  93. }