6
6
namespace Magento \Store \Model \Config \Processor ;
7
7
8
8
use Magento \Framework \App \Config \Spi \PostProcessorInterface ;
9
+ use Magento \Framework \App \DeploymentConfig ;
10
+ use Magento \Framework \App \ResourceConnection ;
11
+ use Magento \Store \Api \Data \StoreInterface ;
12
+ use Magento \Store \Api \Data \WebsiteInterface ;
9
13
use Magento \Store \App \Config \Type \Scopes ;
14
+ use Magento \Store \Model \ResourceModel \Store ;
15
+ use Magento \Store \Model \ResourceModel \Store \AllStoresCollectionFactory ;
16
+ use Magento \Store \Model \ResourceModel \Website ;
17
+ use Magento \Store \Model \ResourceModel \Website \AllWebsitesCollection ;
18
+ use Magento \Store \Model \ResourceModel \Website \AllWebsitesCollectionFactory ;
10
19
11
20
/**
12
21
* Fallback through different scopes and merge them
@@ -18,20 +27,72 @@ class Fallback implements PostProcessorInterface
18
27
*/
19
28
private $ scopes ;
20
29
30
+ /**
31
+ * @var ResourceConnection
32
+ */
33
+ private $ resourceConnection ;
34
+
35
+ /**
36
+ * @var array
37
+ */
38
+ private $ storeData = [];
39
+
40
+ /**
41
+ * @var array
42
+ */
43
+ private $ websiteData = [];
44
+
45
+ /**
46
+ * @var Store
47
+ */
48
+ private $ storeResource ;
49
+
50
+ /**
51
+ * @var Website
52
+ */
53
+ private $ websiteResource ;
54
+
55
+ /**
56
+ * @var DeploymentConfig
57
+ */
58
+ private $ deploymentConfig ;
59
+
21
60
/**
22
61
* Fallback constructor.
62
+ *
23
63
* @param Scopes $scopes
64
+ * @param ResourceConnection $resourceConnection
65
+ * @param Store $storeResource
66
+ * @param Website $websiteResource
67
+ * @param DeploymentConfig $deploymentConfig
24
68
*/
25
- public function __construct (Scopes $ scopes )
26
- {
69
+ public function __construct (
70
+ Scopes $ scopes ,
71
+ ResourceConnection $ resourceConnection ,
72
+ Store $ storeResource ,
73
+ Website $ websiteResource ,
74
+ DeploymentConfig $ deploymentConfig
75
+ ) {
27
76
$ this ->scopes = $ scopes ;
77
+ $ this ->resourceConnection = $ resourceConnection ;
78
+ $ this ->storeResource = $ storeResource ;
79
+ $ this ->websiteResource = $ websiteResource ;
80
+ $ this ->deploymentConfig = $ deploymentConfig ;
28
81
}
29
82
30
83
/**
31
84
* @inheritdoc
32
85
*/
33
86
public function process (array $ data )
34
87
{
88
+ if ($ this ->deploymentConfig ->isDbAvailable ()) {//read only from db
89
+ $ this ->storeData = $ this ->storeResource ->readAllStores ();
90
+ $ this ->websiteData = $ this ->websiteResource ->readAllWebsites ();
91
+ } else {
92
+ $ this ->storeData = $ this ->scopes ->get ('stores ' );
93
+ $ this ->websiteData = $ this ->scopes ->get ('websites ' );
94
+ }
95
+
35
96
$ defaultConfig = isset ($ data ['default ' ]) ? $ data ['default ' ] : [];
36
97
$ result = [
37
98
'default ' => $ defaultConfig ,
@@ -55,12 +116,14 @@ public function process(array $data)
55
116
* @param array $websitesConfig
56
117
* @return array
57
118
*/
58
- private function prepareWebsitesConfig (array $ defaultConfig , array $ websitesConfig )
59
- {
119
+ private function prepareWebsitesConfig (
120
+ array $ defaultConfig ,
121
+ array $ websitesConfig
122
+ ) {
60
123
$ result = [];
61
- foreach ((array )$ this ->scopes -> get ( ' websites ' ) as $ websiteData ) {
62
- $ code = $ websiteData ['code ' ];
63
- $ id = $ websiteData ['website_id ' ];
124
+ foreach ((array )$ this ->websiteData as $ website ) {
125
+ $ code = $ website ['code ' ];
126
+ $ id = $ website ['website_id ' ];
64
127
$ websiteConfig = isset ($ websitesConfig [$ code ]) ? $ websitesConfig [$ code ] : [];
65
128
$ result [$ code ] = array_replace_recursive ($ defaultConfig , $ websiteConfig );
66
129
$ result [$ id ] = $ result [$ code ];
@@ -76,15 +139,19 @@ private function prepareWebsitesConfig(array $defaultConfig, array $websitesConf
76
139
* @param array $storesConfig
77
140
* @return array
78
141
*/
79
- private function prepareStoresConfig (array $ defaultConfig , array $ websitesConfig , array $ storesConfig )
80
- {
142
+ private function prepareStoresConfig (
143
+ array $ defaultConfig ,
144
+ array $ websitesConfig ,
145
+ array $ storesConfig
146
+ ) {
81
147
$ result = [];
82
- foreach ((array )$ this ->scopes ->get ('stores ' ) as $ storeData ) {
83
- $ code = $ storeData ['code ' ];
84
- $ id = $ storeData ['store_id ' ];
148
+
149
+ foreach ((array )$ this ->storeData as $ store ) {
150
+ $ code = $ store ['code ' ];
151
+ $ id = $ store ['store_id ' ];
85
152
$ websiteConfig = [];
86
- if (isset ($ storeData ['website_id ' ])) {
87
- $ websiteConfig = $ this ->getWebsiteConfig ($ websitesConfig , $ storeData ['website_id ' ]);
153
+ if (isset ($ store ['website_id ' ])) {
154
+ $ websiteConfig = $ this ->getWebsiteConfig ($ websitesConfig , $ store ['website_id ' ]);
88
155
}
89
156
$ storeConfig = isset ($ storesConfig [$ code ]) ? $ storesConfig [$ code ] : [];
90
157
$ result [$ code ] = array_replace_recursive ($ defaultConfig , $ websiteConfig , $ storeConfig );
@@ -94,17 +161,17 @@ private function prepareStoresConfig(array $defaultConfig, array $websitesConfig
94
161
}
95
162
96
163
/**
97
- * Retrieve Website Config
164
+ * Find information about website by its ID.
98
165
*
99
- * @param array $websites
166
+ * @param array $websites Has next format: (website_code => [website_data])
100
167
* @param int $id
101
168
* @return array
102
169
*/
103
170
private function getWebsiteConfig (array $ websites , $ id )
104
171
{
105
- foreach ($ this ->scopes -> get ( ' websites ' ) as $ websiteData ) {
106
- if ($ websiteData ['website_id ' ] == $ id ) {
107
- $ code = $ websiteData ['code ' ];
172
+ foreach (( array ) $ this ->websiteData as $ website ) {
173
+ if ($ website ['website_id ' ] == $ id ) {
174
+ $ code = $ website ['code ' ];
108
175
return isset ($ websites [$ code ]) ? $ websites [$ code ] : [];
109
176
}
110
177
}
0 commit comments