7
7
8
8
use Magento \Config \Model \Config \Structure ;
9
9
use Magento \Config \Model \Config \StructureFactory ;
10
+ use Magento \Framework \App \Config \ValueInterface ;
10
11
use Magento \Framework \App \Config \Value ;
11
12
use Magento \Framework \App \Config \ValueFactory ;
12
- use Magento \Framework \App \Config \ValueInterface ;
13
- use Magento \Framework \App \DeploymentConfig ;
13
+ use Magento \Framework \App \ScopeInterface ;
14
+ use Magento \Framework \App \ScopeResolverPool ;
15
+ use Magento \Framework \Exception \RuntimeException ;
14
16
15
17
/**
16
18
* Creates a prepared instance of Value.
20
22
class PreparedValueFactory
21
23
{
22
24
/**
23
- * The deployment configuration reader .
25
+ * The scope resolver pool .
24
26
*
25
- * @var DeploymentConfig
27
+ * @var ScopeResolverPool
26
28
*/
27
- private $ deploymentConfig ;
29
+ private $ scopeResolverPool ;
28
30
29
31
/**
30
32
* The manager for system configuration structure.
@@ -42,16 +44,16 @@ class PreparedValueFactory
42
44
private $ valueFactory ;
43
45
44
46
/**
45
- * @param DeploymentConfig $deploymentConfig The deployment configuration reader
47
+ * @param ScopeResolverPool $scopeResolverPool The scope resolver pool
46
48
* @param StructureFactory $structureFactory The manager for system configuration structure
47
49
* @param ValueFactory $valueFactory The factory for configuration value objects
48
50
*/
49
51
public function __construct (
50
- DeploymentConfig $ deploymentConfig ,
52
+ ScopeResolverPool $ scopeResolverPool ,
51
53
StructureFactory $ structureFactory ,
52
54
ValueFactory $ valueFactory
53
55
) {
54
- $ this ->deploymentConfig = $ deploymentConfig ;
56
+ $ this ->scopeResolverPool = $ scopeResolverPool ;
55
57
$ this ->structureFactory = $ structureFactory ;
56
58
$ this ->valueFactory = $ valueFactory ;
57
59
}
@@ -62,31 +64,41 @@ public function __construct(
62
64
* @param string $path The configuration path in format group/section/field_name
63
65
* @param string $value The configuration value
64
66
* @param string $scope The configuration scope (default, website, or store)
65
- * @param string $scopeCode The scope code
67
+ * @param string|int|null $scopeCode The scope code
66
68
* @return ValueInterface
69
+ * @throws RuntimeException If Value can not be created
67
70
* @see ValueInterface
68
71
* @see Value
69
72
*/
70
- public function create ($ path , $ value , $ scope , $ scopeCode )
73
+ public function create ($ path , $ value , $ scope , $ scopeCode = null )
71
74
{
72
- /** @var Structure $structure */
73
- $ structure = $ this ->structureFactory ->create ();
74
- /** @var Structure\ElementInterface $field */
75
- $ field = $ this ->deploymentConfig ->isAvailable ()
76
- ? $ structure ->getElement ($ path )
77
- : null ;
78
- /** @var ValueInterface $backendModel */
79
- $ backendModel = $ field instanceof Structure \Element \Field && $ field ->hasBackendModel ()
80
- ? $ field ->getBackendModel ()
81
- : $ this ->valueFactory ->create ();
75
+ try {
76
+ /** @var Structure $structure */
77
+ $ structure = $ this ->structureFactory ->create ();
78
+ /** @var Structure\ElementInterface $field */
79
+ $ field = $ structure ->getElement ($ path );
80
+ /** @var ValueInterface $backendModel */
81
+ $ backendModel = $ field instanceof Structure \Element \Field && $ field ->hasBackendModel ()
82
+ ? $ field ->getBackendModel ()
83
+ : $ this ->valueFactory ->create ();
82
84
83
- if ($ backendModel instanceof Value) {
84
- $ backendModel ->setPath ($ path );
85
- $ backendModel ->setScope ($ scope );
86
- $ backendModel ->setScopeId ($ scopeCode );
87
- $ backendModel ->setValue ($ value );
88
- }
85
+ if ($ backendModel instanceof Value) {
86
+ $ scopeId = 0 ;
89
87
90
- return $ backendModel ;
88
+ if ($ scope !== ScopeInterface::SCOPE_DEFAULT ) {
89
+ $ scopeResolver = $ this ->scopeResolverPool ->get ($ scope );
90
+ $ scopeId = $ scopeResolver ->getScope ($ scopeCode )->getId ();
91
+ }
92
+
93
+ $ backendModel ->setPath ($ path );
94
+ $ backendModel ->setScope ($ scope );
95
+ $ backendModel ->setScopeId ($ scopeId );
96
+ $ backendModel ->setValue ($ value );
97
+ }
98
+
99
+ return $ backendModel ;
100
+ } catch (\Exception $ exception ) {
101
+ throw new RuntimeException (__ ('%1 ' , $ exception ->getMessage ()), $ exception );
102
+ }
91
103
}
92
104
}
0 commit comments