ProcessorFacade.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Console\Command\ConfigSet;
  7. use Magento\Config\Console\Command\ConfigSetCommand;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Framework\App\Scope\ValidatorInterface;
  10. use Magento\Config\Model\Config\PathValidator;
  11. use Magento\Framework\Config\File\ConfigFilePool;
  12. use Magento\Framework\Exception\LocalizedException;
  13. use Magento\Framework\Exception\ConfigurationMismatchException;
  14. use Magento\Framework\Exception\CouldNotSaveException;
  15. use Magento\Framework\Exception\ValidatorException;
  16. use Magento\Deploy\Model\DeploymentConfig\Hash;
  17. use Magento\Config\App\Config\Type\System;
  18. use Magento\Framework\App\Config;
  19. /**
  20. * Processor facade for config:set command.
  21. *
  22. * @see ConfigSetCommand
  23. *
  24. * @api
  25. * @since 101.0.0
  26. */
  27. class ProcessorFacade
  28. {
  29. /**
  30. * The scope and scope code validator.
  31. *
  32. * Checks if scope and scope code exist, and scope code belongs to scope.
  33. * For example, scope "websites" and scope code "base" exist, and scope code "base" belongs to scope "website".
  34. *
  35. * @var ValidatorInterface
  36. */
  37. private $scopeValidator;
  38. /**
  39. * The path validator.
  40. *
  41. * Checks whether the config path present in configuration structure.
  42. *
  43. * @var PathValidator
  44. */
  45. private $pathValidator;
  46. /**
  47. * The factory for config:set processors.
  48. *
  49. * @var ConfigSetProcessorFactory
  50. */
  51. private $configSetProcessorFactory;
  52. /**
  53. * The hash manager.
  54. *
  55. * @var Hash
  56. */
  57. private $hash;
  58. /**
  59. * The application config storage.
  60. *
  61. * @var ScopeConfigInterface
  62. */
  63. private $scopeConfig;
  64. /**
  65. * @param ValidatorInterface $scopeValidator The scope validator
  66. * @param PathValidator $pathValidator The path validator
  67. * @param ConfigSetProcessorFactory $configSetProcessorFactory The factory for config:set processors
  68. * @param Hash $hash The hash manager
  69. * @param ScopeConfigInterface $scopeConfig The application config storage
  70. */
  71. public function __construct(
  72. ValidatorInterface $scopeValidator,
  73. PathValidator $pathValidator,
  74. ConfigSetProcessorFactory $configSetProcessorFactory,
  75. Hash $hash,
  76. ScopeConfigInterface $scopeConfig
  77. ) {
  78. $this->scopeValidator = $scopeValidator;
  79. $this->pathValidator = $pathValidator;
  80. $this->configSetProcessorFactory = $configSetProcessorFactory;
  81. $this->hash = $hash;
  82. $this->scopeConfig = $scopeConfig;
  83. }
  84. /**
  85. * Processes config:set command.
  86. *
  87. * @param string $path The configuration path in format section/group/field_name
  88. * @param string $value The configuration value
  89. * @param string $scope The configuration scope (default, website, or store)
  90. * @param string $scopeCode The scope code
  91. * @param boolean $lock The lock flag
  92. * @return string Processor response message
  93. * @throws ValidatorException If some validation is wrong
  94. * @since 101.0.0
  95. * @deprecated 101.0.4
  96. * @see processWithLockTarget()
  97. */
  98. public function process($path, $value, $scope, $scopeCode, $lock)
  99. {
  100. return $this->processWithLockTarget($path, $value, $scope, $scopeCode, $lock);
  101. }
  102. /**
  103. * Processes config:set command with the option to set a target file.
  104. *
  105. * @param string $path The configuration path in format section/group/field_name
  106. * @param string $value The configuration value
  107. * @param string $scope The configuration scope (default, website, or store)
  108. * @param string $scopeCode The scope code
  109. * @param boolean $lock The lock flag
  110. * @param string $lockTarget
  111. * @return string Processor response message
  112. * @throws ValidatorException If some validation is wrong
  113. * @since 101.0.4
  114. */
  115. public function processWithLockTarget(
  116. $path,
  117. $value,
  118. $scope,
  119. $scopeCode,
  120. $lock,
  121. $lockTarget = ConfigFilePool::APP_ENV
  122. ) {
  123. try {
  124. $this->scopeValidator->isValid($scope, $scopeCode);
  125. $this->pathValidator->validate($path);
  126. } catch (LocalizedException $exception) {
  127. throw new ValidatorException(__($exception->getMessage()), $exception);
  128. }
  129. $processor =
  130. $lock
  131. ? ( $lockTarget == ConfigFilePool::APP_ENV
  132. ? $this->configSetProcessorFactory->create(ConfigSetProcessorFactory::TYPE_LOCK_ENV)
  133. : $this->configSetProcessorFactory->create(ConfigSetProcessorFactory::TYPE_LOCK_CONFIG)
  134. )
  135. : $this->configSetProcessorFactory->create(ConfigSetProcessorFactory::TYPE_DEFAULT)
  136. ;
  137. $message =
  138. $lock
  139. ? ( $lockTarget == ConfigFilePool::APP_ENV
  140. ? 'Value was saved in app/etc/env.php and locked.'
  141. : 'Value was saved in app/etc/config.php and locked.'
  142. )
  143. : 'Value was saved.';
  144. // The processing flow depends on --lock and --share options.
  145. $processor->process($path, $value, $scope, $scopeCode);
  146. $this->hash->regenerate(System::CONFIG_TYPE);
  147. if ($this->scopeConfig instanceof Config) {
  148. $this->scopeConfig->clean();
  149. }
  150. return $message;
  151. }
  152. }