SensitiveConfigSetFacade.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\FileSystemException;
  9. use Magento\Framework\Exception\RuntimeException;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. use Magento\Deploy\Model\ConfigWriter;
  13. use Magento\Framework\App\Config\CommentParserInterface;
  14. use Magento\Framework\App\Scope\ValidatorInterface;
  15. use Magento\Framework\Config\File\ConfigFilePool;
  16. use Magento\Framework\Exception\LocalizedException;
  17. /**
  18. * Processes the sensitive:config:set command.
  19. */
  20. class SensitiveConfigSetFacade
  21. {
  22. /**
  23. * The parser for config comments.
  24. *
  25. * @var CommentParserInterface
  26. */
  27. private $commentParser;
  28. /**
  29. * The pool of configs.
  30. *
  31. * @var ConfigFilePool
  32. */
  33. private $configFilePool;
  34. /**
  35. * The config writer.
  36. *
  37. * @var ConfigWriter
  38. */
  39. private $configWriter;
  40. /**
  41. * The validator for scopes.
  42. *
  43. * @var ValidatorInterface
  44. */
  45. private $scopeValidator;
  46. /**
  47. * The factory of config collectors.
  48. *
  49. * @var CollectorFactory
  50. */
  51. private $collectorFactory;
  52. /**
  53. * @param ConfigFilePool $configFilePool The pool of configs
  54. * @param CommentParserInterface $commentParser The parser for config comments
  55. * @param ConfigWriter $configWriter The config writer
  56. * @param ValidatorInterface $scopeValidator The validator for scopes
  57. * @param CollectorFactory $collectorFactory The factory of config collectors
  58. */
  59. public function __construct(
  60. ConfigFilePool $configFilePool,
  61. CommentParserInterface $commentParser,
  62. ConfigWriter $configWriter,
  63. ValidatorInterface $scopeValidator,
  64. CollectorFactory $collectorFactory
  65. ) {
  66. $this->commentParser = $commentParser;
  67. $this->configFilePool = $configFilePool;
  68. $this->configWriter = $configWriter;
  69. $this->scopeValidator = $scopeValidator;
  70. $this->collectorFactory = $collectorFactory;
  71. }
  72. /**
  73. * Processes the config:sensitive:set command.
  74. *
  75. * @param InputInterface $input The input manager
  76. * @param OutputInterface $output The output manager
  77. * @return void
  78. * @throws LocalizedException If scope or scope code is not valid
  79. * @throws RuntimeException If sensitive config can not be filled
  80. */
  81. public function process(InputInterface $input, OutputInterface $output)
  82. {
  83. $scope = $input->getOption(SensitiveConfigSetCommand::INPUT_OPTION_SCOPE);
  84. $scopeCode = $input->getOption(SensitiveConfigSetCommand::INPUT_OPTION_SCOPE_CODE);
  85. $isInteractive = $input->getOption(SensitiveConfigSetCommand::INPUT_OPTION_INTERACTIVE);
  86. $this->scopeValidator->isValid($scope, $scopeCode);
  87. $configPaths = $this->getConfigPaths();
  88. $collector = $this->collectorFactory->create(
  89. $isInteractive ? CollectorFactory::TYPE_INTERACTIVE : CollectorFactory::TYPE_SIMPLE
  90. );
  91. $values = $collector->getValues($input, $output, $configPaths);
  92. $this->configWriter->save($values, $scope, $scopeCode);
  93. $output->writeln(sprintf(
  94. '<info>Configuration value%s saved in app/etc/%s</info>',
  95. $isInteractive ? 's' : '',
  96. $this->configFilePool->getPath(ConfigFilePool::APP_ENV)
  97. ));
  98. }
  99. /**
  100. * Get sensitive configuration paths.
  101. *
  102. * @return array
  103. * @throws LocalizedException if configuration file not exists or sensitive configuration is empty
  104. */
  105. private function getConfigPaths()
  106. {
  107. $configFilePath = $this->configFilePool->getPath(ConfigFilePool::APP_CONFIG);
  108. try {
  109. $configPaths = $this->commentParser->execute($configFilePath);
  110. } catch (FileSystemException $e) {
  111. throw new RuntimeException(__(
  112. 'File app/etc/%1 can\'t be read. Please check if it exists and has read permissions.',
  113. [
  114. $configFilePath
  115. ]
  116. ));
  117. }
  118. if (empty($configPaths)) {
  119. throw new RuntimeException(__('There are no sensitive configurations to fill'));
  120. }
  121. return $configPaths;
  122. }
  123. }