123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\View\Element;
- use Magento\Framework\App\Filesystem\DirectoryList;
- use Magento\Framework\Filesystem;
- /**
- * Standard Magento block.
- * Should be used when you declare a block in frontend area layout handle.
- *
- * Avoid extending this class.
- *
- * If you need custom presentation logic in your blocks, use this class as block, and declare
- * custom view models in block arguments in layout handle file.
- *
- * Example:
- * <block name="my.block" class="Magento\Backend\Block\Template" template="My_Module::template.phtml" >
- * <arguments>
- * <argument name="viewModel" xsi:type="object">My\Module\ViewModel\Custom</argument>
- * </arguments>
- * </block>
- *
- * @api
- * @SuppressWarnings(PHPMD.NumberOfChildren)
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @since 100.0.2
- */
- class Template extends AbstractBlock
- {
- /**
- * Config path to 'Allow Symlinks' template settings
- */
- const XML_PATH_TEMPLATE_ALLOW_SYMLINK = 'dev/template/allow_symlink';
- /**
- * Assigned variables for view
- *
- * @var array
- */
- protected $_viewVars = [];
- /**
- * Base URL
- *
- * @var string
- */
- protected $_baseUrl;
- /**
- * JS URL
- *
- * @var string
- */
- protected $_jsUrl;
- /**
- * Is allowed symlinks flag
- *
- * @var bool
- */
- protected $_allowSymlinks;
- /**
- * Filesystem instance
- *
- * @var Filesystem
- */
- protected $_filesystem;
- /**
- * Path to template file in theme.
- *
- * @var string
- */
- protected $_template;
- /**
- * Template engine pool
- *
- * @var \Magento\Framework\View\TemplateEnginePool
- */
- protected $templateEnginePool;
- /**
- * Store manager
- *
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- protected $_storeManager;
- /**
- * Application state
- *
- * @var \Magento\Framework\App\State
- */
- protected $_appState;
- /**
- * Root directory instance
- *
- * @var \Magento\Framework\Filesystem\Directory\ReadInterface
- */
- protected $directory;
- /**
- * Media directory instance
- *
- * @var \Magento\Framework\Filesystem\Directory\ReadInterface
- */
- private $mediaDirectory;
- /**
- * Template context
- *
- * @var \Magento\Framework\View\Element\BlockInterface
- */
- protected $templateContext;
- /**
- * @var \Magento\Framework\View\Page\Config
- */
- protected $pageConfig;
- /**
- * @var \Magento\Framework\View\Element\Template\File\Resolver
- */
- protected $resolver;
- /**
- * @var \Magento\Framework\View\Element\Template\File\Validator
- */
- protected $validator;
- /**
- * Constructor
- *
- * @param Template\Context $context
- * @param array $data
- */
- public function __construct(Template\Context $context, array $data = [])
- {
- $this->validator = $context->getValidator();
- $this->resolver = $context->getResolver();
- $this->_filesystem = $context->getFilesystem();
- $this->templateEnginePool = $context->getEnginePool();
- $this->_storeManager = $context->getStoreManager();
- $this->_appState = $context->getAppState();
- $this->templateContext = $this;
- $this->pageConfig = $context->getPageConfig();
- parent::__construct($context, $data);
- }
- /**
- * Set template context. Sets the object that should represent $block in template
- *
- * @param \Magento\Framework\View\Element\BlockInterface $templateContext
- * @return void
- */
- public function setTemplateContext($templateContext)
- {
- $this->templateContext = $templateContext;
- }
- /**
- * Internal constructor, that is called from real constructor
- * @return void
- */
- protected function _construct()
- {
- parent::_construct();
- /*
- * In case template was passed through constructor
- * we assign it to block's property _template
- * Mainly for those cases when block created
- * not via \Magento\Framework\View\Model\LayoutInterface::addBlock()
- */
- if ($this->hasData('template')) {
- $this->setTemplate($this->getData('template'));
- }
- }
- /**
- * Get relevant path to template
- *
- * @return string
- */
- public function getTemplate()
- {
- return $this->_template;
- }
- /**
- * Set path to template used for generating block's output.
- *
- * @param string $template
- * @return $this
- */
- public function setTemplate($template)
- {
- $this->_template = $template;
- return $this;
- }
- /**
- * Get absolute path to template
- *
- * @param string|null $template
- * @return string|bool
- */
- public function getTemplateFile($template = null)
- {
- $params = ['module' => $this->getModuleName()];
- $area = $this->getArea();
- if ($area) {
- $params['area'] = $area;
- }
- return $this->resolver->getTemplateFileName($template ?: $this->getTemplate(), $params);
- }
- /**
- * Get design area
- *
- * @return string
- */
- public function getArea()
- {
- return $this->_getData('area') ? $this->_getData('area') : $this->_appState->getAreaCode();
- }
- /**
- * Assign variable
- *
- * @param string|array $key
- * @param mixed $value
- * @return $this
- */
- public function assign($key, $value = null)
- {
- if (is_array($key)) {
- foreach ($key as $subKey => $subValue) {
- $this->assign($subKey, $subValue);
- }
- } else {
- $this->_viewVars[$key] = $value;
- }
- return $this;
- }
- /**
- * Retrieve block view from file (template)
- *
- * @param string $fileName
- * @return string
- */
- public function fetchView($fileName)
- {
- $relativeFilePath = $this->getRootDirectory()->getRelativePath($fileName);
- \Magento\Framework\Profiler::start(
- 'TEMPLATE:' . $fileName,
- ['group' => 'TEMPLATE', 'file_name' => $relativeFilePath]
- );
- if ($this->validator->isValid($fileName)) {
- $extension = pathinfo($fileName, PATHINFO_EXTENSION);
- $templateEngine = $this->templateEnginePool->get($extension);
- $html = $templateEngine->render($this->templateContext, $fileName, $this->_viewVars);
- } else {
- $html = '';
- $templatePath = $fileName ?: $this->getTemplate();
- $errorMessage = "Invalid template file: '{$templatePath}' in module: '{$this->getModuleName()}'"
- . " block's name: '{$this->getNameInLayout()}'";
- if ($this->_appState->getMode() === \Magento\Framework\App\State::MODE_DEVELOPER) {
- throw new \Magento\Framework\Exception\ValidatorException(
- new \Magento\Framework\Phrase(
- $errorMessage
- )
- );
- }
- $this->_logger->critical($errorMessage);
- }
- \Magento\Framework\Profiler::stop('TEMPLATE:' . $fileName);
- return $html;
- }
- /**
- * Render block HTML
- *
- * @return string
- */
- protected function _toHtml()
- {
- if (!$this->getTemplate()) {
- return '';
- }
- return $this->fetchView($this->getTemplateFile());
- }
- /**
- * Get base url of the application
- *
- * @return string
- */
- public function getBaseUrl()
- {
- if (!$this->_baseUrl) {
- $this->_baseUrl = $this->_urlBuilder->getBaseUrl();
- }
- return $this->_baseUrl;
- }
- /**
- * Get data from specified object
- *
- * @param \Magento\Framework\DataObject $object
- * @param string $key
- * @return mixed
- */
- public function getObjectData(\Magento\Framework\DataObject $object, $key)
- {
- return $object->getDataUsingMethod((string)$key);
- }
- /**
- * Get cache key informative items
- *
- * @return array
- */
- public function getCacheKeyInfo()
- {
- return [
- 'BLOCK_TPL',
- $this->_storeManager->getStore()->getCode(),
- $this->getTemplateFile(),
- 'base_url' => $this->getBaseUrl(),
- 'template' => $this->getTemplate()
- ];
- }
- /**
- * Instantiates filesystem directory
- *
- * @return \Magento\Framework\Filesystem\Directory\ReadInterface
- */
- protected function getRootDirectory()
- {
- if (null === $this->directory) {
- $this->directory = $this->_filesystem->getDirectoryRead(DirectoryList::ROOT);
- }
- return $this->directory;
- }
- /**
- * Get media directory
- *
- * @return \Magento\Framework\Filesystem\Directory\Read
- */
- protected function getMediaDirectory()
- {
- if (!$this->mediaDirectory) {
- $this->mediaDirectory = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
- }
- return $this->mediaDirectory;
- }
- }
|