-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Fallback.php
182 lines (163 loc) · 5.2 KB
/
Fallback.php
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Store\Model\Config\Processor;
use Magento\Framework\App\Config\Spi\PostProcessorInterface;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\ResourceConnection;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Api\Data\WebsiteInterface;
use Magento\Store\App\Config\Type\Scopes;
use Magento\Store\Model\ResourceModel\Store;
use Magento\Store\Model\ResourceModel\Store\AllStoresCollectionFactory;
use Magento\Store\Model\ResourceModel\Website;
use Magento\Store\Model\ResourceModel\Website\AllWebsitesCollection;
use Magento\Store\Model\ResourceModel\Website\AllWebsitesCollectionFactory;
/**
* Fallback throguh different scopes and merge them
*
* @package Magento\Store\Model\Config\Processor
*/
class Fallback implements PostProcessorInterface
{
/**
* @var Scopes
*/
private $scopes;
/**
* @var ResourceConnection
*/
private $resourceConnection;
/**
* @var array
*/
private $storeData = [];
/**
* @var array
*/
private $websiteData = [];
/**
* @var Store
*/
private $storeResource;
/**
* @var Website
*/
private $websiteResource;
/**
* @var DeploymentConfig
*/
private $deploymentConfig;
/**
* @param Scopes $scopes
* @param ResourceConnection $resourceConnection
* @param Store $storeResource
* @param Website $websiteResource
* @param DeploymentConfig $deploymentConfig
*/
public function __construct(
Scopes $scopes,
ResourceConnection $resourceConnection,
Store $storeResource,
Website $websiteResource,
DeploymentConfig $deploymentConfig
) {
$this->scopes = $scopes;
$this->resourceConnection = $resourceConnection;
$this->storeResource = $storeResource;
$this->websiteResource = $websiteResource;
$this->deploymentConfig = $deploymentConfig;
}
/**
* @inheritdoc
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function process(array $data)
{
if ($this->deploymentConfig->isDbAvailable()) {//read only from db
$this->storeData = $this->storeResource->readAllStores();
$this->websiteData = $this->websiteResource->readAllWebsites();
} else {
$this->storeData = $this->scopes->get('stores');
$this->websiteData = $this->scopes->get('websites');
}
$defaultConfig = isset($data['default']) ? $data['default'] : [];
$result = [
'default' => $defaultConfig,
'websites' => [],
'stores' => []
];
$websitesConfig = isset($data['websites']) ? $data['websites'] : [];
$result['websites'] = $this->prepareWebsitesConfig($defaultConfig, $websitesConfig);
$storesConfig = isset($data['stores']) ? $data['stores'] : [];
$result['stores'] = $this->prepareStoresConfig($defaultConfig, $websitesConfig, $storesConfig);
return $result;
}
/**
* Prepare website data from Config/Type/Scopes
*
* @param array $defaultConfig
* @param array $websitesConfig
* @return array
*/
private function prepareWebsitesConfig(
array $defaultConfig,
array $websitesConfig
) {
$result = [];
foreach ($this->websiteData as $website) {
$code = $website['code'];
$id = $website['website_id'];
$websiteConfig = isset($websitesConfig[$code]) ? $websitesConfig[$code] : [];
$result[$code] = array_replace_recursive($defaultConfig, $websiteConfig);
$result[$id] = $result[$code];
}
return $result;
}
/**
* Prepare stores data from Config/Type/Scopes
*
* @param array $defaultConfig
* @param array $websitesConfig
* @param array $storesConfig
* @return array
*/
private function prepareStoresConfig(
array $defaultConfig,
array $websitesConfig,
array $storesConfig
) {
$result = [];
foreach ($this->storeData as $store) {
$code = $store['code'];
$id = $store['store_id'];
$websiteConfig = [];
if (isset($store['website_id'])) {
$websiteConfig = $this->getWebsiteConfig($websitesConfig, $store['website_id']);
}
$storeConfig = isset($storesConfig[$code]) ? $storesConfig[$code] : [];
$result[$code] = array_replace_recursive($defaultConfig, $websiteConfig, $storeConfig);
$result[$id] = $result[$code];
}
return $result;
}
/**
* Find by id specific information about website.
*
* @param array $websites Has next format: (website_code => [website_data])
* @param int $id
* @return array
*/
private function getWebsiteConfig(array $websites, $id)
{
foreach ($this->websiteData as $website) {
if ($website['website_id'] == $id) {
$code = $website['code'];
return isset($websites[$code]) ? $websites[$code] : [];
}
}
return [];
}
}