Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Fallback.php
Go to the documentation of this file.
1 <?php
7 
15 use Magento\Store\Model\ResourceModel\Store\AllStoresCollectionFactory;
17 use Magento\Store\Model\ResourceModel\Website\AllWebsitesCollection;
18 use Magento\Store\Model\ResourceModel\Website\AllWebsitesCollectionFactory;
19 
24 {
28  private $scopes;
29 
33  private $resourceConnection;
34 
38  private $storeData = [];
39 
43  private $websiteData = [];
44 
48  private $storeResource;
49 
53  private $websiteResource;
54 
58  private $deploymentConfig;
59 
69  public function __construct(
70  Scopes $scopes,
71  ResourceConnection $resourceConnection,
72  Store $storeResource,
73  Website $websiteResource,
74  DeploymentConfig $deploymentConfig
75  ) {
76  $this->scopes = $scopes;
77  $this->resourceConnection = $resourceConnection;
78  $this->storeResource = $storeResource;
79  $this->websiteResource = $websiteResource;
80  $this->deploymentConfig = $deploymentConfig;
81  }
82 
86  public function process(array $data)
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 
96  $defaultConfig = isset($data['default']) ? $data['default'] : [];
97  $result = [
98  'default' => $defaultConfig,
99  'websites' => [],
100  'stores' => []
101  ];
102 
103  $websitesConfig = isset($data['websites']) ? $data['websites'] : [];
104  $result['websites'] = $this->prepareWebsitesConfig($defaultConfig, $websitesConfig);
105 
106  $storesConfig = isset($data['stores']) ? $data['stores'] : [];
107  $result['stores'] = $this->prepareStoresConfig($defaultConfig, $websitesConfig, $storesConfig);
108 
109  return $result;
110  }
111 
119  private function prepareWebsitesConfig(
120  array $defaultConfig,
121  array $websitesConfig
122  ) {
123  $result = [];
124  foreach ((array)$this->websiteData as $website) {
125  $code = $website['code'];
126  $id = $website['website_id'];
127  $websiteConfig = isset($websitesConfig[$code]) ? $websitesConfig[$code] : [];
128  $result[$code] = array_replace_recursive($defaultConfig, $websiteConfig);
129  $result[$id] = $result[$code];
130  }
131  return $result;
132  }
133 
142  private function prepareStoresConfig(
143  array $defaultConfig,
144  array $websitesConfig,
145  array $storesConfig
146  ) {
147  $result = [];
148 
149  foreach ((array)$this->storeData as $store) {
150  $code = $store['code'];
151  $id = $store['store_id'];
152  $websiteConfig = [];
153  if (isset($store['website_id'])) {
154  $websiteConfig = $this->getWebsiteConfig($websitesConfig, $store['website_id']);
155  }
156  $storeConfig = isset($storesConfig[$code]) ? $storesConfig[$code] : [];
157  $result[$code] = array_replace_recursive($defaultConfig, $websiteConfig, $storeConfig);
158  $result[$id] = $result[$code];
159  }
160  return $result;
161  }
162 
170  private function getWebsiteConfig(array $websites, $id)
171  {
172  foreach ((array)$this->websiteData as $website) {
173  if ($website['website_id'] == $id) {
174  $code = $website['code'];
175  return isset($websites[$code]) ? $websites[$code] : [];
176  }
177  }
178  return [];
179  }
180 }
$id
Definition: fieldset.phtml:14
__construct(Scopes $scopes, ResourceConnection $resourceConnection, Store $storeResource, Website $websiteResource, DeploymentConfig $deploymentConfig)
Definition: Fallback.php:69
$deploymentConfig
$code
Definition: info.phtml:12