Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Layout.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
14 
24 class Layout extends AbstractResult
25 {
29  protected $layoutFactory;
30 
35 
39  protected $layoutReaderPool;
40 
44  protected $layout;
45 
49  protected $translateInline;
50 
54  protected $eventManager;
55 
59  protected $request;
60 
72  public function __construct(
73  View\Element\Template\Context $context,
74  View\LayoutFactory $layoutFactory,
75  View\Layout\ReaderPool $layoutReaderPool,
76  Framework\Translate\InlineInterface $translateInline,
77  View\Layout\BuilderFactory $layoutBuilderFactory,
78  View\Layout\GeneratorPool $generatorPool,
79  $isIsolated = false
80  ) {
81  $this->layoutFactory = $layoutFactory;
82  $this->layoutBuilderFactory = $layoutBuilderFactory;
83  $this->layoutReaderPool = $layoutReaderPool;
84  $this->eventManager = $context->getEventManager();
85  $this->request = $context->getRequest();
86  $this->translateInline = $translateInline;
87  // TODO Shared layout object will be deleted in MAGETWO-28359
88  $this->layout = $isIsolated
89  ? $this->layoutFactory->create(['reader' => $this->layoutReaderPool, 'generatorPool' => $generatorPool])
90  : $context->getLayout();
91  $this->layout->setGeneratorPool($generatorPool);
92  $this->initLayoutBuilder();
93  }
94 
100  protected function initLayoutBuilder()
101  {
102  $this->layoutBuilderFactory->create(View\Layout\BuilderFactory::TYPE_LAYOUT, ['layout' => $this->layout]);
103  }
104 
110  public function getLayout()
111  {
112  return $this->layout;
113  }
114 
118  public function addDefaultHandle()
119  {
120  $this->addHandle($this->getDefaultLayoutHandle());
121  return $this;
122  }
123 
129  public function getDefaultLayoutHandle()
130  {
131  return strtolower($this->request->getFullActionName());
132  }
133 
138  public function addHandle($handleName)
139  {
140  $this->getLayout()->getUpdate()->addHandle($handleName);
141  return $this;
142  }
143 
150  public function addUpdate($update)
151  {
152  $this->getLayout()->getUpdate()->addUpdate($update);
153  return $this;
154  }
155 
162  public function renderResult(ResponseInterface $httpResponse)
163  {
164  \Magento\Framework\Profiler::start('LAYOUT');
165  \Magento\Framework\Profiler::start('layout_render');
166 
167  $this->eventManager->dispatch('layout_render_before');
168  $this->eventManager->dispatch('layout_render_before_' . $this->request->getFullActionName());
169 
170  $this->applyHttpHeaders($httpResponse);
171  $this->render($httpResponse);
172 
173  \Magento\Framework\Profiler::stop('layout_render');
174  \Magento\Framework\Profiler::stop('LAYOUT');
175  return $this;
176  }
177 
181  protected function render(HttpResponseInterface $response)
182  {
183  $output = $this->layout->getOutput();
184  $this->translateInline->processResponseBody($output);
185  $response->appendBody($output);
186  return $this;
187  }
188 }
__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)
Definition: Layout.php:72
$response
Definition: 404.php:11
render(HttpResponseInterface $response)
Definition: Layout.php:181
renderResult(ResponseInterface $httpResponse)
Definition: Layout.php:162
applyHttpHeaders(HttpResponseInterface $response)