Context.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View;
  7. use Magento\Framework\App\Cache\StateInterface as CacheState;
  8. use Magento\Framework\App\CacheInterface as Cache;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. use Magento\Framework\App\FrontControllerInterface;
  11. use Magento\Framework\App\Request\Http as Request;
  12. use Magento\Framework\App\State as AppState;
  13. use Magento\Framework\Event\ManagerInterface;
  14. use Psr\Log\LoggerInterface as Logger;
  15. use Magento\Framework\Session\SessionManager;
  16. use Magento\Framework\TranslateInterface;
  17. use Magento\Framework\UrlInterface;
  18. use Magento\Framework\View\ConfigInterface as ViewConfig;
  19. /**
  20. * Application Runtime Context
  21. *
  22. * @todo Reduce fields number
  23. * @todo Reduce class dependencies
  24. *
  25. * @SuppressWarnings(PHPMD.TooManyFields)
  26. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  27. * @api
  28. * @since 100.0.2
  29. */
  30. class Context
  31. {
  32. /**
  33. * Request
  34. *
  35. * @var Request
  36. */
  37. protected $request;
  38. /**
  39. * Event manager
  40. *
  41. * @var ManagerInterface
  42. */
  43. protected $eventManager;
  44. /**
  45. * URL builder
  46. * @var \Magento\Framework\UrlInterface
  47. */
  48. protected $urlBuilder;
  49. /**
  50. * Translator
  51. *
  52. * @var \Magento\Framework\TranslateInterface
  53. */
  54. protected $translator;
  55. /**
  56. * Cache
  57. *
  58. * @var \Magento\Framework\App\CacheInterface
  59. */
  60. protected $cache;
  61. /**
  62. * Design
  63. *
  64. * @var \Magento\Framework\View\DesignInterface
  65. */
  66. protected $design;
  67. /**
  68. * Session
  69. *
  70. * @var \Magento\Framework\Session\SessionManagerInterface
  71. */
  72. protected $session;
  73. /**
  74. * Store config
  75. *
  76. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  77. */
  78. protected $scopeConfig;
  79. /**
  80. * Front controller
  81. *
  82. * @var FrontControllerInterface
  83. */
  84. protected $frontController;
  85. /**
  86. * Layout
  87. *
  88. * @var \Magento\Framework\View\LayoutInterface
  89. */
  90. protected $layout;
  91. /**
  92. * View config model
  93. *
  94. * @var \Magento\Framework\View\Config
  95. */
  96. protected $viewConfig;
  97. /**
  98. * Cache state
  99. *
  100. * @var \Magento\Framework\App\Cache\StateInterface
  101. */
  102. protected $cacheState;
  103. /**
  104. * Logger
  105. *
  106. * @var \Psr\Log\LoggerInterface
  107. */
  108. protected $logger;
  109. /**
  110. * Application state
  111. *
  112. * @var \Magento\Framework\App\State
  113. */
  114. protected $appState;
  115. /**
  116. * Constructor
  117. *
  118. * @param Request $request
  119. * @param ManagerInterface $eventManager
  120. * @param UrlInterface $urlBuilder
  121. * @param TranslateInterface $translator
  122. * @param Cache $cache
  123. * @param DesignInterface $design
  124. * @param SessionManager $session
  125. * @param ScopeConfigInterface $scopeConfig
  126. * @param FrontControllerInterface $frontController
  127. * @param ViewConfig $viewConfig
  128. * @param CacheState $cacheState
  129. * @param Logger $logger
  130. * @param AppState $appState
  131. * @param LayoutInterface $layout
  132. *
  133. * @todo reduce parameter number
  134. *
  135. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  136. */
  137. public function __construct(
  138. Request $request,
  139. ManagerInterface $eventManager,
  140. UrlInterface $urlBuilder,
  141. TranslateInterface $translator,
  142. Cache $cache,
  143. DesignInterface $design,
  144. SessionManager $session,
  145. ScopeConfigInterface $scopeConfig,
  146. FrontControllerInterface $frontController,
  147. ViewConfig $viewConfig,
  148. CacheState $cacheState,
  149. Logger $logger,
  150. AppState $appState,
  151. LayoutInterface $layout
  152. ) {
  153. $this->request = $request;
  154. $this->eventManager = $eventManager;
  155. $this->urlBuilder = $urlBuilder;
  156. $this->translator = $translator;
  157. $this->cache = $cache;
  158. $this->design = $design;
  159. $this->session = $session;
  160. $this->scopeConfig = $scopeConfig;
  161. $this->frontController = $frontController;
  162. $this->viewConfig = $viewConfig;
  163. $this->cacheState = $cacheState;
  164. $this->logger = $logger;
  165. $this->appState = $appState;
  166. $this->layout = $layout;
  167. }
  168. /**
  169. * Retrieve cache
  170. *
  171. * @return \Magento\Framework\App\CacheInterface
  172. */
  173. public function getCache()
  174. {
  175. return $this->cache;
  176. }
  177. /**
  178. * Retrieve design package
  179. *
  180. * @return \Magento\Framework\View\DesignInterface
  181. */
  182. public function getDesignPackage()
  183. {
  184. return $this->design;
  185. }
  186. /**
  187. * Retrieve event manager
  188. *
  189. * @return ManagerInterface
  190. */
  191. public function getEventManager()
  192. {
  193. return $this->eventManager;
  194. }
  195. /**
  196. * Retrieve front controller
  197. *
  198. * @return FrontControllerInterface
  199. */
  200. public function getFrontController()
  201. {
  202. return $this->frontController;
  203. }
  204. /**
  205. * Retrieve layout
  206. *
  207. * @return \Magento\Framework\View\LayoutInterface
  208. */
  209. public function getLayout()
  210. {
  211. return $this->layout;
  212. }
  213. /**
  214. * Retrieve request
  215. *
  216. * @return Request
  217. */
  218. public function getRequest()
  219. {
  220. return $this->request;
  221. }
  222. /**
  223. * Retrieve session
  224. *
  225. * @return \Magento\Framework\Session\SessionManagerInterface
  226. */
  227. public function getSession()
  228. {
  229. return $this->session;
  230. }
  231. /**
  232. * Retrieve scope config
  233. *
  234. * @return \Magento\Framework\App\Config\ScopeConfigInterface
  235. */
  236. public function getScopeConfig()
  237. {
  238. return $this->scopeConfig;
  239. }
  240. /**
  241. * Retrieve translator
  242. *
  243. * @return \Magento\Framework\TranslateInterface
  244. */
  245. public function getTranslator()
  246. {
  247. return $this->translator;
  248. }
  249. /**
  250. * Retrieve URL builder
  251. *
  252. * @return \Magento\Framework\UrlInterface
  253. */
  254. public function getUrlBuilder()
  255. {
  256. return $this->urlBuilder;
  257. }
  258. /**
  259. * Retrieve view config
  260. *
  261. * @return \Magento\Framework\View\ConfigInterface
  262. */
  263. public function getViewConfig()
  264. {
  265. return $this->viewConfig;
  266. }
  267. /**
  268. * Retrieve cache state
  269. *
  270. * @return \Magento\Framework\App\Cache\StateInterface
  271. */
  272. public function getCacheState()
  273. {
  274. return $this->cacheState;
  275. }
  276. /**
  277. * Retrieve logger
  278. *
  279. * @return \Psr\Log\LoggerInterface
  280. */
  281. public function getLogger()
  282. {
  283. return $this->logger;
  284. }
  285. /**
  286. * Retrieve layout area
  287. *
  288. * @return string
  289. */
  290. public function getArea()
  291. {
  292. return $this->appState->getAreaCode();
  293. }
  294. /**
  295. * Retrieve the module name
  296. *
  297. * @return string
  298. */
  299. public function getModuleName()
  300. {
  301. return $this->getRequest()->getModuleName();
  302. }
  303. /**
  304. * @see getModuleName
  305. */
  306. public function getFrontName()
  307. {
  308. return $this->getModuleName();
  309. }
  310. /**
  311. * Retrieve the controller name
  312. *
  313. * @return string
  314. */
  315. public function getControllerName()
  316. {
  317. return $this->getRequest()->getControllerName();
  318. }
  319. /**
  320. * Retrieve the action name
  321. *
  322. * @return string
  323. */
  324. public function getActionName()
  325. {
  326. return $this->getRequest()->getActionName();
  327. }
  328. /**
  329. * Retrieve the full action name
  330. *
  331. * @return string
  332. */
  333. public function getFullActionName()
  334. {
  335. return strtolower($this->getFrontName() . '_' . $this->getControllerName() . '_' . $this->getActionName());
  336. }
  337. /**
  338. * Retrieve acceptance type
  339. *
  340. * @return string
  341. */
  342. public function getAcceptType()
  343. {
  344. // TODO: do intelligence here
  345. $type = $this->getHeader('Accept', 'html');
  346. if (strpos($type, 'json') !== false) {
  347. return 'json';
  348. } elseif (strpos($type, 'soap') !== false) {
  349. return 'soap';
  350. } elseif (strpos($type, 'text/html') !== false) {
  351. return 'html';
  352. } else {
  353. return 'xml';
  354. }
  355. }
  356. /**
  357. * Retrieve a member of the $_POST superglobal
  358. *
  359. * @param string $key
  360. * @param mixed $default Default value to use if key not found
  361. * @return mixed|null if key does not exist
  362. */
  363. public function getPost($key = null, $default = null)
  364. {
  365. return $this->getRequest()->getPost($key, $default);
  366. }
  367. /**
  368. * Retrieve a member of the $_POST superglobal
  369. *
  370. * @param string|null $key
  371. * @param mixed $default Default value to use if key not found
  372. * @return mixed alias of getPost
  373. */
  374. public function getQuery($key = null, $default = null)
  375. {
  376. return $this->getRequest()->getPost($key, $default);
  377. }
  378. /**
  379. * Retrieve a parameter
  380. *
  381. * @param string|null $key
  382. * @param mixed $default Default value to use if key not found
  383. * @return mixed
  384. */
  385. public function getParam($key = null, $default = null)
  386. {
  387. return $this->getRequest()->getParam($key, $default);
  388. }
  389. /**
  390. * Retrieve an array of parameters
  391. *
  392. * @return array
  393. */
  394. public function getParams()
  395. {
  396. return $this->getRequest()->getParams();
  397. }
  398. /**
  399. * Return the value of the given HTTP header.
  400. *
  401. * @param string $header
  402. * @return string|false HTTP header value, or false if not found
  403. */
  404. public function getHeader($header)
  405. {
  406. return $this->getRequest()->getHeader($header);
  407. }
  408. /**
  409. * Return the raw body of the request, if present
  410. *
  411. * @return string|false Raw body, or false if not present
  412. */
  413. public function getContent()
  414. {
  415. return $this->getRequest()->getContent();
  416. }
  417. /**
  418. * Retrieve application state
  419. *
  420. * @return \Magento\Framework\App\State
  421. */
  422. public function getAppState()
  423. {
  424. return $this->appState;
  425. }
  426. /**
  427. * Retrieve parent theme instance
  428. *
  429. * @param Design\ThemeInterface $theme
  430. * @return Design\ThemeInterface
  431. * @throws \Exception
  432. */
  433. protected function getPhysicalTheme(Design\ThemeInterface $theme)
  434. {
  435. $result = $theme;
  436. while ($result->getId() && !$result->isPhysical()) {
  437. $result = $result->getParentTheme();
  438. }
  439. if (!$result) {
  440. throw new \Exception("Unable to find a physical ancestor for a theme '{$theme->getThemeTitle()}'.");
  441. }
  442. return $result;
  443. }
  444. }