Layout.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Result;
  7. use Magento\Framework;
  8. use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
  9. use Magento\Framework\App\ResponseInterface;
  10. use Magento\Framework\Controller\AbstractResult;
  11. use Magento\Framework\View;
  12. /**
  13. * A generic layout response can be used for rendering any kind of layout
  14. * So it comprises a response body from the layout elements it has and sets it to the HTTP response
  15. *
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. *
  18. * @api
  19. * @since 100.0.2
  20. */
  21. class Layout extends AbstractResult
  22. {
  23. /**
  24. * @var \Magento\Framework\View\LayoutFactory
  25. */
  26. protected $layoutFactory;
  27. /**
  28. * @var \Magento\Framework\View\Layout\BuilderFactory
  29. */
  30. protected $layoutBuilderFactory;
  31. /**
  32. * @var \Magento\Framework\View\Layout\ReaderPool
  33. */
  34. protected $layoutReaderPool;
  35. /**
  36. * @var \Magento\Framework\View\LayoutInterface
  37. */
  38. protected $layout;
  39. /**
  40. * @var \Magento\Framework\Translate\InlineInterface
  41. */
  42. protected $translateInline;
  43. /**
  44. * @var \Magento\Framework\Event\ManagerInterface
  45. */
  46. protected $eventManager;
  47. /**
  48. * @var \Magento\Framework\App\Request\Http
  49. */
  50. protected $request;
  51. /**
  52. * Constructor
  53. *
  54. * @param View\Element\Template\Context $context
  55. * @param View\LayoutFactory $layoutFactory
  56. * @param View\Layout\ReaderPool $layoutReaderPool
  57. * @param Framework\Translate\InlineInterface $translateInline
  58. * @param View\Layout\BuilderFactory $layoutBuilderFactory
  59. * @param View\Layout\GeneratorPool $generatorPool
  60. * @param bool $isIsolated
  61. */
  62. public function __construct(
  63. View\Element\Template\Context $context,
  64. View\LayoutFactory $layoutFactory,
  65. View\Layout\ReaderPool $layoutReaderPool,
  66. Framework\Translate\InlineInterface $translateInline,
  67. View\Layout\BuilderFactory $layoutBuilderFactory,
  68. View\Layout\GeneratorPool $generatorPool,
  69. $isIsolated = false
  70. ) {
  71. $this->layoutFactory = $layoutFactory;
  72. $this->layoutBuilderFactory = $layoutBuilderFactory;
  73. $this->layoutReaderPool = $layoutReaderPool;
  74. $this->eventManager = $context->getEventManager();
  75. $this->request = $context->getRequest();
  76. $this->translateInline = $translateInline;
  77. // TODO Shared layout object will be deleted in MAGETWO-28359
  78. $this->layout = $isIsolated
  79. ? $this->layoutFactory->create(['reader' => $this->layoutReaderPool, 'generatorPool' => $generatorPool])
  80. : $context->getLayout();
  81. $this->layout->setGeneratorPool($generatorPool);
  82. $this->initLayoutBuilder();
  83. }
  84. /**
  85. * Create layout builder
  86. *
  87. * @return void
  88. */
  89. protected function initLayoutBuilder()
  90. {
  91. $this->layoutBuilderFactory->create(View\Layout\BuilderFactory::TYPE_LAYOUT, ['layout' => $this->layout]);
  92. }
  93. /**
  94. * Get layout instance for current page
  95. *
  96. * @return \Magento\Framework\View\LayoutInterface
  97. */
  98. public function getLayout()
  99. {
  100. return $this->layout;
  101. }
  102. /**
  103. * @return $this
  104. */
  105. public function addDefaultHandle()
  106. {
  107. $this->addHandle($this->getDefaultLayoutHandle());
  108. return $this;
  109. }
  110. /**
  111. * Retrieve the default layout handle name for the current action
  112. *
  113. * @return string
  114. */
  115. public function getDefaultLayoutHandle()
  116. {
  117. return strtolower($this->request->getFullActionName());
  118. }
  119. /**
  120. * @param string|string[] $handleName
  121. * @return $this
  122. */
  123. public function addHandle($handleName)
  124. {
  125. $this->getLayout()->getUpdate()->addHandle($handleName);
  126. return $this;
  127. }
  128. /**
  129. * Add update to merge object
  130. *
  131. * @param string $update
  132. * @return $this
  133. */
  134. public function addUpdate($update)
  135. {
  136. $this->getLayout()->getUpdate()->addUpdate($update);
  137. return $this;
  138. }
  139. /**
  140. * Render current layout
  141. *
  142. * @param HttpResponseInterface|ResponseInterface $httpResponse
  143. * @return $this
  144. */
  145. public function renderResult(ResponseInterface $httpResponse)
  146. {
  147. \Magento\Framework\Profiler::start('LAYOUT');
  148. \Magento\Framework\Profiler::start('layout_render');
  149. $this->eventManager->dispatch('layout_render_before');
  150. $this->eventManager->dispatch('layout_render_before_' . $this->request->getFullActionName());
  151. $this->applyHttpHeaders($httpResponse);
  152. $this->render($httpResponse);
  153. \Magento\Framework\Profiler::stop('layout_render');
  154. \Magento\Framework\Profiler::stop('LAYOUT');
  155. return $this;
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. protected function render(HttpResponseInterface $response)
  161. {
  162. $output = $this->layout->getOutput();
  163. $this->translateInline->processResponseBody($output);
  164. $response->appendBody($output);
  165. return $this;
  166. }
  167. }