Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Area.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Framework\App;
8 
10 
17 {
18  const AREA_GLOBAL = 'global';
19  const AREA_FRONTEND = 'frontend';
20  const AREA_ADMINHTML = 'adminhtml';
21  const AREA_DOC = 'doc';
22  const AREA_CRONTAB = 'crontab';
23  const AREA_WEBAPI_REST = 'webapi_rest';
24  const AREA_WEBAPI_SOAP = 'webapi_soap';
25  const AREA_GRAPHQL = 'graphql';
26 
30  const AREA_ADMIN = 'admin';
31 
35  const PARAM_AREA = 'area';
36 
42  protected $_loadedParts;
43 
49  protected $_code;
50 
56  protected $_eventManager;
57 
63  protected $_translator;
64 
70  protected $_objectManager;
71 
75  protected $_diConfigLoader;
76 
80  protected $_logger;
81 
87  protected $_design;
88 
92  protected $_scopeResolver;
93 
97  protected $_designExceptions;
98 
110  public function __construct(
111  \Psr\Log\LoggerInterface $logger,
112  \Magento\Framework\Event\ManagerInterface $eventManager,
113  \Magento\Framework\TranslateInterface $translator,
115  ConfigLoaderInterface $diConfigLoader,
116  \Magento\Framework\App\DesignInterface $design,
117  \Magento\Framework\App\ScopeResolverInterface $scopeResolver,
118  \Magento\Framework\View\DesignExceptions $designExceptions,
119  $areaCode
120  ) {
121  $this->_code = $areaCode;
122  $this->_objectManager = $objectManager;
123  $this->_diConfigLoader = $diConfigLoader;
124  $this->_eventManager = $eventManager;
125  $this->_translator = $translator;
126  $this->_logger = $logger;
127  $this->_design = $design;
128  $this->_scopeResolver = $scopeResolver;
129  $this->_designExceptions = $designExceptions;
130  }
131 
138  public function load($part = null)
139  {
140  if ($part === null) {
141  $this->_loadPart(self::PART_CONFIG)->_loadPart(self::PART_DESIGN)->_loadPart(self::PART_TRANSLATE);
142  } else {
143  $this->_loadPart($part);
144  }
145  return $this;
146  }
147 
154  public function detectDesign($request = null)
155  {
156  if ($this->_code == self::AREA_FRONTEND) {
157  $isDesignException = $request && $this->_applyUserAgentDesignException($request);
158  if (!$isDesignException) {
159  $this->_design->loadChange(
160  $this->_scopeResolver->getScope()->getId()
161  )->changeDesign(
162  $this->_getDesign()
163  );
164  }
165  }
166  }
167 
175  {
176  try {
177  $theme = $this->_designExceptions->getThemeByRequest($request);
178  if (false !== $theme) {
179  $this->_getDesign()->setDesignTheme($theme);
180  return true;
181  }
182  } catch (\Exception $e) {
183  $this->_logger->critical($e);
184  }
185  return false;
186  }
187 
191  protected function _getDesign()
192  {
193  return $this->_objectManager->get(\Magento\Framework\View\DesignInterface::class);
194  }
195 
202  protected function _loadPart($part)
203  {
204  if (isset($this->_loadedParts[$part])) {
205  return $this;
206  }
207  \Magento\Framework\Profiler::start(
208  'load_area:' . $this->_code . '.' . $part,
209  ['group' => 'load_area', 'area_code' => $this->_code, 'part' => $part]
210  );
211  switch ($part) {
212  case self::PART_CONFIG:
213  $this->_initConfig();
214  break;
216  $this->_initTranslate();
217  break;
218  case self::PART_DESIGN:
219  $this->_initDesign();
220  break;
221  }
222  $this->_loadedParts[$part] = true;
223  \Magento\Framework\Profiler::stop('load_area:' . $this->_code . '.' . $part);
224  return $this;
225  }
226 
232  protected function _initConfig()
233  {
234  $this->_objectManager->configure($this->_diConfigLoader->load($this->_code));
235  return $this;
236  }
237 
243  protected function _initTranslate()
244  {
245  $this->_translator->loadData(null, false);
246 
248  $this->_objectManager->get(\Magento\Framework\Phrase\RendererInterface::class)
249  );
250 
251  return $this;
252  }
253 
259  protected function _initDesign()
260  {
261  $this->_getDesign()->setArea($this->_code)->setDefaultDesignTheme();
262  return $this;
263  }
264 }
__construct(\Psr\Log\LoggerInterface $logger, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\TranslateInterface $translator, \Magento\Framework\ObjectManagerInterface $objectManager, ConfigLoaderInterface $diConfigLoader, \Magento\Framework\App\DesignInterface $design, \Magento\Framework\App\ScopeResolverInterface $scopeResolver, \Magento\Framework\View\DesignExceptions $designExceptions, $areaCode)
Definition: Area.php:110
$objectManager
Definition: bootstrap.php:17
$logger
detectDesign($request=null)
Definition: Area.php:154
_applyUserAgentDesignException($request)
Definition: Area.php:174
static setRenderer(RendererInterface $renderer)
Definition: Phrase.php:46
$theme