SymfonyQuestionHelper.php 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\Output\OutputInterface;
  12. use Symfony\Component\Console\Question\ChoiceQuestion;
  13. use Symfony\Component\Console\Question\ConfirmationQuestion;
  14. use Symfony\Component\Console\Question\Question;
  15. use Symfony\Component\Console\Style\SymfonyStyle;
  16. use Symfony\Component\Console\Formatter\OutputFormatter;
  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. protected function writePrompt(OutputInterface $output, Question $question)
  28. {
  29. $text = OutputFormatter::escapeTrailingBackslash($question->getQuestion());
  30. $default = $question->getDefault();
  31. switch (true) {
  32. case null === $default:
  33. $text = sprintf(' <info>%s</info>:', $text);
  34. break;
  35. case $question instanceof ConfirmationQuestion:
  36. $text = sprintf(' <info>%s (yes/no)</info> [<comment>%s</comment>]:', $text, $default ? 'yes' : 'no');
  37. break;
  38. case $question instanceof ChoiceQuestion && $question->isMultiselect():
  39. $choices = $question->getChoices();
  40. $default = explode(',', $default);
  41. foreach ($default as $key => $value) {
  42. $default[$key] = $choices[trim($value)];
  43. }
  44. $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(implode(', ', $default)));
  45. break;
  46. case $question instanceof ChoiceQuestion:
  47. $choices = $question->getChoices();
  48. $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($choices[$default]));
  49. break;
  50. default:
  51. $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($default));
  52. }
  53. $output->writeln($text);
  54. if ($question instanceof ChoiceQuestion) {
  55. $width = max(array_map('strlen', array_keys($question->getChoices())));
  56. foreach ($question->getChoices() as $key => $value) {
  57. $output->writeln(sprintf(" [<comment>%-${width}s</comment>] %s", $key, $value));
  58. }
  59. }
  60. $output->write(' > ');
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function writeError(OutputInterface $output, \Exception $error)
  66. {
  67. if ($output instanceof SymfonyStyle) {
  68. $output->newLine();
  69. $output->error($error->getMessage());
  70. return;
  71. }
  72. parent::writeError($output, $error);
  73. }
  74. }