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
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Config\Model;
use Magento\Config\Model\Config\BackendFactory;
use Magento\Config\Model\Config\Structure;
use Magento\Config\Model\Config\StructureFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\ValueInterface;
use Magento\Framework\App\Config\Value;
use Magento\Framework\App\ScopeInterface;
use Magento\Store\Model\ScopeTypeNormalizer;
use Magento\Framework\App\ScopeResolverPool;
use Magento\Framework\Exception\RuntimeException;
/**
* Creates a prepared instance of Value.
*
* @see ValueInterface
* @api
* @since 101.0.0
*/
class PreparedValueFactory
{
/**
* The scope resolver pool.
*
* @var ScopeResolverPool
*/
private $scopeResolverPool;
/**
* The manager for system configuration structure.
*
* @var StructureFactory
*/
private $structureFactory;
/**
* The factory for configuration value objects.
*
* @see ValueInterface
* @var BackendFactory
*/
private $valueFactory;
/**
* The scope configuration.
*
* @var ScopeConfigInterface
*/
private $config;
/**
* The scope type normalizer.
*
* @var ScopeTypeNormalizer
*/
private $scopeTypeNormalizer;
/**
* @param ScopeResolverPool $scopeResolverPool The scope resolver pool
* @param StructureFactory $structureFactory The manager for system configuration structure
* @param BackendFactory $valueFactory The factory for configuration value objects
* @param ScopeConfigInterface $config The scope configuration
* @param ScopeTypeNormalizer $scopeTypeNormalizer The scope type normalizer
*/
public function __construct(
ScopeResolverPool $scopeResolverPool,
StructureFactory $structureFactory,
BackendFactory $valueFactory,
ScopeConfigInterface $config,
ScopeTypeNormalizer $scopeTypeNormalizer
) {
$this->scopeResolverPool = $scopeResolverPool;
$this->structureFactory = $structureFactory;
$this->valueFactory = $valueFactory;
$this->config = $config;
$this->scopeTypeNormalizer = $scopeTypeNormalizer;
}
/**
* Returns instance of Value with defined properties.
*
* @param string $path The configuration path in format section/group/field_name
* @param string $value The configuration value
* @param string $scope The configuration scope (default, website, or store)
* @param string|int|null $scopeCode The scope code
* @return ValueInterface
* @throws RuntimeException If Value can not be created
* @see ValueInterface
* @since 101.0.0
*/
public function create($path, $value, $scope, $scopeCode = null)
{
try {
/** @var Structure $structure */
$structure = $this->structureFactory->create();
/** @var Structure\ElementInterface $field */
$field = $structure->getElementByConfigPath($path);
$configPath = $path;
/** @var string $backendModelName */
if ($field instanceof Structure\Element\Field && $field->hasBackendModel()) {
$backendModelName = $field->getData()['backend_model'];
$configPath = $field->getConfigPath() ?: $path;
} else {
$backendModelName = ValueInterface::class;
}
/** @var ValueInterface $backendModel */
$backendModel = $this->valueFactory->create(
$backendModelName,
['config' => $this->config]
);
if ($backendModel instanceof Value) {
$scopeId = 0;
$scope = $this->scopeTypeNormalizer->normalize($scope);
if ($scope !== ScopeInterface::SCOPE_DEFAULT) {
$scopeId = $this->scopeResolverPool->get($scope)
->getScope($scopeCode)
->getId();
}
if ($field instanceof Structure\Element\Field) {
$groupPath = $field->getGroupPath();
$group = $structure->getElement($groupPath);
$backendModel->setField($field->getId());
$backendModel->setGroupId($group->getId());
$backendModel->setFieldConfig($field->getData());
}
$backendModel->setPath($configPath);
$backendModel->setScope($scope);
$backendModel->setScopeId($scopeId);
$backendModel->setScopeCode($scopeCode);
$backendModel->setValue($value);
}
return $backendModel;
} catch (\Exception $exception) {
throw new RuntimeException(__('%1', $exception->getMessage()), $exception);
}
}
}