123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\View\Result;
- use Magento\Framework;
- use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
- use Magento\Framework\App\ResponseInterface;
- use Magento\Framework\Controller\AbstractResult;
- use Magento\Framework\View;
- /**
- * A generic layout response can be used for rendering any kind of layout
- * So it comprises a response body from the layout elements it has and sets it to the HTTP response
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- *
- * @api
- * @since 100.0.2
- */
- class Layout extends AbstractResult
- {
- /**
- * @var \Magento\Framework\View\LayoutFactory
- */
- protected $layoutFactory;
- /**
- * @var \Magento\Framework\View\Layout\BuilderFactory
- */
- protected $layoutBuilderFactory;
- /**
- * @var \Magento\Framework\View\Layout\ReaderPool
- */
- protected $layoutReaderPool;
- /**
- * @var \Magento\Framework\View\LayoutInterface
- */
- protected $layout;
- /**
- * @var \Magento\Framework\Translate\InlineInterface
- */
- protected $translateInline;
- /**
- * @var \Magento\Framework\Event\ManagerInterface
- */
- protected $eventManager;
- /**
- * @var \Magento\Framework\App\Request\Http
- */
- protected $request;
- /**
- * Constructor
- *
- * @param View\Element\Template\Context $context
- * @param View\LayoutFactory $layoutFactory
- * @param View\Layout\ReaderPool $layoutReaderPool
- * @param Framework\Translate\InlineInterface $translateInline
- * @param View\Layout\BuilderFactory $layoutBuilderFactory
- * @param View\Layout\GeneratorPool $generatorPool
- * @param bool $isIsolated
- */
- public function __construct(
- View\Element\Template\Context $context,
- View\LayoutFactory $layoutFactory,
- View\Layout\ReaderPool $layoutReaderPool,
- Framework\Translate\InlineInterface $translateInline,
- View\Layout\BuilderFactory $layoutBuilderFactory,
- View\Layout\GeneratorPool $generatorPool,
- $isIsolated = false
- ) {
- $this->layoutFactory = $layoutFactory;
- $this->layoutBuilderFactory = $layoutBuilderFactory;
- $this->layoutReaderPool = $layoutReaderPool;
- $this->eventManager = $context->getEventManager();
- $this->request = $context->getRequest();
- $this->translateInline = $translateInline;
- // TODO Shared layout object will be deleted in MAGETWO-28359
- $this->layout = $isIsolated
- ? $this->layoutFactory->create(['reader' => $this->layoutReaderPool, 'generatorPool' => $generatorPool])
- : $context->getLayout();
- $this->layout->setGeneratorPool($generatorPool);
- $this->initLayoutBuilder();
- }
- /**
- * Create layout builder
- *
- * @return void
- */
- protected function initLayoutBuilder()
- {
- $this->layoutBuilderFactory->create(View\Layout\BuilderFactory::TYPE_LAYOUT, ['layout' => $this->layout]);
- }
- /**
- * Get layout instance for current page
- *
- * @return \Magento\Framework\View\LayoutInterface
- */
- public function getLayout()
- {
- return $this->layout;
- }
- /**
- * @return $this
- */
- public function addDefaultHandle()
- {
- $this->addHandle($this->getDefaultLayoutHandle());
- return $this;
- }
- /**
- * Retrieve the default layout handle name for the current action
- *
- * @return string
- */
- public function getDefaultLayoutHandle()
- {
- return strtolower($this->request->getFullActionName());
- }
- /**
- * @param string|string[] $handleName
- * @return $this
- */
- public function addHandle($handleName)
- {
- $this->getLayout()->getUpdate()->addHandle($handleName);
- return $this;
- }
- /**
- * Add update to merge object
- *
- * @param string $update
- * @return $this
- */
- public function addUpdate($update)
- {
- $this->getLayout()->getUpdate()->addUpdate($update);
- return $this;
- }
- /**
- * Render current layout
- *
- * @param HttpResponseInterface|ResponseInterface $httpResponse
- * @return $this
- */
- public function renderResult(ResponseInterface $httpResponse)
- {
- \Magento\Framework\Profiler::start('LAYOUT');
- \Magento\Framework\Profiler::start('layout_render');
- $this->eventManager->dispatch('layout_render_before');
- $this->eventManager->dispatch('layout_render_before_' . $this->request->getFullActionName());
- $this->applyHttpHeaders($httpResponse);
- $this->render($httpResponse);
- \Magento\Framework\Profiler::stop('layout_render');
- \Magento\Framework\Profiler::stop('LAYOUT');
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- protected function render(HttpResponseInterface $response)
- {
- $output = $this->layout->getOutput();
- $this->translateInline->processResponseBody($output);
- $response->appendBody($output);
- return $this;
- }
- }
|