SymfonyQuestionHelper.php 3.7 KB

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