123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\View;
- use Magento\Framework\App\Cache\StateInterface as CacheState;
- use Magento\Framework\App\CacheInterface as Cache;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- use Magento\Framework\App\FrontControllerInterface;
- use Magento\Framework\App\Request\Http as Request;
- use Magento\Framework\App\State as AppState;
- use Magento\Framework\Event\ManagerInterface;
- use Psr\Log\LoggerInterface as Logger;
- use Magento\Framework\Session\SessionManager;
- use Magento\Framework\TranslateInterface;
- use Magento\Framework\UrlInterface;
- use Magento\Framework\View\ConfigInterface as ViewConfig;
- /**
- * Application Runtime Context
- *
- * @todo Reduce fields number
- * @todo Reduce class dependencies
- *
- * @SuppressWarnings(PHPMD.TooManyFields)
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @api
- * @since 100.0.2
- */
- class Context
- {
- /**
- * Request
- *
- * @var Request
- */
- protected $request;
- /**
- * Event manager
- *
- * @var ManagerInterface
- */
- protected $eventManager;
- /**
- * URL builder
- * @var \Magento\Framework\UrlInterface
- */
- protected $urlBuilder;
- /**
- * Translator
- *
- * @var \Magento\Framework\TranslateInterface
- */
- protected $translator;
- /**
- * Cache
- *
- * @var \Magento\Framework\App\CacheInterface
- */
- protected $cache;
- /**
- * Design
- *
- * @var \Magento\Framework\View\DesignInterface
- */
- protected $design;
- /**
- * Session
- *
- * @var \Magento\Framework\Session\SessionManagerInterface
- */
- protected $session;
- /**
- * Store config
- *
- * @var \Magento\Framework\App\Config\ScopeConfigInterface
- */
- protected $scopeConfig;
- /**
- * Front controller
- *
- * @var FrontControllerInterface
- */
- protected $frontController;
- /**
- * Layout
- *
- * @var \Magento\Framework\View\LayoutInterface
- */
- protected $layout;
- /**
- * View config model
- *
- * @var \Magento\Framework\View\Config
- */
- protected $viewConfig;
- /**
- * Cache state
- *
- * @var \Magento\Framework\App\Cache\StateInterface
- */
- protected $cacheState;
- /**
- * Logger
- *
- * @var \Psr\Log\LoggerInterface
- */
- protected $logger;
- /**
- * Application state
- *
- * @var \Magento\Framework\App\State
- */
- protected $appState;
- /**
- * Constructor
- *
- * @param Request $request
- * @param ManagerInterface $eventManager
- * @param UrlInterface $urlBuilder
- * @param TranslateInterface $translator
- * @param Cache $cache
- * @param DesignInterface $design
- * @param SessionManager $session
- * @param ScopeConfigInterface $scopeConfig
- * @param FrontControllerInterface $frontController
- * @param ViewConfig $viewConfig
- * @param CacheState $cacheState
- * @param Logger $logger
- * @param AppState $appState
- * @param LayoutInterface $layout
- *
- * @todo reduce parameter number
- *
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- Request $request,
- ManagerInterface $eventManager,
- UrlInterface $urlBuilder,
- TranslateInterface $translator,
- Cache $cache,
- DesignInterface $design,
- SessionManager $session,
- ScopeConfigInterface $scopeConfig,
- FrontControllerInterface $frontController,
- ViewConfig $viewConfig,
- CacheState $cacheState,
- Logger $logger,
- AppState $appState,
- LayoutInterface $layout
- ) {
- $this->request = $request;
- $this->eventManager = $eventManager;
- $this->urlBuilder = $urlBuilder;
- $this->translator = $translator;
- $this->cache = $cache;
- $this->design = $design;
- $this->session = $session;
- $this->scopeConfig = $scopeConfig;
- $this->frontController = $frontController;
- $this->viewConfig = $viewConfig;
- $this->cacheState = $cacheState;
- $this->logger = $logger;
- $this->appState = $appState;
- $this->layout = $layout;
- }
- /**
- * Retrieve cache
- *
- * @return \Magento\Framework\App\CacheInterface
- */
- public function getCache()
- {
- return $this->cache;
- }
- /**
- * Retrieve design package
- *
- * @return \Magento\Framework\View\DesignInterface
- */
- public function getDesignPackage()
- {
- return $this->design;
- }
- /**
- * Retrieve event manager
- *
- * @return ManagerInterface
- */
- public function getEventManager()
- {
- return $this->eventManager;
- }
- /**
- * Retrieve front controller
- *
- * @return FrontControllerInterface
- */
- public function getFrontController()
- {
- return $this->frontController;
- }
- /**
- * Retrieve layout
- *
- * @return \Magento\Framework\View\LayoutInterface
- */
- public function getLayout()
- {
- return $this->layout;
- }
- /**
- * Retrieve request
- *
- * @return Request
- */
- public function getRequest()
- {
- return $this->request;
- }
- /**
- * Retrieve session
- *
- * @return \Magento\Framework\Session\SessionManagerInterface
- */
- public function getSession()
- {
- return $this->session;
- }
- /**
- * Retrieve scope config
- *
- * @return \Magento\Framework\App\Config\ScopeConfigInterface
- */
- public function getScopeConfig()
- {
- return $this->scopeConfig;
- }
- /**
- * Retrieve translator
- *
- * @return \Magento\Framework\TranslateInterface
- */
- public function getTranslator()
- {
- return $this->translator;
- }
- /**
- * Retrieve URL builder
- *
- * @return \Magento\Framework\UrlInterface
- */
- public function getUrlBuilder()
- {
- return $this->urlBuilder;
- }
- /**
- * Retrieve view config
- *
- * @return \Magento\Framework\View\ConfigInterface
- */
- public function getViewConfig()
- {
- return $this->viewConfig;
- }
- /**
- * Retrieve cache state
- *
- * @return \Magento\Framework\App\Cache\StateInterface
- */
- public function getCacheState()
- {
- return $this->cacheState;
- }
- /**
- * Retrieve logger
- *
- * @return \Psr\Log\LoggerInterface
- */
- public function getLogger()
- {
- return $this->logger;
- }
- /**
- * Retrieve layout area
- *
- * @return string
- */
- public function getArea()
- {
- return $this->appState->getAreaCode();
- }
- /**
- * Retrieve the module name
- *
- * @return string
- */
- public function getModuleName()
- {
- return $this->getRequest()->getModuleName();
- }
- /**
- * @see getModuleName
- */
- public function getFrontName()
- {
- return $this->getModuleName();
- }
- /**
- * Retrieve the controller name
- *
- * @return string
- */
- public function getControllerName()
- {
- return $this->getRequest()->getControllerName();
- }
- /**
- * Retrieve the action name
- *
- * @return string
- */
- public function getActionName()
- {
- return $this->getRequest()->getActionName();
- }
- /**
- * Retrieve the full action name
- *
- * @return string
- */
- public function getFullActionName()
- {
- return strtolower($this->getFrontName() . '_' . $this->getControllerName() . '_' . $this->getActionName());
- }
- /**
- * Retrieve acceptance type
- *
- * @return string
- */
- public function getAcceptType()
- {
- // TODO: do intelligence here
- $type = $this->getHeader('Accept', 'html');
- if (strpos($type, 'json') !== false) {
- return 'json';
- } elseif (strpos($type, 'soap') !== false) {
- return 'soap';
- } elseif (strpos($type, 'text/html') !== false) {
- return 'html';
- } else {
- return 'xml';
- }
- }
- /**
- * Retrieve a member of the $_POST superglobal
- *
- * @param string $key
- * @param mixed $default Default value to use if key not found
- * @return mixed|null if key does not exist
- */
- public function getPost($key = null, $default = null)
- {
- return $this->getRequest()->getPost($key, $default);
- }
- /**
- * Retrieve a member of the $_POST superglobal
- *
- * @param string|null $key
- * @param mixed $default Default value to use if key not found
- * @return mixed alias of getPost
- */
- public function getQuery($key = null, $default = null)
- {
- return $this->getRequest()->getPost($key, $default);
- }
- /**
- * Retrieve a parameter
- *
- * @param string|null $key
- * @param mixed $default Default value to use if key not found
- * @return mixed
- */
- public function getParam($key = null, $default = null)
- {
- return $this->getRequest()->getParam($key, $default);
- }
- /**
- * Retrieve an array of parameters
- *
- * @return array
- */
- public function getParams()
- {
- return $this->getRequest()->getParams();
- }
- /**
- * Return the value of the given HTTP header.
- *
- * @param string $header
- * @return string|false HTTP header value, or false if not found
- */
- public function getHeader($header)
- {
- return $this->getRequest()->getHeader($header);
- }
- /**
- * Return the raw body of the request, if present
- *
- * @return string|false Raw body, or false if not present
- */
- public function getContent()
- {
- return $this->getRequest()->getContent();
- }
- /**
- * Retrieve application state
- *
- * @return \Magento\Framework\App\State
- */
- public function getAppState()
- {
- return $this->appState;
- }
- /**
- * Retrieve parent theme instance
- *
- * @param Design\ThemeInterface $theme
- * @return Design\ThemeInterface
- * @throws \Exception
- */
- protected function getPhysicalTheme(Design\ThemeInterface $theme)
- {
- $result = $theme;
- while ($result->getId() && !$result->isPhysical()) {
- $result = $result->getParentTheme();
- }
- if (!$result) {
- throw new \Exception("Unable to find a physical ancestor for a theme '{$theme->getThemeTitle()}'.");
- }
- return $result;
- }
- }
|