Importer.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Model\Config;
  7. use Magento\Config\Model\Config\Importer\SaveProcessor;
  8. use Magento\Framework\App\Area;
  9. use Magento\Framework\App\Config;
  10. use Magento\Framework\App\Config\ScopeConfigInterface;
  11. use Magento\Framework\App\DeploymentConfig\ImporterInterface;
  12. use Magento\Framework\App\State;
  13. use Magento\Framework\Config\ScopeInterface;
  14. use Magento\Framework\Exception\State\InvalidTransitionException;
  15. use Magento\Framework\FlagManager;
  16. use Magento\Framework\Stdlib\ArrayUtils;
  17. /**
  18. * Processes data from specific section of configuration.
  19. * Do not physically imports data into database, but invokes backend models of configs.
  20. *
  21. * {@inheritdoc}
  22. * @see \Magento\Deploy\Console\Command\App\ConfigImport\Importer
  23. * @api
  24. * @since 101.0.0
  25. */
  26. class Importer implements ImporterInterface
  27. {
  28. /**
  29. * Code of the flag to retrieve previously imported config data.
  30. */
  31. const FLAG_CODE = 'system_config_snapshot';
  32. /**
  33. * The flag manager.
  34. *
  35. * @var FlagManager
  36. */
  37. private $flagManager;
  38. /**
  39. * An array utils.
  40. *
  41. * @var ArrayUtils
  42. */
  43. private $arrayUtils;
  44. /**
  45. * The application config storage.
  46. *
  47. * @var ScopeConfigInterface
  48. */
  49. private $scopeConfig;
  50. /**
  51. * The application state.
  52. *
  53. * @var State
  54. */
  55. private $state;
  56. /**
  57. * The application scope to run.
  58. *
  59. * @var ScopeInterface
  60. */
  61. private $scope;
  62. /**
  63. * The configuration saving processor.
  64. *
  65. * @var SaveProcessor
  66. */
  67. private $saveProcessor;
  68. /**
  69. * @param FlagManager $flagManager The flag manager
  70. * @param ArrayUtils $arrayUtils An array utils
  71. * @param SaveProcessor $saveProcessor Saves configuration data
  72. * @param ScopeConfigInterface $scopeConfig The application config storage.
  73. * @param State $state The application scope to run
  74. * @param ScopeInterface $scope The application scope
  75. */
  76. public function __construct(
  77. FlagManager $flagManager,
  78. ArrayUtils $arrayUtils,
  79. SaveProcessor $saveProcessor,
  80. ScopeConfigInterface $scopeConfig,
  81. State $state,
  82. ScopeInterface $scope
  83. ) {
  84. $this->flagManager = $flagManager;
  85. $this->arrayUtils = $arrayUtils;
  86. $this->saveProcessor = $saveProcessor;
  87. $this->scopeConfig = $scopeConfig;
  88. $this->state = $state;
  89. $this->scope = $scope;
  90. }
  91. /**
  92. * Invokes saving of configurations when data was not imported before
  93. * or current value is different from previously imported.
  94. *
  95. * {@inheritdoc}
  96. * @since 101.0.0
  97. */
  98. public function import(array $data)
  99. {
  100. $currentScope = $this->scope->getCurrentScope();
  101. try {
  102. $savedFlag = $this->flagManager->getFlagData(static::FLAG_CODE) ?: [];
  103. $changedData = array_replace_recursive(
  104. $this->arrayUtils->recursiveDiff($savedFlag, $data),
  105. $this->arrayUtils->recursiveDiff($data, $savedFlag)
  106. );
  107. /**
  108. * Re-init config with new data.
  109. * This is required to load latest effective configuration value.
  110. */
  111. if ($this->scopeConfig instanceof Config) {
  112. $this->scopeConfig->clean();
  113. }
  114. $this->state->emulateAreaCode(Area::AREA_ADMINHTML, function () use ($changedData) {
  115. $this->scope->setCurrentScope(Area::AREA_ADMINHTML);
  116. // Invoke saving of new values.
  117. $this->saveProcessor->process($changedData);
  118. });
  119. $this->scope->setCurrentScope($currentScope);
  120. $this->flagManager->saveFlag(static::FLAG_CODE, $data);
  121. } catch (\Exception $e) {
  122. throw new InvalidTransitionException(__('%1', $e->getMessage()), $e);
  123. } finally {
  124. $this->scope->setCurrentScope($currentScope);
  125. }
  126. return ['System config was processed'];
  127. }
  128. /**
  129. * @inheritdoc
  130. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  131. * @since 101.0.0
  132. */
  133. public function getWarningMessages(array $data)
  134. {
  135. return [];
  136. }
  137. }