Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
StoreManager Class Reference
Inheritance diagram for StoreManager:
StoreManagerInterface StoreWebsiteRelationInterface

Public Member Functions

 __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)
 
 setCurrentStore ($store)
 
 setIsSingleStoreModeAllowed ($value)
 
 hasSingleStore ()
 
 isSingleStoreMode ()
 
 getStore ($storeId=null)
 
 getStores ($withDefault=false, $codeKey=false)
 
 getWebsite ($websiteId=null)
 
 getWebsites ($withDefault=false, $codeKey=false)
 
 reinitStores ()
 
 getDefaultStoreView ()
 
 getGroup ($groupId=null)
 
 getGroups ($withDefault=false)
 
 getStoreByWebsiteId ($websiteId)
 

Data Fields

const PARAM_RUN_CODE = 'MAGE_RUN_CODE'
 
const PARAM_RUN_TYPE = 'MAGE_RUN_TYPE'
 
const XML_PATH_SINGLE_STORE_MODE_ENABLED = 'general/single_store_mode/enabled'
 
- Data Fields inherited from StoreManagerInterface
const CONTEXT_STORE = 'store'
 
const PARAM_NAME = '___store'
 

Protected Member Functions

 isSingleStoreModeEnabled ()
 

Protected Attributes

 $storeRepository
 
 $groupRepository
 
 $websiteRepository
 
 $scopeConfig
 
 $storeResolver
 
 $cache
 
 $currentStoreId = null
 
 $_hasSingleStore
 
 $isSingleStoreAllowed
 

Detailed Description

Service contract, which manage scopes

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 17 of file StoreManager.php.

Constructor & Destructor Documentation

◆ __construct()

__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 
)
Parameters
\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
bool$isSingleStoreAllowed

Definition at line 98 of file StoreManager.php.

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  }

Member Function Documentation

◆ getDefaultStoreView()

getDefaultStoreView ( )

{Retrieve default store for default group and website

Returns
\Magento\Store\Api\Data\StoreInterface|null
}

Implements StoreManagerInterface.

Definition at line 247 of file StoreManager.php.

248  {
249  $defaultWebsite = $this->websiteRepository->getDefault();
250  $defaultStore = $this->getGroup($defaultWebsite->getDefaultGroupId())->getDefaultStore();
251  return $defaultStore ?: null;
252  }

◆ getGroup()

getGroup (   $groupId = null)

{Retrieve application store group object

Parameters
null | \Magento\Store\Api\Data\GroupInterface | string$groupId
Returns
\Magento\Store\Api\Data\GroupInterface
}

Implements StoreManagerInterface.

Definition at line 257 of file StoreManager.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$group
Definition: sections.phtml:16

◆ getGroups()

getGroups (   $withDefault = false)

{Prepare array of store groups

Parameters
bool$withDefault
Returns
\Magento\Store\Api\Data\GroupInterface[]
}

Implements StoreManagerInterface.

Definition at line 272 of file StoreManager.php.

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  }

◆ getStore()

getStore (   $storeId = null)

{Retrieve application store object

Parameters
null | string | bool | int | \Magento\Store\Api\Data\StoreInterface$storeId
Returns
\Magento\Store\Api\Data\StoreInterface
Exceptions
NoSuchEntityExceptionIf given store doesn't exist.
}

Implements StoreManagerInterface.

Definition at line 152 of file StoreManager.php.

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  }

◆ getStoreByWebsiteId()

getStoreByWebsiteId (   $websiteId)

Get assigned to website store

Parameters
int$websiteId
Returns
array
Since
100.2.0

Implements StoreWebsiteRelationInterface.

Definition at line 312 of file StoreManager.php.

313  {
314  return $this->getStoreWebsiteRelation()->getStoreByWebsiteId($websiteId);
315  }

◆ getStores()

getStores (   $withDefault = false,
  $codeKey = false 
)

{Retrieve stores array

Parameters
bool$withDefault
bool$codeKey
Returns
\Magento\Store\Api\Data\StoreInterface[]
}

Implements StoreManagerInterface.

Definition at line 176 of file StoreManager.php.

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  }

◆ getWebsite()

getWebsite (   $websiteId = null)

{Retrieve application website object

Parameters
null | bool | int | string | \Magento\Store\Api\Data\WebsiteInterface$websiteId
Returns
\Magento\Store\Api\Data\WebsiteInterface
Exceptions
}

