|
7 | 7 |
|
8 | 8 | use Magento\Framework\App\Config\ConfigSourceInterface; |
9 | 9 | use Magento\Framework\App\DeploymentConfig; |
10 | | -use Magento\Store\Model\Group; |
11 | | -use Magento\Store\Model\ResourceModel\Website\CollectionFactory as WebsiteCollectionFactory; |
12 | | -use Magento\Store\Model\ResourceModel\Group\CollectionFactory as GroupCollectionFactory; |
13 | | -use Magento\Store\Model\ResourceModel\Store\CollectionFactory as StoreCollectionFactory; |
14 | | -use Magento\Store\Model\Store; |
15 | | -use Magento\Store\Model\Website; |
16 | | -use Magento\Store\Model\WebsiteFactory; |
17 | | -use Magento\Store\Model\GroupFactory; |
18 | | -use Magento\Store\Model\StoreFactory; |
| 10 | +use Magento\Framework\App\ResourceConnection; |
| 11 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
19 | 12 |
|
20 | 13 | /** |
21 | | - * Class RuntimeConfigSource |
22 | | - * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 14 | + * Config source. Retrieve all configuration for scopes from db |
23 | 15 | */ |
24 | 16 | class RuntimeConfigSource implements ConfigSourceInterface |
25 | 17 | { |
26 | | - /** |
27 | | - * @var WebsiteCollectionFactory |
28 | | - */ |
29 | | - private $websiteCollectionFactory; |
30 | | - |
31 | | - /** |
32 | | - * @var GroupCollectionFactory |
33 | | - */ |
34 | | - private $groupCollectionFactory; |
35 | | - |
36 | | - /** |
37 | | - * @var StoreCollectionFactory |
38 | | - */ |
39 | | - private $storeCollectionFactory; |
40 | | - |
41 | 18 | /** |
42 | 19 | * @var DeploymentConfig |
43 | 20 | */ |
44 | 21 | private $deploymentConfig; |
45 | 22 |
|
46 | 23 | /** |
47 | | - * @var WebsiteFactory |
48 | | - */ |
49 | | - private $websiteFactory; |
50 | | - |
51 | | - /** |
52 | | - * @var GroupFactory |
| 24 | + * @var ResourceConnection |
53 | 25 | */ |
54 | | - private $groupFactory; |
| 26 | + private $resourceConnection; |
55 | 27 |
|
56 | 28 | /** |
57 | | - * @var StoreFactory |
| 29 | + * @var AdapterInterface |
58 | 30 | */ |
59 | | - private $storeFactory; |
| 31 | + private $connection; |
60 | 32 |
|
61 | 33 | /** |
62 | | - * DynamicDataProvider constructor. |
63 | | - * |
64 | | - * @param WebsiteCollectionFactory $websiteCollectionFactory |
65 | | - * @param GroupCollectionFactory $groupCollectionFactory |
66 | | - * @param StoreCollectionFactory $storeCollectionFactory |
67 | | - * @param WebsiteFactory $websiteFactory |
68 | | - * @param GroupFactory $groupFactory |
69 | | - * @param StoreFactory $storeFactory |
70 | 34 | * @param DeploymentConfig $deploymentConfig |
| 35 | + * @param ResourceConnection $resourceConnection |
71 | 36 | */ |
72 | 37 | public function __construct( |
73 | | - WebsiteCollectionFactory $websiteCollectionFactory, |
74 | | - GroupCollectionFactory $groupCollectionFactory, |
75 | | - StoreCollectionFactory $storeCollectionFactory, |
76 | | - WebsiteFactory $websiteFactory, |
77 | | - GroupFactory $groupFactory, |
78 | | - StoreFactory $storeFactory, |
79 | | - DeploymentConfig $deploymentConfig |
| 38 | + DeploymentConfig $deploymentConfig, |
| 39 | + ResourceConnection $resourceConnection |
80 | 40 | ) { |
81 | | - $this->websiteCollectionFactory = $websiteCollectionFactory; |
82 | | - $this->groupCollectionFactory = $groupCollectionFactory; |
83 | | - $this->storeCollectionFactory = $storeCollectionFactory; |
84 | 41 | $this->deploymentConfig = $deploymentConfig; |
85 | | - $this->websiteFactory = $websiteFactory; |
86 | | - $this->groupFactory = $groupFactory; |
87 | | - $this->storeFactory = $storeFactory; |
| 42 | + $this->resourceConnection = $resourceConnection; |
88 | 43 | } |
89 | 44 |
|
90 | 45 | /** |
91 | | - * @inheritdoc |
| 46 | + * Return whole scopes config data from db. |
| 47 | + * Ignore $path argument due to config source must return all config data |
| 48 | + * |
| 49 | + * @param string $path |
| 50 | + * @return array |
| 51 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
92 | 52 | */ |
93 | 53 | public function get($path = '') |
94 | 54 | { |
95 | | - if (strpos($path, '/') === false) { |
96 | | - $scopePool = $path; |
97 | | - $scopeCode = null; |
98 | | - } else { |
99 | | - list($scopePool, $scopeCode) = explode('/', $path); |
100 | | - } |
101 | | - |
102 | | - $data = []; |
103 | 55 | if ($this->canUseDatabase()) { |
104 | | - switch ($scopePool) { |
105 | | - case 'websites': |
106 | | - $data['websites'] = $this->getWebsitesData($scopeCode); |
107 | | - break; |
108 | | - case 'groups': |
109 | | - $data['groups'] = $this->getGroupsData($scopeCode); |
110 | | - break; |
111 | | - case 'stores': |
112 | | - $data['stores'] = $this->getStoresData($scopeCode); |
113 | | - break; |
114 | | - default: |
115 | | - $data = [ |
116 | | - 'websites' => $this->getWebsitesData(), |
117 | | - 'groups' => $this->getGroupsData(), |
118 | | - 'stores' => $this->getStoresData(), |
119 | | - ]; |
120 | | - break; |
121 | | - } |
| 56 | + return [ |
| 57 | + 'websites' => $this->getEntities('store_website', 'code'), |
| 58 | + 'groups' => $this->getEntities('store_group', 'group_id'), |
| 59 | + 'stores' => $this->getEntities('store', 'code'), |
| 60 | + ]; |
122 | 61 | } |
123 | 62 |
|
124 | | - return $data; |
| 63 | + return []; |
125 | 64 | } |
126 | 65 |
|
127 | 66 | /** |
128 | | - * @param string|null $code |
129 | | - * @return array |
| 67 | + * @return AdapterInterface |
130 | 68 | */ |
131 | | - private function getWebsitesData($code = null) |
| 69 | + private function getConnection() |
132 | 70 | { |
133 | | - if ($code !== null) { |
134 | | - $website = $this->websiteFactory->create(); |
135 | | - $website->load($code); |
136 | | - $data[$code] = $website->getData(); |
137 | | - } else { |
138 | | - $collection = $this->websiteCollectionFactory->create(); |
139 | | - $collection->setLoadDefault(true); |
140 | | - $data = []; |
141 | | - /** @var Website $website */ |
142 | | - foreach ($collection as $website) { |
143 | | - $data[$website->getCode()] = $website->getData(); |
144 | | - } |
| 71 | + if (null === $this->connection) { |
| 72 | + $this->connection = $this->resourceConnection->getConnection(); |
145 | 73 | } |
146 | | - return $data; |
| 74 | + return $this->connection; |
147 | 75 | } |
148 | 76 |
|
149 | 77 | /** |
150 | | - * @param string|null $id |
| 78 | + * Get entities from specified table in format [entityKeyField => [entity data], ...] |
| 79 | + * |
| 80 | + * @param string $table |
| 81 | + * @param string $keyField |
151 | 82 | * @return array |
152 | 83 | */ |
153 | | - private function getGroupsData($id = null) |
| 84 | + private function getEntities($table, $keyField) |
154 | 85 | { |
155 | | - if ($id !== null) { |
156 | | - $group = $this->groupFactory->create(); |
157 | | - $group->load($id); |
158 | | - $data[$id] = $group->getData(); |
159 | | - } else { |
160 | | - $collection = $this->groupCollectionFactory->create(); |
161 | | - $collection->setLoadDefault(true); |
162 | | - $data = []; |
163 | | - /** @var Group $group */ |
164 | | - foreach ($collection as $group) { |
165 | | - $data[$group->getId()] = $group->getData(); |
166 | | - } |
| 86 | + $entities = $this->getConnection()->fetchAll( |
| 87 | + $this->getConnection()->select()->from($this->resourceConnection->getTableName($table)) |
| 88 | + ); |
| 89 | + $data = []; |
| 90 | + foreach ($entities as $entity) { |
| 91 | + $data[$entity[$keyField]] = $entity; |
167 | 92 | } |
168 | | - return $data; |
169 | | - } |
170 | | - |
171 | | - /** |
172 | | - * @param string|null $code |
173 | | - * @return array |
174 | | - */ |
175 | | - private function getStoresData($code = null) |
176 | | - { |
177 | | - if ($code !== null) { |
178 | | - $store = $this->storeFactory->create(); |
179 | 93 |
|
180 | | - if (is_numeric($code)) { |
181 | | - $store->load($code); |
182 | | - } else { |
183 | | - $store->load($code, 'code'); |
184 | | - } |
185 | | - |
186 | | - $data[$code] = $store->getData(); |
187 | | - } else { |
188 | | - $collection = $this->storeCollectionFactory->create(); |
189 | | - $collection->setLoadDefault(true); |
190 | | - $data = []; |
191 | | - /** @var Store $store */ |
192 | | - foreach ($collection as $store) { |
193 | | - $data[$store->getCode()] = $store->getData(); |
194 | | - } |
195 | | - return $data; |
196 | | - } |
197 | 94 | return $data; |
198 | 95 | } |
199 | 96 |
|
|
0 commit comments