DefaultProcessor.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\App\Config\Type\System;
  8. use Magento\Config\Console\Command\ConfigSetCommand;
  9. use Magento\Config\Model\Config\Factory as ConfigFactory;
  10. use Magento\Framework\App\Config\ConfigPathResolver;
  11. use Magento\Framework\App\DeploymentConfig;
  12. use Magento\Framework\App\ObjectManager;
  13. use Magento\Framework\Exception\CouldNotSaveException;
  14. use Magento\Config\Model\PreparedValueFactory;
  15. /**
  16. * Processes default flow of config:set command.
  17. *
  18. * This processor saves the value of configuration into database.
  19. *
  20. * @inheritdoc
  21. * @api
  22. * @since 101.0.0
  23. */
  24. class DefaultProcessor implements ConfigSetProcessorInterface
  25. {
  26. /**
  27. * The deployment configuration reader.
  28. *
  29. * @var DeploymentConfig
  30. */
  31. private $deploymentConfig;
  32. /**
  33. * The resolver for configuration paths according to source type.
  34. *
  35. * @var ConfigPathResolver
  36. */
  37. private $configPathResolver;
  38. /**
  39. * The factory for prepared value.
  40. *
  41. * @var PreparedValueFactory
  42. */
  43. private $preparedValueFactory;
  44. /**
  45. * @var ConfigFactory
  46. */
  47. private $configFactory;
  48. /**
  49. * @param PreparedValueFactory $preparedValueFactory The factory for prepared value
  50. * @param DeploymentConfig $deploymentConfig The deployment configuration reader
  51. * @param ConfigPathResolver $configPathResolver The resolver for configuration paths according to source type
  52. * @param ConfigFactory|null $configFactory
  53. */
  54. public function __construct(
  55. PreparedValueFactory $preparedValueFactory,
  56. DeploymentConfig $deploymentConfig,
  57. ConfigPathResolver $configPathResolver,
  58. ConfigFactory $configFactory = null
  59. ) {
  60. $this->preparedValueFactory = $preparedValueFactory;
  61. $this->deploymentConfig = $deploymentConfig;
  62. $this->configPathResolver = $configPathResolver;
  63. $this->configFactory = $configFactory ?? ObjectManager::getInstance()->get(ConfigFactory::class);
  64. }
  65. /**
  66. * Processes database flow of config:set command.
  67. *
  68. * Requires installed application.
  69. *
  70. * @inheritdoc
  71. * @since 101.0.0
  72. */
  73. public function process($path, $value, $scope, $scopeCode)
  74. {
  75. if ($this->isLocked($path, $scope, $scopeCode)) {
  76. throw new CouldNotSaveException(
  77. __(
  78. 'The value you set has already been locked. To change the value, use the --%1 option.',
  79. ConfigSetCommand::OPTION_LOCK_ENV
  80. )
  81. );
  82. }
  83. try {
  84. $config = $this->configFactory->create([
  85. 'scope' => $scope,
  86. 'scope_code' => $scopeCode,
  87. ]);
  88. $config->setDataByPath($path, $value);
  89. $config->save();
  90. } catch (\Exception $exception) {
  91. throw new CouldNotSaveException(__('%1', $exception->getMessage()), $exception);
  92. }
  93. }
  94. /**
  95. * Checks whether configuration is locked in file storage.
  96. *
  97. * @param string $path The path to configuration
  98. * @param string $scope The scope of configuration
  99. * @param string $scopeCode The scope code of configuration
  100. * @return bool
  101. */
  102. private function isLocked($path, $scope, $scopeCode)
  103. {
  104. $scopePath = $this->configPathResolver->resolve($path, $scope, $scopeCode, System::CONFIG_TYPE);
  105. return $this->deploymentConfig->get($scopePath) !== null;
  106. }
  107. }