Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StoreManager.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Store\Model;
7 
11 
17 class StoreManager implements
20 {
24  const PARAM_RUN_CODE = 'MAGE_RUN_CODE';
25 
29  const PARAM_RUN_TYPE = 'MAGE_RUN_TYPE';
30 
34  const XML_PATH_SINGLE_STORE_MODE_ENABLED = 'general/single_store_mode/enabled';
35 
39  protected $storeRepository;
40 
44  protected $groupRepository;
45 
49  protected $websiteRepository;
50 
56  protected $scopeConfig;
57 
61  protected $storeResolver;
62 
66  protected $cache;
67 
73  protected $currentStoreId = null;
74 
80  protected $_hasSingleStore;
81 
88 
98  public function __construct(
99  \Magento\Store\Api\StoreRepositoryInterface $storeRepository,
100  \Magento\Store\Api\GroupRepositoryInterface $groupRepository,
101  \Magento\Store\Api\WebsiteRepositoryInterface $websiteRepository,
104  \Magento\Framework\Cache\FrontendInterface $cache,
105  $isSingleStoreAllowed = true
106  ) {
107  $this->storeRepository = $storeRepository;
108  $this->websiteRepository = $websiteRepository;
109  $this->groupRepository = $groupRepository;
110  $this->scopeConfig = $scopeConfig;
111  $this->storeResolver = $storeResolver;
112  $this->cache = $cache;
113  $this->isSingleStoreAllowed = $isSingleStoreAllowed;
114  }
115 
119  public function setCurrentStore($store)
120  {
121  $this->currentStoreId = $store;
122  }
123 
128  {
129  $this->isSingleStoreAllowed = $value;
130  }
131 
135  public function hasSingleStore()
136  {
137  // TODO: MAGETWO-39902 add cache, move value to consts
138  return $this->isSingleStoreAllowed && count($this->getStores(true)) < 3;
139  }
140 
144  public function isSingleStoreMode()
145  {
146  return $this->isSingleStoreModeEnabled() && $this->hasSingleStore();
147  }
148 
152  public function getStore($storeId = null)
153  {
154  if (!isset($storeId) || '' === $storeId || $storeId === true) {
155  if (null === $this->currentStoreId) {
156  \Magento\Framework\Profiler::start('store.resolve');
157  $this->currentStoreId = $this->storeResolver->getCurrentStoreId();
158  \Magento\Framework\Profiler::stop('store.resolve');
159  }
161  }
162  if ($storeId instanceof \Magento\Store\Api\Data\StoreInterface) {
163  return $storeId;
164  }
165 
166  $store = is_numeric($storeId)
167  ? $this->storeRepository->getById($storeId)
168  : $this->storeRepository->get($storeId);
169 
170  return $store;
171  }
172 
176  public function getStores($withDefault = false, $codeKey = false)
177  {
178  $stores = [];
179  foreach ($this->storeRepository->getList() as $store) {
180  if (!$withDefault && $store->getId() == 0) {
181  continue;
182  }
183  if ($codeKey) {
184  $stores[$store->getCode()] = $store;
185  } else {
186  $stores[$store->getId()] = $store;
187  }
188  }
189  return $stores;
190  }
191 
195  public function getWebsite($websiteId = null)
196  {
197  if ($websiteId === null || $websiteId === '') {
198  $website = $this->websiteRepository->getById($this->getStore()->getWebsiteId());
199  } elseif ($websiteId instanceof Website) {
201  } elseif ($websiteId === true) {
202  $website = $this->websiteRepository->getDefault();
203  } elseif (is_numeric($websiteId)) {
204  $website = $this->websiteRepository->getById($websiteId);
205  } else {
206  $website = $this->websiteRepository->get($websiteId);
207  }
208 
209  return $website;
210  }
211 
215  public function getWebsites($withDefault = false, $codeKey = false)
216  {
217  $websites = [];
218  foreach ($this->websiteRepository->getList() as $website) {
219  if (!$withDefault && $website->getId() == 0) {
220  continue;
221  }
222  if ($codeKey) {
223  $websites[$website->getCode()] = $website;
224  } else {
225  $websites[$website->getId()] = $website;
226  }
227  }
228  return $websites;
229  }
230 
234  public function reinitStores()
235  {
236  $this->currentStoreId = null;
238  $this->scopeConfig->clean();
239  $this->storeRepository->clean();
240  $this->websiteRepository->clean();
241  $this->groupRepository->clean();
242  }
243 
247  public function getDefaultStoreView()
248  {
249  $defaultWebsite = $this->websiteRepository->getDefault();
250  $defaultStore = $this->getGroup($defaultWebsite->getDefaultGroupId())->getDefaultStore();
251  return $defaultStore ?: null;
252  }
253 
257  public function getGroup($groupId = null)
258  {
259  if (null === $groupId) {
260  $group = $this->groupRepository->get($this->getStore()->getGroupId());
261  } elseif ($groupId instanceof \Magento\Store\Api\Data\GroupInterface) {
262  $group = $groupId;
263  } else {
264  $group = $this->groupRepository->get($groupId);
265  }
266  return $group;
267  }
268 
272  public function getGroups($withDefault = false)
273  {
274  $groups = $this->groupRepository->getList();
275 
276  return $withDefault ? $groups : array_filter(
277  $groups,
278  function ($item) {
279  return $item->getId() != 0;
280  }
281  );
282  }
283 
292  protected function isSingleStoreModeEnabled()
293  {
294  return (bool)$this->scopeConfig->getValue(
295  self::XML_PATH_SINGLE_STORE_MODE_ENABLED,
297  );
298  }
299 
304  private function getStoreWebsiteRelation()
305  {
306  return ObjectManager::getInstance()->get(StoreWebsiteRelation::class);
307  }
308 
313  {
314  return $this->getStoreWebsiteRelation()->getStoreByWebsiteId($websiteId);
315  }
316 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$group
Definition: sections.phtml:16
__construct(\Magento\Store\Api\StoreRepositoryInterface $storeRepository, \Magento\Store\Api\GroupRepositoryInterface $groupRepository, \Magento\Store\Api\WebsiteRepositoryInterface $websiteRepository, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, StoreResolverInterface $storeResolver, \Magento\Framework\Cache\FrontendInterface $cache, $isSingleStoreAllowed=true)
$value
Definition: gender.phtml:16
const CLEANING_MODE_MATCHING_ANY_TAG
Definition: Cache.php:76
getWebsites($withDefault=false, $codeKey=false)
getStores($withDefault=false, $codeKey=false)