Fallback.php 5.2 KB

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