StoreManager.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Model;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Store\Api\StoreResolverInterface;
  10. use Magento\Store\Model\ResourceModel\StoreWebsiteRelation;
  11. /**
  12. * Service contract, which manage scopes
  13. *
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class StoreManager implements
  17. \Magento\Store\Model\StoreManagerInterface,
  18. \Magento\Store\Api\StoreWebsiteRelationInterface
  19. {
  20. /**
  21. * Application run code
  22. */
  23. const PARAM_RUN_CODE = 'MAGE_RUN_CODE';
  24. /**
  25. * Application run type (store|website)
  26. */
  27. const PARAM_RUN_TYPE = 'MAGE_RUN_TYPE';
  28. /**
  29. * Whether single store mode enabled or not
  30. */
  31. const XML_PATH_SINGLE_STORE_MODE_ENABLED = 'general/single_store_mode/enabled';
  32. /**
  33. * @var \Magento\Store\Api\StoreRepositoryInterface
  34. */
  35. protected $storeRepository;
  36. /**
  37. * @var \Magento\Store\Api\GroupRepositoryInterface
  38. */
  39. protected $groupRepository;
  40. /**
  41. * @var \Magento\Store\Api\WebsiteRepositoryInterface
  42. */
  43. protected $websiteRepository;
  44. /**
  45. * Scope config
  46. *
  47. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  48. */
  49. protected $scopeConfig;
  50. /**
  51. * @var StoreResolverInterface
  52. */
  53. protected $storeResolver;
  54. /**
  55. * @var \Magento\Framework\Cache\FrontendInterface
  56. */
  57. protected $cache;
  58. /**
  59. * Default store code
  60. *
  61. * @var string
  62. */
  63. protected $currentStoreId = null;
  64. /**
  65. * Flag that shows that system has only one store view
  66. *
  67. * @var bool
  68. */
  69. protected $_hasSingleStore;
  70. /**
  71. * Flag is single store mode allowed
  72. *
  73. * @var bool
  74. */
  75. protected $isSingleStoreAllowed;
  76. /**
  77. * StoreManager constructor.
  78. *
  79. * @param \Magento\Store\Api\StoreRepositoryInterface $storeRepository
  80. * @param \Magento\Store\Api\GroupRepositoryInterface $groupRepository
  81. * @param \Magento\Store\Api\WebsiteRepositoryInterface $websiteRepository
  82. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  83. * @param StoreResolverInterface $storeResolver
  84. * @param \Magento\Framework\Cache\FrontendInterface $cache
  85. * @param bool $isSingleStoreAllowed
  86. */
  87. public function __construct(
  88. \Magento\Store\Api\StoreRepositoryInterface $storeRepository,
  89. \Magento\Store\Api\GroupRepositoryInterface $groupRepository,
  90. \Magento\Store\Api\WebsiteRepositoryInterface $websiteRepository,
  91. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  92. StoreResolverInterface $storeResolver,
  93. \Magento\Framework\Cache\FrontendInterface $cache,
  94. $isSingleStoreAllowed = true
  95. ) {
  96. $this->storeRepository = $storeRepository;
  97. $this->websiteRepository = $websiteRepository;
  98. $this->groupRepository = $groupRepository;
  99. $this->scopeConfig = $scopeConfig;
  100. $this->storeResolver = $storeResolver;
  101. $this->cache = $cache;
  102. $this->isSingleStoreAllowed = $isSingleStoreAllowed;
  103. }
  104. /**
  105. * @inheritdoc
  106. */
  107. public function setCurrentStore($store)
  108. {
  109. $this->currentStoreId = $store;
  110. }
  111. /**
  112. * @inheritdoc
  113. */
  114. public function setIsSingleStoreModeAllowed($value)
  115. {
  116. $this->isSingleStoreAllowed = $value;
  117. }
  118. /**
  119. * @inheritdoc
  120. */
  121. public function hasSingleStore()
  122. {
  123. // TODO: MAGETWO-39902 add cache, move value to consts
  124. return $this->isSingleStoreAllowed && count($this->getStores(true)) < 3;
  125. }
  126. /**
  127. * @inheritdoc
  128. */
  129. public function isSingleStoreMode()
  130. {
  131. return $this->isSingleStoreModeEnabled() && $this->hasSingleStore();
  132. }
  133. /**
  134. * @inheritdoc
  135. */
  136. public function getStore($storeId = null)
  137. {
  138. if (!isset($storeId) || '' === $storeId || $storeId === true) {
  139. if (null === $this->currentStoreId) {
  140. \Magento\Framework\Profiler::start('store.resolve');
  141. $this->currentStoreId = $this->storeResolver->getCurrentStoreId();
  142. \Magento\Framework\Profiler::stop('store.resolve');
  143. }
  144. $storeId = $this->currentStoreId;
  145. }
  146. if ($storeId instanceof \Magento\Store\Api\Data\StoreInterface) {
  147. return $storeId;
  148. }
  149. $store = is_numeric($storeId)
  150. ? $this->storeRepository->getById($storeId)
  151. : $this->storeRepository->get($storeId);
  152. return $store;
  153. }
  154. /**
  155. * @inheritdoc
  156. */
  157. public function getStores($withDefault = false, $codeKey = false)
  158. {
  159. $stores = [];
  160. foreach ($this->storeRepository->getList() as $store) {
  161. if (!$withDefault && $store->getId() == 0) {
  162. continue;
  163. }
  164. if ($codeKey) {
  165. $stores[$store->getCode()] = $store;
  166. } else {
  167. $stores[$store->getId()] = $store;
  168. }
  169. }
  170. return $stores;
  171. }
  172. /**
  173. * @inheritdoc
  174. */
  175. public function getWebsite($websiteId = null)
  176. {
  177. if ($websiteId === null || $websiteId === '') {
  178. $website = $this->websiteRepository->getById($this->getStore()->getWebsiteId());
  179. } elseif ($websiteId instanceof Website) {
  180. $website = $websiteId;
  181. } elseif ($websiteId === true) {
  182. $website = $this->websiteRepository->getDefault();
  183. } elseif (is_numeric($websiteId)) {
  184. $website = $this->websiteRepository->getById($websiteId);
  185. } else {
  186. $website = $this->websiteRepository->get($websiteId);
  187. }
  188. return $website;
  189. }
  190. /**
  191. * @inheritdoc
  192. */
  193. public function getWebsites($withDefault = false, $codeKey = false)
  194. {
  195. $websites = [];
  196. foreach ($this->websiteRepository->getList() as $website) {
  197. if (!$withDefault && $website->getId() == 0) {
  198. continue;
  199. }
  200. if ($codeKey) {
  201. $websites[$website->getCode()] = $website;
  202. } else {
  203. $websites[$website->getId()] = $website;
  204. }
  205. }
  206. return $websites;
  207. }
  208. /**
  209. * @inheritdoc
  210. */
  211. public function reinitStores()
  212. {
  213. $this->currentStoreId = null;
  214. $this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, [StoreResolver::CACHE_TAG, Store::CACHE_TAG]);
  215. $this->scopeConfig->clean();
  216. $this->storeRepository->clean();
  217. $this->websiteRepository->clean();
  218. $this->groupRepository->clean();
  219. }
  220. /**
  221. * @inheritdoc
  222. */
  223. public function getDefaultStoreView()
  224. {
  225. $defaultWebsite = $this->websiteRepository->getDefault();
  226. $defaultStore = $this->getGroup($defaultWebsite->getDefaultGroupId())->getDefaultStore();
  227. return $defaultStore ?: null;
  228. }
  229. /**
  230. * @inheritdoc
  231. */
  232. public function getGroup($groupId = null)
  233. {
  234. if (null === $groupId) {
  235. $group = $this->groupRepository->get($this->getStore()->getGroupId());
  236. } elseif ($groupId instanceof \Magento\Store\Api\Data\GroupInterface) {
  237. $group = $groupId;
  238. } else {
  239. $group = $this->groupRepository->get($groupId);
  240. }
  241. return $group;
  242. }
  243. /**
  244. * @inheritdoc
  245. */
  246. public function getGroups($withDefault = false)
  247. {
  248. $groups = $this->groupRepository->getList();
  249. return $withDefault ? $groups : array_filter(
  250. $groups,
  251. function ($item) {
  252. return $item->getId() != 0;
  253. }
  254. );
  255. }
  256. /**
  257. * Check if Single-Store mode is enabled in configuration
  258. *
  259. * This flag only shows that admin does not want to show certain UI components at backend (like store switchers etc)
  260. * if Magento has only one store view but it does not check the store view collection
  261. *
  262. * @return bool
  263. */
  264. protected function isSingleStoreModeEnabled()
  265. {
  266. return $this->scopeConfig->isSetFlag(
  267. self::XML_PATH_SINGLE_STORE_MODE_ENABLED,
  268. ScopeInterface::SCOPE_STORE
  269. );
  270. }
  271. /**
  272. * Get Store Website Relation
  273. *
  274. * @deprecated 100.2.0
  275. * @return StoreWebsiteRelation
  276. */
  277. private function getStoreWebsiteRelation()
  278. {
  279. return ObjectManager::getInstance()->get(StoreWebsiteRelation::class);
  280. }
  281. /**
  282. * @inheritdoc
  283. */
  284. public function getStoreByWebsiteId($websiteId)
  285. {
  286. return $this->getStoreWebsiteRelation()->getStoreByWebsiteId($websiteId);
  287. }
  288. }