Config.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Theme Config model
  8. */
  9. namespace Magento\Theme\Model;
  10. use Magento\Framework\App\Config\ScopeConfigInterface;
  11. class Config
  12. {
  13. /**
  14. * @var \Magento\Framework\App\Config\Storage\WriterInterface
  15. */
  16. protected $_configWriter;
  17. /**
  18. * @var \Magento\Framework\App\Config\ValueInterface
  19. */
  20. protected $_configData;
  21. /**
  22. * @var \Magento\Store\Model\StoreManagerInterface
  23. */
  24. protected $_storeManager;
  25. /**
  26. * Application event manager
  27. *
  28. * @var \Magento\Framework\Event\ManagerInterface
  29. */
  30. protected $_eventManager;
  31. /**
  32. * @var \Magento\Framework\Cache\FrontendInterface
  33. */
  34. protected $_configCache;
  35. /**
  36. * @var \Magento\Framework\Cache\FrontendInterface
  37. */
  38. protected $_layoutCache;
  39. /**
  40. * @param \Magento\Framework\App\Config\ValueInterface $configData
  41. * @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
  42. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  43. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  44. * @param \Magento\Framework\Cache\FrontendInterface $configCache
  45. * @param \Magento\Framework\Cache\FrontendInterface $layoutCache
  46. */
  47. public function __construct(
  48. \Magento\Framework\App\Config\ValueInterface $configData,
  49. \Magento\Framework\App\Config\Storage\WriterInterface $configWriter,
  50. \Magento\Store\Model\StoreManagerInterface $storeManager,
  51. \Magento\Framework\Event\ManagerInterface $eventManager,
  52. \Magento\Framework\Cache\FrontendInterface $configCache,
  53. \Magento\Framework\Cache\FrontendInterface $layoutCache
  54. ) {
  55. $this->_configData = $configData;
  56. $this->_configWriter = $configWriter;
  57. $this->_storeManager = $storeManager;
  58. $this->_eventManager = $eventManager;
  59. $this->_configCache = $configCache;
  60. $this->_layoutCache = $layoutCache;
  61. }
  62. /**
  63. * Assign theme to the stores
  64. *
  65. * @param \Magento\Framework\View\Design\ThemeInterface $theme
  66. * @param array $stores
  67. * @param string $scope
  68. * @return $this
  69. */
  70. public function assignToStore(
  71. $theme,
  72. array $stores = [],
  73. $scope = \Magento\Store\Model\ScopeInterface::SCOPE_STORES
  74. ) {
  75. $isReassigned = false;
  76. $this->_unassignThemeFromStores($theme->getId(), $stores, $scope, $isReassigned);
  77. if ($this->_storeManager->isSingleStoreMode()) {
  78. $this->_assignThemeToDefaultScope($theme->getId(), $isReassigned);
  79. } else {
  80. $this->_assignThemeToStores($theme->getId(), $stores, $scope, $isReassigned);
  81. }
  82. if ($isReassigned) {
  83. $this->_configCache->clean();
  84. $this->_layoutCache->clean();
  85. }
  86. $this->_eventManager->dispatch(
  87. 'assign_theme_to_stores_after',
  88. ['stores' => $stores, 'scope' => $scope, 'theme' => $theme]
  89. );
  90. return $this;
  91. }
  92. /**
  93. * Get assigned scopes collection of a theme
  94. *
  95. * @param string $scope
  96. * @param string $configPath
  97. * @return \Magento\Config\Model\ResourceModel\Config\Data\Collection
  98. */
  99. protected function _getAssignedScopesCollection($scope, $configPath)
  100. {
  101. return $this->_configData->getCollection()->addFieldToFilter(
  102. 'scope',
  103. $scope
  104. )->addFieldToFilter(
  105. 'path',
  106. $configPath
  107. );
  108. }
  109. /**
  110. * Unassign given theme from stores that were unchecked
  111. *
  112. * @param string $themeId
  113. * @param array $stores
  114. * @param string $scope
  115. * @param bool &$isReassigned
  116. * @return $this
  117. */
  118. protected function _unassignThemeFromStores($themeId, $stores, $scope, &$isReassigned)
  119. {
  120. $configPath = \Magento\Framework\View\DesignInterface::XML_PATH_THEME_ID;
  121. foreach ($this->_getAssignedScopesCollection($scope, $configPath) as $config) {
  122. if ($config->getValue() == $themeId && !in_array($config->getScopeId(), $stores)) {
  123. $this->_configWriter->delete($configPath, $scope, $config->getScopeId());
  124. $isReassigned = true;
  125. }
  126. }
  127. return $this;
  128. }
  129. /**
  130. * Assign given theme to stores
  131. *
  132. * @param string $themeId
  133. * @param array $stores
  134. * @param string $scope
  135. * @param bool &$isReassigned
  136. * @return $this
  137. */
  138. protected function _assignThemeToStores($themeId, $stores, $scope, &$isReassigned)
  139. {
  140. $configPath = \Magento\Framework\View\DesignInterface::XML_PATH_THEME_ID;
  141. if (count($stores) > 0) {
  142. foreach ($stores as $storeId) {
  143. $this->_configWriter->save($configPath, $themeId, $scope, $storeId);
  144. $isReassigned = true;
  145. }
  146. }
  147. return $this;
  148. }
  149. /**
  150. * Assign theme to default scope
  151. *
  152. * @param string $themeId
  153. * @param bool &$isReassigned
  154. * @return $this
  155. */
  156. protected function _assignThemeToDefaultScope($themeId, &$isReassigned)
  157. {
  158. $configPath = \Magento\Framework\View\DesignInterface::XML_PATH_THEME_ID;
  159. $this->_configWriter->save($configPath, $themeId, ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
  160. $isReassigned = true;
  161. return $this;
  162. }
  163. }