Implements StoreManagerInterface.

Definition at line 195 of file StoreManager.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ getWebsites()

getWebsites (   $withDefault = false,
  $codeKey = false 
)

{Get loaded websites

Parameters
bool$withDefault
bool$codeKey
Returns
\Magento\Store\Api\Data\WebsiteInterface[]
}

Implements StoreManagerInterface.

Definition at line 215 of file StoreManager.php.

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  }

◆ hasSingleStore()

hasSingleStore ( )

{Check if store has only one store view

Returns
bool
}

Implements StoreManagerInterface.

Definition at line 135 of file StoreManager.php.

136  {
137  // TODO: MAGETWO-39902 add cache, move value to consts
138  return $this->isSingleStoreAllowed && count($this->getStores(true)) < 3;
139  }
getStores($withDefault=false, $codeKey=false)

◆ isSingleStoreMode()

isSingleStoreMode ( )

{Check if system is run in the single store mode

Returns
bool
}

Implements StoreManagerInterface.

Definition at line 144 of file StoreManager.php.

◆ isSingleStoreModeEnabled()

isSingleStoreModeEnabled ( )
protected

Check if Single-Store mode is enabled in configuration

This flag only shows that admin does not want to show certain UI components at backend (like store switchers etc) if Magento has only one store view but it does not check the store view collection

Returns
bool

Definition at line 292 of file StoreManager.php.

293  {
294  return (bool)$this->scopeConfig->getValue(
295  self::XML_PATH_SINGLE_STORE_MODE_ENABLED,
297  );
298  }

◆ reinitStores()

reinitStores ( )

{Reinitialize store list

Returns
void
}

Implements StoreManagerInterface.

Definition at line 234 of file StoreManager.php.

235  {
236  $this->currentStoreId = null;
238  $this->scopeConfig->clean();
239  $this->storeRepository->clean();
240  $this->websiteRepository->clean();
241  $this->groupRepository->clean();
242  }
const CLEANING_MODE_MATCHING_ANY_TAG
Definition: Cache.php:76

◆ setCurrentStore()

setCurrentStore (   $store)

{Set current default store

Parameters
string$store
Returns
void
}

Implements StoreManagerInterface.

Definition at line 119 of file StoreManager.php.

120  {
121  $this->currentStoreId = $store;
122  }

◆ setIsSingleStoreModeAllowed()

setIsSingleStoreModeAllowed (   $value)

{Allow or disallow single store mode

Parameters
bool$value
Returns
void
}

Implements StoreManagerInterface.

Definition at line 127 of file StoreManager.php.

128  {
129  $this->isSingleStoreAllowed = $value;
130  }
$value
Definition: gender.phtml:16

Field Documentation

◆ $_hasSingleStore

$_hasSingleStore
protected

Definition at line 80 of file StoreManager.php.

◆ $cache

$cache
protected

Definition at line 66 of file StoreManager.php.

◆ $currentStoreId

$currentStoreId = null
protected

Definition at line 73 of file StoreManager.php.

◆ $groupRepository

$groupRepository
protected

Definition at line 44 of file StoreManager.php.

◆ $isSingleStoreAllowed

$isSingleStoreAllowed
protected

Definition at line 87 of file StoreManager.php.

◆ $scopeConfig

$scopeConfig
protected

Definition at line 56 of file StoreManager.php.

◆ $storeRepository

$storeRepository
protected

Definition at line 39 of file StoreManager.php.

◆ $storeResolver

$storeResolver
protected

Definition at line 61 of file StoreManager.php.

◆ $websiteRepository

$websiteRepository
protected

Definition at line 49 of file StoreManager.php.

◆ PARAM_RUN_CODE

const PARAM_RUN_CODE = 'MAGE_RUN_CODE'

Application run code

Definition at line 24 of file StoreManager.php.

◆ PARAM_RUN_TYPE

const PARAM_RUN_TYPE = 'MAGE_RUN_TYPE'

Application run type (store|website)

Definition at line 29 of file StoreManager.php.

◆ XML_PATH_SINGLE_STORE_MODE_ENABLED

const XML_PATH_SINGLE_STORE_MODE_ENABLED = 'general/single_store_mode/enabled'

Whether single store mode enabled or not

Definition at line 34 of file StoreManager.php.


The documentation for this class was generated from the following file: