Design.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\View;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Store\Model\ScopeInterface;
  9. /**
  10. * Keeps design settings for current request
  11. */
  12. class Design implements \Magento\Framework\View\DesignInterface
  13. {
  14. /**
  15. * Package area
  16. *
  17. * @var string
  18. */
  19. protected $_area;
  20. /**
  21. * Package theme
  22. *
  23. * @var \Magento\Theme\Model\Theme
  24. */
  25. protected $_theme;
  26. /**
  27. * Directory of the css file
  28. * Using only to transmit additional parameter in callback functions
  29. *
  30. * @var string
  31. */
  32. protected $_callbackFileDir;
  33. /**
  34. * Store list manager
  35. *
  36. * @var \Magento\Store\Model\StoreManagerInterface
  37. */
  38. protected $_storeManager;
  39. /**
  40. * @var \Magento\Framework\View\Design\Theme\FlyweightFactory
  41. */
  42. protected $_flyweightFactory;
  43. /**
  44. * @var \Magento\Theme\Model\ThemeFactory
  45. */
  46. protected $_themeFactory;
  47. /**
  48. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  49. */
  50. private $_scopeConfig;
  51. /**
  52. * @var \Magento\Framework\Locale\ResolverInterface
  53. */
  54. protected $_locale;
  55. /**
  56. * @var \Magento\Framework\ObjectManagerInterface
  57. */
  58. protected $objectManager;
  59. /**
  60. * @var \Magento\Framework\App\State
  61. */
  62. protected $_appState;
  63. /**
  64. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  65. * @param \Magento\Framework\View\Design\Theme\FlyweightFactory $flyweightFactory
  66. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  67. * @param \Magento\Theme\Model\ThemeFactory $themeFactory
  68. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  69. * @param \Magento\Framework\App\State $appState
  70. * @param array $themes
  71. */
  72. public function __construct(
  73. \Magento\Store\Model\StoreManagerInterface $storeManager,
  74. \Magento\Framework\View\Design\Theme\FlyweightFactory $flyweightFactory,
  75. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  76. \Magento\Theme\Model\ThemeFactory $themeFactory,
  77. \Magento\Framework\ObjectManagerInterface $objectManager,
  78. \Magento\Framework\App\State $appState,
  79. array $themes
  80. ) {
  81. $this->_storeManager = $storeManager;
  82. $this->_flyweightFactory = $flyweightFactory;
  83. $this->_themeFactory = $themeFactory;
  84. $this->_scopeConfig = $scopeConfig;
  85. $this->_appState = $appState;
  86. $this->_themes = $themes;
  87. $this->objectManager = $objectManager;
  88. }
  89. /**
  90. * Set package area
  91. *
  92. * @param string $area
  93. * @return $this
  94. */
  95. public function setArea($area)
  96. {
  97. $this->_area = $area;
  98. $this->_theme = null;
  99. return $this;
  100. }
  101. /**
  102. * Retrieve package area
  103. *
  104. * @return string
  105. */
  106. public function getArea()
  107. {
  108. // In order to support environment emulation of area, if area is set, return it
  109. if ($this->_area && !$this->_appState->isAreaCodeEmulated()) {
  110. return $this->_area;
  111. }
  112. return $this->_appState->getAreaCode();
  113. }
  114. /**
  115. * Set theme path
  116. *
  117. * @param \Magento\Framework\View\Design\ThemeInterface|string $theme
  118. * @param string $area
  119. * @return $this
  120. */
  121. public function setDesignTheme($theme, $area = null)
  122. {
  123. if ($area) {
  124. $this->setArea($area);
  125. } else {
  126. $area = $this->getArea();
  127. }
  128. if ($theme instanceof \Magento\Framework\View\Design\ThemeInterface) {
  129. $this->_theme = $theme;
  130. } else {
  131. $this->_theme = $this->_flyweightFactory->create($theme, $area);
  132. }
  133. return $this;
  134. }
  135. /**
  136. * Get default theme which declared in configuration
  137. *
  138. * Write default theme to core_config_data
  139. *
  140. * @param string|null $area
  141. * @param array $params
  142. * @return string|int
  143. */
  144. public function getConfigurationDesignTheme($area = null, array $params = [])
  145. {
  146. if (!$area) {
  147. $area = $this->getArea();
  148. }
  149. $theme = null;
  150. $store = isset($params['store']) ? $params['store'] : null;
  151. if ($this->_isThemePerStoreView($area)) {
  152. if ($this->_storeManager->isSingleStoreMode()) {
  153. $theme = $this->_scopeConfig->getValue(
  154. self::XML_PATH_THEME_ID,
  155. ScopeInterface::SCOPE_WEBSITES
  156. );
  157. } else {
  158. $theme = (string) $this->_scopeConfig->getValue(
  159. self::XML_PATH_THEME_ID,
  160. ScopeInterface::SCOPE_STORE,
  161. $store
  162. );
  163. }
  164. }
  165. if (!$theme && isset($this->_themes[$area])) {
  166. $theme = $this->_themes[$area];
  167. }
  168. return $theme;
  169. }
  170. /**
  171. * Whether themes in specified area are supposed to be configured per store view
  172. *
  173. * @param string $area
  174. * @return bool
  175. */
  176. private function _isThemePerStoreView($area)
  177. {
  178. return $area == self::DEFAULT_AREA;
  179. }
  180. /**
  181. * Set default design theme
  182. *
  183. * @return $this
  184. */
  185. public function setDefaultDesignTheme()
  186. {
  187. $this->setDesignTheme($this->getConfigurationDesignTheme());
  188. return $this;
  189. }
  190. /**
  191. * Design theme model getter
  192. *
  193. * @return \Magento\Theme\Model\Theme
  194. */
  195. public function getDesignTheme()
  196. {
  197. if ($this->_theme === null) {
  198. $this->_theme = $this->_themeFactory->create();
  199. }
  200. return $this->_theme;
  201. }
  202. /**
  203. * {@inheritdoc}
  204. */
  205. public function getThemePath(\Magento\Framework\View\Design\ThemeInterface $theme)
  206. {
  207. $themePath = $theme->getThemePath();
  208. if (!$themePath) {
  209. $themeId = $theme->getId();
  210. if ($themeId) {
  211. $themePath = self::PUBLIC_THEME_DIR . $themeId;
  212. } else {
  213. $themePath = self::PUBLIC_VIEW_DIR;
  214. }
  215. }
  216. return $themePath;
  217. }
  218. /**
  219. * Get locale
  220. *
  221. * @return string
  222. */
  223. public function getLocale()
  224. {
  225. if (null === $this->_locale) {
  226. $this->_locale = $this->objectManager->get(\Magento\Framework\Locale\ResolverInterface::class);
  227. }
  228. return $this->_locale->getLocale();
  229. }
  230. /**
  231. * @param \Magento\Framework\Locale\ResolverInterface $locale
  232. * @return $this
  233. */
  234. public function setLocale(\Magento\Framework\Locale\ResolverInterface $locale)
  235. {
  236. $this->_locale = $locale;
  237. return $this;
  238. }
  239. /**
  240. * {@inheritdoc}
  241. */
  242. public function getDesignParams()
  243. {
  244. $params = [
  245. 'area' => $this->getArea(),
  246. 'themeModel' => $this->getDesignTheme(),
  247. 'locale' => $this->getLocale(),
  248. ];
  249. return $params;
  250. }
  251. }