SaveProcessor.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Model\Config\Importer;
  7. use Magento\Config\Model\PreparedValueFactory;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Framework\App\Config\Value;
  10. use Magento\Framework\Stdlib\ArrayUtils;
  11. /**
  12. * Saves configuration from importer
  13. */
  14. class SaveProcessor
  15. {
  16. /**
  17. * Builder which creates value object according to their backend models.
  18. *
  19. * @var PreparedValueFactory
  20. */
  21. private $valueFactory;
  22. /**
  23. * An array utils.
  24. *
  25. * @var ArrayUtils
  26. */
  27. private $arrayUtils;
  28. /**
  29. * The application config storage.
  30. *
  31. * @var ScopeConfigInterface
  32. */
  33. private $scopeConfig;
  34. /**
  35. * @param ArrayUtils $arrayUtils An array utils
  36. * @param PreparedValueFactory $valueBuilder Builder which creates value object according to their backend models
  37. * @param ScopeConfigInterface $scopeConfig The application config storage.
  38. */
  39. public function __construct(
  40. ArrayUtils $arrayUtils,
  41. PreparedValueFactory $valueBuilder,
  42. ScopeConfigInterface $scopeConfig
  43. ) {
  44. $this->arrayUtils = $arrayUtils;
  45. $this->valueFactory = $valueBuilder;
  46. $this->scopeConfig = $scopeConfig;
  47. }
  48. /**
  49. * Emulates saving of data array.
  50. *
  51. * @param array $data The data to be saved
  52. * @return void
  53. */
  54. public function process(array $data)
  55. {
  56. foreach ($data as $scope => $scopeData) {
  57. if ($scope === ScopeConfigInterface::SCOPE_TYPE_DEFAULT) {
  58. $this->invokeSave($scopeData, $scope);
  59. } else {
  60. foreach ($scopeData as $scopeCode => $scopeCodeData) {
  61. $this->invokeSave($scopeCodeData, $scope, $scopeCode);
  62. }
  63. }
  64. }
  65. }
  66. /**
  67. * Emulates saving of configuration.
  68. * This is a temporary solution until Magento reworks
  69. * backend models for configurations.
  70. *
  71. * Example of $scopeData argument:
  72. *
  73. * ```php
  74. * [
  75. * 'web' => [
  76. * 'unsecure' => [
  77. * 'base_url' => "http://magento2.local/"
  78. * ]
  79. * ]
  80. * ];
  81. * ```
  82. *
  83. * @param array $scopeData The data for specific scope
  84. * @param string $scope The configuration scope (default, website, or store)
  85. * @param string $scopeCode The scope code
  86. * @return void
  87. */
  88. private function invokeSave(array $scopeData, $scope, $scopeCode = null)
  89. {
  90. $scopeData = array_keys($this->arrayUtils->flatten($scopeData));
  91. foreach ($scopeData as $path) {
  92. $value = $this->scopeConfig->getValue($path, $scope, $scopeCode);
  93. $backendModel = $this->valueFactory->create($path, $value, $scope, $scopeCode);
  94. if ($backendModel instanceof Value) {
  95. $backendModel->beforeSave();
  96. $backendModel->afterSave();
  97. }
  98. }
  99. }
  100. }