Area.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App;
  7. use Magento\Framework\ObjectManager\ConfigLoaderInterface;
  8. /**
  9. * Application area model
  10. *
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class Area implements \Magento\Framework\App\AreaInterface
  14. {
  15. const AREA_GLOBAL = 'global';
  16. const AREA_FRONTEND = 'frontend';
  17. const AREA_ADMINHTML = 'adminhtml';
  18. const AREA_DOC = 'doc';
  19. const AREA_CRONTAB = 'crontab';
  20. const AREA_WEBAPI_REST = 'webapi_rest';
  21. const AREA_WEBAPI_SOAP = 'webapi_soap';
  22. const AREA_GRAPHQL = 'graphql';
  23. /**
  24. * @deprecated
  25. */
  26. const AREA_ADMIN = 'admin';
  27. /**
  28. * Area parameter.
  29. */
  30. const PARAM_AREA = 'area';
  31. /**
  32. * Array of area loaded parts
  33. *
  34. * @var array
  35. */
  36. protected $_loadedParts;
  37. /**
  38. * Area code
  39. *
  40. * @var string
  41. */
  42. protected $_code;
  43. /**
  44. * Event Manager
  45. *
  46. * @var \Magento\Framework\Event\ManagerInterface
  47. */
  48. protected $_eventManager;
  49. /**
  50. * Translator
  51. *
  52. * @var \Magento\Framework\TranslateInterface
  53. */
  54. protected $_translator;
  55. /**
  56. * Object manager
  57. *
  58. * @var \Magento\Framework\ObjectManagerInterface
  59. */
  60. protected $_objectManager;
  61. /**
  62. * @var ConfigLoaderInterface
  63. */
  64. protected $_diConfigLoader;
  65. /**
  66. * @var \Psr\Log\LoggerInterface
  67. */
  68. protected $_logger;
  69. /**
  70. * Core design
  71. *
  72. * @var \Magento\Framework\App\DesignInterface
  73. */
  74. protected $_design;
  75. /**
  76. * @var \Magento\Framework\App\ScopeResolverInterface
  77. */
  78. protected $_scopeResolver;
  79. /**
  80. * @var \Magento\Framework\View\DesignExceptions
  81. */
  82. protected $_designExceptions;
  83. /**
  84. * @param \Psr\Log\LoggerInterface $logger
  85. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  86. * @param \Magento\Framework\TranslateInterface $translator
  87. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  88. * @param ConfigLoaderInterface $diConfigLoader
  89. * @param \Magento\Framework\App\DesignInterface $design
  90. * @param \Magento\Framework\App\ScopeResolverInterface $scopeResolver
  91. * @param \Magento\Framework\View\DesignExceptions $designExceptions
  92. * @param string $areaCode
  93. */
  94. public function __construct(
  95. \Psr\Log\LoggerInterface $logger,
  96. \Magento\Framework\Event\ManagerInterface $eventManager,
  97. \Magento\Framework\TranslateInterface $translator,
  98. \Magento\Framework\ObjectManagerInterface $objectManager,
  99. ConfigLoaderInterface $diConfigLoader,
  100. \Magento\Framework\App\DesignInterface $design,
  101. \Magento\Framework\App\ScopeResolverInterface $scopeResolver,
  102. \Magento\Framework\View\DesignExceptions $designExceptions,
  103. $areaCode
  104. ) {
  105. $this->_code = $areaCode;
  106. $this->_objectManager = $objectManager;
  107. $this->_diConfigLoader = $diConfigLoader;
  108. $this->_eventManager = $eventManager;
  109. $this->_translator = $translator;
  110. $this->_logger = $logger;
  111. $this->_design = $design;
  112. $this->_scopeResolver = $scopeResolver;
  113. $this->_designExceptions = $designExceptions;
  114. }
  115. /**
  116. * Load area data
  117. *
  118. * @param string|null $part
  119. * @return $this
  120. */
  121. public function load($part = null)
  122. {
  123. if ($part === null) {
  124. $this->_loadPart(self::PART_CONFIG)->_loadPart(self::PART_DESIGN)->_loadPart(self::PART_TRANSLATE);
  125. } else {
  126. $this->_loadPart($part);
  127. }
  128. return $this;
  129. }
  130. /**
  131. * Detect and apply design for the area
  132. *
  133. * @param \Magento\Framework\App\RequestInterface $request
  134. * @return void
  135. */
  136. public function detectDesign($request = null)
  137. {
  138. if ($this->_code == self::AREA_FRONTEND) {
  139. $isDesignException = $request && $this->_applyUserAgentDesignException($request);
  140. if (!$isDesignException) {
  141. $this->_design->loadChange(
  142. $this->_scopeResolver->getScope()->getId()
  143. )->changeDesign(
  144. $this->_getDesign()
  145. );
  146. }
  147. }
  148. }
  149. /**
  150. * Analyze user-agent information to override custom design settings
  151. *
  152. * @param \Magento\Framework\App\RequestInterface $request
  153. * @return bool
  154. */
  155. protected function _applyUserAgentDesignException($request)
  156. {
  157. try {
  158. $theme = $this->_designExceptions->getThemeByRequest($request);
  159. if (false !== $theme) {
  160. $this->_getDesign()->setDesignTheme($theme);
  161. return true;
  162. }
  163. } catch (\Exception $e) {
  164. $this->_logger->critical($e);
  165. }
  166. return false;
  167. }
  168. /**
  169. * @return \Magento\Framework\View\DesignInterface
  170. */
  171. protected function _getDesign()
  172. {
  173. return $this->_objectManager->get(\Magento\Framework\View\DesignInterface::class);
  174. }
  175. /**
  176. * Loading part of area
  177. *
  178. * @param string $part
  179. * @return $this
  180. */
  181. protected function _loadPart($part)
  182. {
  183. if (isset($this->_loadedParts[$part])) {
  184. return $this;
  185. }
  186. \Magento\Framework\Profiler::start(
  187. 'load_area:' . $this->_code . '.' . $part,
  188. ['group' => 'load_area', 'area_code' => $this->_code, 'part' => $part]
  189. );
  190. switch ($part) {
  191. case self::PART_CONFIG:
  192. $this->_initConfig();
  193. break;
  194. case self::PART_TRANSLATE:
  195. $this->_initTranslate();
  196. break;
  197. case self::PART_DESIGN:
  198. $this->_initDesign();
  199. break;
  200. }
  201. $this->_loadedParts[$part] = true;
  202. \Magento\Framework\Profiler::stop('load_area:' . $this->_code . '.' . $part);
  203. return $this;
  204. }
  205. /**
  206. * Load area configuration
  207. *
  208. * @return $this
  209. */
  210. protected function _initConfig()
  211. {
  212. $this->_objectManager->configure($this->_diConfigLoader->load($this->_code));
  213. return $this;
  214. }
  215. /**
  216. * Initialize translate object.
  217. *
  218. * @return $this
  219. */
  220. protected function _initTranslate()
  221. {
  222. $this->_translator->loadData(null, false);
  223. \Magento\Framework\Phrase::setRenderer(
  224. $this->_objectManager->get(\Magento\Framework\Phrase\RendererInterface::class)
  225. );
  226. return $this;
  227. }
  228. /**
  229. * Initialize design
  230. *
  231. * @return $this
  232. */
  233. protected function _initDesign()
  234. {
  235. $this->_getDesign()->setArea($this->_code)->setDefaultDesignTheme();
  236. return $this;
  237. }
  238. }