ConfigWriter.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Model;
  7. use Magento\Config\App\Config\Type\System;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Framework\App\Config\Value;
  10. use Magento\Framework\App\DeploymentConfig\Writer;
  11. use Magento\Framework\App\ObjectManager;
  12. use Magento\Framework\Config\File\ConfigFilePool;
  13. use Magento\Framework\Stdlib\ArrayManager;
  14. use Magento\Config\Model\PreparedValueFactory;
  15. /**
  16. * Class ConfigWriter. Save configuration values into config file.
  17. */
  18. class ConfigWriter
  19. {
  20. /**
  21. * @var Writer
  22. */
  23. private $writer;
  24. /**
  25. * @var ArrayManager
  26. */
  27. private $arrayManager;
  28. /**
  29. * Creates a prepared instance of Value.
  30. *
  31. * @var PreparedValueFactory
  32. */
  33. private $preparedValueFactory;
  34. /**
  35. * @param Writer $writer
  36. * @param ArrayManager $arrayManager
  37. * @param PreparedValueFactory|null $valueFactory Creates a prepared instance of Value
  38. */
  39. public function __construct(
  40. Writer $writer,
  41. ArrayManager $arrayManager,
  42. PreparedValueFactory $valueFactory = null
  43. ) {
  44. $this->writer = $writer;
  45. $this->arrayManager = $arrayManager;
  46. $this->preparedValueFactory = $valueFactory ?: ObjectManager::getInstance()->get(PreparedValueFactory::class);
  47. }
  48. /**
  49. * Save given list of configuration values into config file.
  50. *
  51. * @param array $values the configuration values (path-value pairs) to be saved.
  52. * @param string $scope scope in which configuration would be saved.
  53. * @param string|null $scopeCode
  54. * @return void
  55. */
  56. public function save(array $values, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null)
  57. {
  58. $config = [];
  59. $pathPrefix = $this->getPathPrefix($scope, $scopeCode);
  60. $values = array_filter(
  61. $values,
  62. function ($value) {
  63. return $value !== null;
  64. }
  65. );
  66. foreach ($values as $configPath => $configValue) {
  67. $fullConfigPath = $pathPrefix . $configPath;
  68. $backendModel = $this->preparedValueFactory->create($configPath, $configValue, $scope, $scopeCode);
  69. if ($backendModel instanceof Value) {
  70. $backendModel->validateBeforeSave();
  71. $backendModel->beforeSave();
  72. $configValue = $backendModel->getValue();
  73. $backendModel->afterSave();
  74. }
  75. $config = $this->setConfig($config, $fullConfigPath, $configValue);
  76. }
  77. $this->writer->saveConfig(
  78. [ConfigFilePool::APP_ENV => $config]
  79. );
  80. }
  81. /**
  82. * Apply configuration value into configuration array by given path.
  83. * Ignore values that equal to null.
  84. *
  85. * @param array $config
  86. * @param string $configPath
  87. * @param string $configValue
  88. * @return array
  89. */
  90. private function setConfig(array $config, $configPath, $configValue)
  91. {
  92. if ($configValue === null) {
  93. return $config;
  94. }
  95. $config = $this->arrayManager->set(
  96. $configPath,
  97. $config,
  98. $configValue
  99. );
  100. return $config;
  101. }
  102. /**
  103. * Generate config prefix from given $scope and $scopeCode.
  104. * If $scope isn't equal to 'default' and $scopeCode isn't empty put $scopeCode into prefix path,
  105. * otherwise ignore $scopeCode.
  106. *
  107. * @param string $scope
  108. * @param string $scopeCode
  109. * @return string
  110. */
  111. private function getPathPrefix($scope, $scopeCode)
  112. {
  113. $pathPrefixes = [System::CONFIG_TYPE, $scope];
  114. if ($scope !== ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  115. && !empty($scopeCode)
  116. ) {
  117. $pathPrefixes[] = $scopeCode;
  118. }
  119. return implode('/', $pathPrefixes) . '/';
  120. }
  121. }