SimpleCollector.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Console\Command\App\SensitiveConfigSet;
  7. use Magento\Deploy\Console\Command\App\SensitiveConfigSetCommand;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\Phrase;
  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. use Symfony\Component\Console\Helper\QuestionHelper;
  15. /**
  16. * Class SimpleCollector collects configuration value from user input
  17. */
  18. class SimpleCollector implements CollectorInterface
  19. {
  20. /**
  21. * @var QuestionFactory
  22. */
  23. private $questionFactory;
  24. /**
  25. * @var QuestionHelper
  26. */
  27. private $questionHelper;
  28. /**
  29. * @param QuestionFactory $questionFactory
  30. * @param QuestionHelper $questionHelper
  31. */
  32. public function __construct(
  33. QuestionFactory $questionFactory,
  34. QuestionHelper $questionHelper
  35. ) {
  36. $this->questionFactory = $questionFactory;
  37. $this->questionHelper = $questionHelper;
  38. }
  39. /**
  40. * Collects single configuration value from user input
  41. *
  42. * For example, this method will return
  43. * ```php
  44. * ['some/configuration/path' => 'someValue']
  45. * ```
  46. * {@inheritdoc}
  47. */
  48. public function getValues(InputInterface $input, OutputInterface $output, array $configPaths)
  49. {
  50. $inputPath = $input->getArgument(SensitiveConfigSetCommand::INPUT_ARGUMENT_PATH);
  51. $configPathQuestion = $this->getConfigPathQuestion($configPaths);
  52. $configPath = ($inputPath === null)
  53. ? $this->questionHelper->ask($input, $output, $configPathQuestion)
  54. : $inputPath;
  55. $this->validatePath($configPath, $configPaths);
  56. $inputValue = $input->getArgument(SensitiveConfigSetCommand::INPUT_ARGUMENT_VALUE);
  57. $configValueQuestion = $this->getConfigValueQuestion();
  58. $configValue = $inputValue === null
  59. ? $this->questionHelper->ask($input, $output, $configValueQuestion)
  60. : $inputValue;
  61. return [$configPath => $configValue];
  62. }
  63. /**
  64. * Get Question to fill configuration path with autocompletion in interactive mode
  65. *
  66. * @param array $configPaths
  67. * @return Question
  68. */
  69. private function getConfigPathQuestion(array $configPaths)
  70. {
  71. /** @var Question $configPathQuestion */
  72. $configPathQuestion = $this->questionFactory->create([
  73. 'question' => 'Please enter config path: '
  74. ]);
  75. $configPathQuestion->setAutocompleterValues($configPaths);
  76. $configPathQuestion->setValidator(function ($configPath) use ($configPaths) {
  77. $this->validatePath($configPath, $configPaths);
  78. return $configPath;
  79. });
  80. return $configPathQuestion;
  81. }
  82. /**
  83. * Get Question to fill configuration value in interactive mode
  84. *
  85. * @return Question
  86. */
  87. private function getConfigValueQuestion()
  88. {
  89. /** @var Question $configValueQuestion */
  90. $configValueQuestion = $this->questionFactory->create([
  91. 'question' => 'Please enter value: '
  92. ]);
  93. $configValueQuestion->setValidator(function ($interviewer) {
  94. if (empty($interviewer)) {
  95. throw new LocalizedException(new Phrase("The value can't be empty. Enter the value and try again."));
  96. }
  97. return $interviewer;
  98. });
  99. return $configValueQuestion;
  100. }
  101. /**
  102. * Check if entered configuration path is valid, throw LocalizedException otherwise
  103. *
  104. * @param string $configPath Path that should be validated.
  105. * @param array $configPaths List of allowed paths.
  106. * @return void
  107. * @throws LocalizedException If config path not exist in allowed config paths
  108. */
  109. private function validatePath($configPath, array $configPaths)
  110. {
  111. if (!in_array($configPath, $configPaths)) {
  112. throw new LocalizedException(
  113. new Phrase('A configuration with this path does not exist or is not sensitive')
  114. );
  115. }
  116. }
  117. }