QuestionableCommand.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\AdminBundle\Command;
  11. use Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper;
  12. use Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper;
  13. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Question\ConfirmationQuestion;
  17. use Symfony\Component\Console\Question\Question;
  18. abstract class QuestionableCommand extends ContainerAwareCommand
  19. {
  20. /**
  21. * @param InputInterface $input
  22. * @param OutputInterface $output
  23. * @param string $questionText
  24. * @param mixed $default
  25. * @param callable $validator
  26. *
  27. * @return mixed
  28. */
  29. final protected function askAndValidate(InputInterface $input, OutputInterface $output, $questionText, $default, $validator)
  30. {
  31. $questionHelper = $this->getQuestionHelper();
  32. // NEXT_MAJOR: Remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3
  33. if ($questionHelper instanceof DialogHelper) {
  34. return $questionHelper->askAndValidate(
  35. $output,
  36. $questionHelper->getQuestion($questionText, $default),
  37. $validator,
  38. false,
  39. $default
  40. );
  41. }
  42. $question = new Question($questionHelper->getQuestion($questionText, $default), $default);
  43. $question->setValidator($validator);
  44. return $questionHelper->ask($input, $output, $question);
  45. }
  46. /**
  47. * @param InputInterface $input
  48. * @param OutputInterface $output
  49. * @param string $questionText
  50. * @param string $default
  51. * @param string $separator
  52. *
  53. * @return string
  54. */
  55. final protected function askConfirmation(InputInterface $input, OutputInterface $output, $questionText, $default, $separator)
  56. {
  57. $questionHelper = $this->getQuestionHelper();
  58. // NEXT_MAJOR: Remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3
  59. if ($questionHelper instanceof DialogHelper) {
  60. $question = $questionHelper->getQuestion($questionText, $default, $separator);
  61. return $questionHelper->askConfirmation($output, $question, ($default === 'no' ? false : true));
  62. }
  63. $question = new ConfirmationQuestion($questionHelper->getQuestion(
  64. $questionText,
  65. $default,
  66. $separator
  67. ), ($default === 'no' ? false : true));
  68. return $questionHelper->ask($input, $output, $question);
  69. }
  70. /**
  71. * @return QuestionHelper|DialogHelper
  72. */
  73. final protected function getQuestionHelper()
  74. {
  75. // NEXT_MAJOR: Remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3
  76. if (class_exists('Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper')) {
  77. $questionHelper = $this->getHelper('dialog');
  78. if (!$questionHelper instanceof DialogHelper) {
  79. $questionHelper = new DialogHelper();
  80. $this->getHelperSet()->set($questionHelper);
  81. }
  82. } else {
  83. $questionHelper = $this->getHelper('question');
  84. if (!$questionHelper instanceof QuestionHelper) {
  85. $questionHelper = new QuestionHelper();
  86. $this->getHelperSet()->set($questionHelper);
  87. }
  88. }
  89. return $questionHelper;
  90. }
  91. }