66namespace Magento \Store \Model \Config \Processor ;
77
88use 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 ;
913use 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 ;
1019
1120/**
1221 * Fallback through different scopes and merge them
@@ -18,20 +27,72 @@ class Fallback implements PostProcessorInterface
1827 */
1928 private $ scopes ;
2029
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+
2160 /**
2261 * Fallback constructor.
62+ *
2363 * @param Scopes $scopes
64+ * @param ResourceConnection $resourceConnection
65+ * @param Store $storeResource
66+ * @param Website $websiteResource
67+ * @param DeploymentConfig $deploymentConfig
2468 */
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+ ) {
2776 $ this ->scopes = $ scopes ;
77+ $ this ->resourceConnection = $ resourceConnection ;
78+ $ this ->storeResource = $ storeResource ;
79+ $ this ->websiteResource = $ websiteResource ;
80+ $ this ->deploymentConfig = $ deploymentConfig ;
2881 }
2982
3083 /**
3184 * @inheritdoc
3285 */
3386 public function process (array $ data )
3487 {
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+
3596 $ defaultConfig = isset ($ data ['default ' ]) ? $ data ['default ' ] : [];
3697 $ result = [
3798 'default ' => $ defaultConfig ,
@@ -55,12 +116,14 @@ public function process(array $data)
55116 * @param array $websitesConfig
56117 * @return array
57118 */
58- private function prepareWebsitesConfig (array $ defaultConfig , array $ websitesConfig )
59- {
119+ private function prepareWebsitesConfig (
120+ array $ defaultConfig ,
121+ array $ websitesConfig
122+ ) {
60123 $ 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 ' ];
64127 $ websiteConfig = isset ($ websitesConfig [$ code ]) ? $ websitesConfig [$ code ] : [];
65128 $ result [$ code ] = array_replace_recursive ($ defaultConfig , $ websiteConfig );
66129 $ result [$ id ] = $ result [$ code ];
@@ -76,15 +139,19 @@ private function prepareWebsitesConfig(array $defaultConfig, array $websitesConf
76139 * @param array $storesConfig
77140 * @return array
78141 */
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+ ) {
81147 $ 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 ' ];
85152 $ 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 ' ]);
88155 }
89156 $ storeConfig = isset ($ storesConfig [$ code ]) ? $ storesConfig [$ code ] : [];
90157 $ result [$ code ] = array_replace_recursive ($ defaultConfig , $ websiteConfig , $ storeConfig );
@@ -94,17 +161,17 @@ private function prepareStoresConfig(array $defaultConfig, array $websitesConfig
94161 }
95162
96163 /**
97- * Retrieve Website Config
164+ * Find information about website by its ID.
98165 *
99- * @param array $websites
166+ * @param array $websites Has next format: (website_code => [website_data])
100167 * @param int $id
101168 * @return array
102169 */
103170 private function getWebsiteConfig (array $ websites , $ id )
104171 {
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 ' ];
108175 return isset ($ websites [$ code ]) ? $ websites [$ code ] : [];
109176 }
110177 }
0 commit comments