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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* Application configuration object. Used to access configuration when application is initialized and installed.
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\App;
use Magento\Framework\App\Config\ScopeCodeResolver;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\DataObject;
use Magento\TestFramework\ObjectManager;
/**
* @inheritdoc
*/
class Config extends \Magento\Framework\App\Config
{
/**
* @var DataObject[]
*/
private $data;
/**
* @var ScopeCodeResolver
*/
private $scopeCodeResolver;
/**
* Initialize data object with all settings data
*
* @param array $data
* @param string $configType
* @return void
*/
private function setData(array $data, $configType)
{
$this->data[$configType] = new DataObject($data);
}
/**
* Retrieve Scope Code Resolver
*
* @return ScopeCodeResolver
*/
private function getScopeCodeResolver()
{
if (!$this->scopeCodeResolver) {
$this->scopeCodeResolver = ObjectManager::getInstance()->get(ScopeCodeResolver::class);
}
return $this->scopeCodeResolver;
}
/**
* Set config value in the corresponding config scope
*
* @param string $path
* @param mixed $value
* @param string $scope
* @param null|string $scopeCode
* @return void
*/
public function setValue(
$path,
$value,
$scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
$scopeCode = null
) {
$result = $this->get('system');
if ($scope === 'store') {
$scope = 'stores';
} elseif ($scope === 'website') {
$scope = 'websites';
}
if (empty($scopeCode)) {
$scopeCode = $this->getScopeCodeResolver()->resolve($scope, $scopeCode);
}
$keys = explode('/', $path);
if ($scope !== ScopeConfigInterface::SCOPE_TYPE_DEFAULT) {
$searchKeys = array_merge([$scope, $scopeCode], $keys);
} else {
$searchKeys = array_merge([$scope], $keys);
}
$this->updateResult($searchKeys, $result, $value);
$this->setData($result, 'system');
}
/**
* Recursively update results in global variable, which hold configs
*
* @param array $keys
* @param array $result
* @param mixed $value
* @return void
*/
private function updateResult(array $keys, & $result, $value)
{
$key = array_shift($keys);
if (empty($keys)) {
$result[$key] = $value;
} else {
$this->updateResult($keys, $result[$key], $value);
}
}
/**
* Flush all muted settings
*
* @return void
*/
public function clean()
{
$this->data = null;
$this->scopeCodeResolver = null;
parent::clean();
}
/**
* @inheritdoc
*/
public function get($configType, $path = null, $default = null)
{
$path = $path === null ? '' : $path;
if (!isset($this->data[$configType]) || $this->data[$configType]->getData($path) === null) {
return parent::get($configType, $path, $default);
}
return $this->data[$configType]->getData($path);
}
}