LockProcessor.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\Model\PreparedValueFactory;
  9. use Magento\Framework\App\Config\ConfigPathResolver;
  10. use Magento\Framework\App\Config\Value;
  11. use Magento\Framework\App\DeploymentConfig;
  12. use Magento\Framework\Config\File\ConfigFilePool;
  13. use Magento\Framework\Exception\CouldNotSaveException;
  14. use Magento\Framework\Stdlib\ArrayManager;
  15. /**
  16. * Processes file lock flow of config:set command.
  17. * This processor saves the value of configuration into app/etc/env.php
  18. * and locks it for editing in Admin interface.
  19. *
  20. * {@inheritdoc}
  21. */
  22. class LockProcessor implements ConfigSetProcessorInterface
  23. {
  24. /**
  25. * The factory for prepared value
  26. *
  27. * @var PreparedValueFactory
  28. */
  29. private $preparedValueFactory;
  30. /**
  31. * The deployment configuration writer
  32. *
  33. * @var DeploymentConfig\Writer
  34. */
  35. private $deploymentConfigWriter;
  36. /**
  37. * An array manager for different manipulations with arrays
  38. *
  39. * @var ArrayManager
  40. */
  41. private $arrayManager;
  42. /**
  43. * The resolver for configuration paths according to source type
  44. *
  45. * @var ConfigPathResolver
  46. */
  47. private $configPathResolver;
  48. /**
  49. * @var string
  50. */
  51. private $target;
  52. /**
  53. * @param PreparedValueFactory $preparedValueFactory The factory for prepared value
  54. * @param DeploymentConfig\Writer $writer The deployment configuration writer
  55. * @param ArrayManager $arrayManager An array manager for different manipulations with arrays
  56. * @param ConfigPathResolver $configPathResolver The resolver for configuration paths according to source type
  57. * @param string $target
  58. */
  59. public function __construct(
  60. PreparedValueFactory $preparedValueFactory,
  61. DeploymentConfig\Writer $writer,
  62. ArrayManager $arrayManager,
  63. ConfigPathResolver $configPathResolver,
  64. $target = ConfigFilePool::APP_ENV
  65. ) {
  66. $this->preparedValueFactory = $preparedValueFactory;
  67. $this->deploymentConfigWriter = $writer;
  68. $this->arrayManager = $arrayManager;
  69. $this->configPathResolver = $configPathResolver;
  70. $this->target = $target;
  71. }
  72. /**
  73. * Processes lock flow of config:set command.
  74. * Requires read access to filesystem.
  75. *
  76. * {@inheritdoc}
  77. */
  78. public function process($path, $value, $scope, $scopeCode)
  79. {
  80. try {
  81. $configPath = $this->configPathResolver->resolve($path, $scope, $scopeCode, System::CONFIG_TYPE);
  82. $backendModel = $this->preparedValueFactory->create($path, $value, $scope, $scopeCode);
  83. if ($backendModel instanceof Value) {
  84. /**
  85. * Temporary solution until Magento introduce unified interface
  86. * for storing system configuration into database and configuration files.
  87. */
  88. $backendModel->validateBeforeSave();
  89. $backendModel->beforeSave();
  90. $value = $backendModel->getValue();
  91. $backendModel->afterSave();
  92. /**
  93. * Because FS does not support transactions,
  94. * we'll write value just after all validations are triggered.
  95. */
  96. $this->deploymentConfigWriter->saveConfig(
  97. [$this->target => $this->arrayManager->set($configPath, [], $value)],
  98. false
  99. );
  100. }
  101. } catch (\Exception $exception) {
  102. throw new CouldNotSaveException(__('%1', $exception->getMessage()), $exception);
  103. }
  104. }
  105. }