Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Config.php
Go to the documentation of this file.
1 <?php
10 namespace Magento\Theme\Model;
11 
13 
14 class Config
15 {
19  protected $_configWriter;
20 
24  protected $_configData;
25 
29  protected $_storeManager;
30 
36  protected $_eventManager;
37 
41  protected $_configCache;
42 
46  protected $_layoutCache;
47 
56  public function __construct(
58  \Magento\Framework\App\Config\Storage\WriterInterface $configWriter,
60  \Magento\Framework\Event\ManagerInterface $eventManager,
61  \Magento\Framework\Cache\FrontendInterface $configCache,
62  \Magento\Framework\Cache\FrontendInterface $layoutCache
63  ) {
64  $this->_configData = $configData;
65  $this->_configWriter = $configWriter;
66  $this->_storeManager = $storeManager;
67  $this->_eventManager = $eventManager;
68  $this->_configCache = $configCache;
69  $this->_layoutCache = $layoutCache;
70  }
71 
80  public function assignToStore(
81  $theme,
82  array $stores = [],
83  $scope = \Magento\Store\Model\ScopeInterface::SCOPE_STORES
84  ) {
85  $isReassigned = false;
86 
87  $this->_unassignThemeFromStores($theme->getId(), $stores, $scope, $isReassigned);
88 
89  if ($this->_storeManager->isSingleStoreMode()) {
90  $this->_assignThemeToDefaultScope($theme->getId(), $isReassigned);
91  } else {
92  $this->_assignThemeToStores($theme->getId(), $stores, $scope, $isReassigned);
93  }
94 
95  if ($isReassigned) {
96  $this->_configCache->clean();
97  $this->_layoutCache->clean();
98  }
99 
100  $this->_eventManager->dispatch(
101  'assign_theme_to_stores_after',
102  ['stores' => $stores, 'scope' => $scope, 'theme' => $theme]
103  );
104 
105  return $this;
106  }
107 
115  protected function _getAssignedScopesCollection($scope, $configPath)
116  {
117  return $this->_configData->getCollection()->addFieldToFilter(
118  'scope',
119  $scope
120  )->addFieldToFilter(
121  'path',
122  $configPath
123  );
124  }
125 
135  protected function _unassignThemeFromStores($themeId, $stores, $scope, &$isReassigned)
136  {
138  foreach ($this->_getAssignedScopesCollection($scope, $configPath) as $config) {
139  if ($config->getValue() == $themeId && !in_array($config->getScopeId(), $stores)) {
140  $this->_configWriter->delete($configPath, $scope, $config->getScopeId());
141  $isReassigned = true;
142  }
143  }
144  return $this;
145  }
146 
156  protected function _assignThemeToStores($themeId, $stores, $scope, &$isReassigned)
157  {
159  if (count($stores) > 0) {
160  foreach ($stores as $storeId) {
161  $this->_configWriter->save($configPath, $themeId, $scope, $storeId);
162  $isReassigned = true;
163  }
164  }
165  return $this;
166  }
167 
175  protected function _assignThemeToDefaultScope($themeId, &$isReassigned)
176  {
178  $this->_configWriter->save($configPath, $themeId, ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
179  $isReassigned = true;
180  return $this;
181  }
182 }
$config
Definition: fraud_order.php:17
$layoutCache
Definition: layout_cache.php:8
$storeManager
_getAssignedScopesCollection($scope, $configPath)
Definition: Config.php:115
_unassignThemeFromStores($themeId, $stores, $scope, &$isReassigned)
Definition: Config.php:135
_assignThemeToStores($themeId, $stores, $scope, &$isReassigned)
Definition: Config.php:156
$theme
assignToStore( $theme, array $stores=[], $scope=\Magento\Store\Model\ScopeInterface::SCOPE_STORES)
Definition: Config.php:80
_assignThemeToDefaultScope($themeId, &$isReassigned)
Definition: Config.php:175
__construct(\Magento\Framework\App\Config\ValueInterface $configData, \Magento\Framework\App\Config\Storage\WriterInterface $configWriter, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\Cache\FrontendInterface $configCache, \Magento\Framework\Cache\FrontendInterface $layoutCache)
Definition: Config.php:56