YesNo.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Console\QuestionPerformer;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Framework\Phrase;
  9. use Symfony\Component\Console\Helper\QuestionHelper;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. use Symfony\Component\Console\Question\Question;
  13. use Symfony\Component\Console\Question\QuestionFactory;
  14. /**
  15. * Asks a questions to the user.
  16. */
  17. class YesNo
  18. {
  19. /**
  20. * Provides helpers to interact with the user.
  21. *
  22. * @var QuestionHelper
  23. */
  24. private $questionHelper;
  25. /**
  26. * The factory for creating Question objects.
  27. *
  28. * @var QuestionFactory
  29. */
  30. private $questionFactory;
  31. /**
  32. * @param QuestionHelper $questionHelper Provides helpers to interact with the user
  33. * @param QuestionFactory $questionFactory The factory for creating Question objects
  34. */
  35. public function __construct(
  36. QuestionHelper $questionHelper,
  37. QuestionFactory $questionFactory
  38. ) {
  39. $this->questionHelper = $questionHelper;
  40. $this->questionFactory = $questionFactory;
  41. }
  42. /**
  43. * Asks a question to the user. The question is generates from given array of messages.
  44. *
  45. * @param string[] $messages The array of messages for creating a question
  46. * @param InputInterface $input An InputInterface instance
  47. * @param OutputInterface $output An OutputInterface instance
  48. * @return bool
  49. */
  50. public function execute(array $messages, InputInterface $input, OutputInterface $output)
  51. {
  52. if (!$input->isInteractive()) {
  53. return true;
  54. }
  55. $question = $this->getConfirmationQuestion($messages);
  56. $answer = $this->questionHelper->ask($input, $output, $question);
  57. return in_array(strtolower($answer), ['yes', 'y']);
  58. }
  59. /**
  60. * Creates Question object from given array of messages.
  61. *
  62. * @param string[] $messages array of messages
  63. * @return Question
  64. * @throws LocalizedException is thrown when a user entered a wrong answer
  65. */
  66. private function getConfirmationQuestion(array $messages)
  67. {
  68. /** @var Question $question */
  69. $question = $this->questionFactory->create([
  70. 'question' => implode(PHP_EOL, $messages) . PHP_EOL
  71. ]);
  72. $question->setValidator(function ($answer) {
  73. if (!in_array(strtolower($answer), ['yes', 'y', 'no', 'n'])) {
  74. throw new LocalizedException(
  75. new Phrase('A [y]es or [n]o selection needs to be made. Select and try again.')
  76. );
  77. }
  78. return $answer;
  79. });
  80. return $question;
  81. }
  82. }