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