View.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App;
  7. class View implements ViewInterface
  8. {
  9. /**
  10. * @var \Magento\Framework\View\LayoutInterface
  11. */
  12. protected $_layout;
  13. /**
  14. * @var \Magento\Framework\Config\ScopeInterface
  15. */
  16. protected $_configScope;
  17. /**
  18. * @var \Magento\Framework\Event\ManagerInterface
  19. */
  20. protected $_eventManager;
  21. /**
  22. * @var \Magento\Framework\View\Result\Page
  23. */
  24. protected $page;
  25. /**
  26. * @var ActionFlag
  27. */
  28. protected $_actionFlag;
  29. /**
  30. * @var ResponseInterface
  31. */
  32. protected $_response;
  33. /**
  34. * @var RequestInterface
  35. */
  36. protected $_request;
  37. /**
  38. * @var bool
  39. */
  40. protected $_isLayoutLoaded = false;
  41. /**
  42. * @param \Magento\Framework\View\LayoutInterface $layout
  43. * @param RequestInterface $request
  44. * @param ResponseInterface $response
  45. * @param \Magento\Framework\Config\ScopeInterface $configScope
  46. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  47. * @param \Magento\Framework\View\Result\PageFactory $pageFactory
  48. * @param ActionFlag $actionFlag
  49. */
  50. public function __construct(
  51. \Magento\Framework\View\LayoutInterface $layout,
  52. RequestInterface $request,
  53. ResponseInterface $response,
  54. \Magento\Framework\Config\ScopeInterface $configScope,
  55. \Magento\Framework\Event\ManagerInterface $eventManager,
  56. \Magento\Framework\View\Result\PageFactory $pageFactory,
  57. ActionFlag $actionFlag
  58. ) {
  59. $this->_layout = $layout;
  60. $this->_request = $request;
  61. $this->_response = $response;
  62. $this->_configScope = $configScope;
  63. $this->_eventManager = $eventManager;
  64. $this->_actionFlag = $actionFlag;
  65. $this->page = $pageFactory->create(true);
  66. }
  67. /**
  68. * Retrieve current page object
  69. *
  70. * @return \Magento\Framework\View\Result\Page
  71. */
  72. public function getPage()
  73. {
  74. return $this->page;
  75. }
  76. /**
  77. * Retrieve current layout object
  78. *
  79. * @return \Magento\Framework\View\LayoutInterface
  80. */
  81. public function getLayout()
  82. {
  83. return $this->page->getLayout();
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function loadLayout($handles = null, $generateBlocks = true, $generateXml = true, $addActionHandles = true)
  89. {
  90. if ($this->_isLayoutLoaded) {
  91. throw new \RuntimeException('Layout must be loaded only once.');
  92. }
  93. // if handles were specified in arguments load them first
  94. if (!empty($handles)) {
  95. $this->getLayout()->getUpdate()->addHandle($handles);
  96. }
  97. if ($addActionHandles) {
  98. // add default layout handles for this action
  99. $this->page->initLayout();
  100. }
  101. $this->loadLayoutUpdates();
  102. if (!$generateXml) {
  103. return $this;
  104. }
  105. $this->generateLayoutXml();
  106. if (!$generateBlocks) {
  107. return $this;
  108. }
  109. $this->generateLayoutBlocks();
  110. $this->_isLayoutLoaded = true;
  111. return $this;
  112. }
  113. /**
  114. * Retrieve the default layout handle name for the current action
  115. *
  116. * @return string
  117. */
  118. public function getDefaultLayoutHandle()
  119. {
  120. return $this->page->getDefaultLayoutHandle();
  121. }
  122. /**
  123. * Add layout handle by full controller action name
  124. *
  125. * @return $this
  126. */
  127. public function addActionLayoutHandles()
  128. {
  129. $this->getLayout()->getUpdate()->addHandle($this->getDefaultLayoutHandle());
  130. return $this;
  131. }
  132. /**
  133. * Add layout updates handles associated with the action page
  134. *
  135. * @param array|null $parameters page parameters
  136. * @param string|null $defaultHandle
  137. * @return bool
  138. */
  139. public function addPageLayoutHandles(array $parameters = [], $defaultHandle = null)
  140. {
  141. return $this->page->addPageLayoutHandles($parameters, $defaultHandle);
  142. }
  143. /**
  144. * Load layout updates
  145. *
  146. * @return $this
  147. */
  148. public function loadLayoutUpdates()
  149. {
  150. $this->page->getConfig()->publicBuild();
  151. return $this;
  152. }
  153. /**
  154. * Generate layout xml
  155. *
  156. * @return $this
  157. */
  158. public function generateLayoutXml()
  159. {
  160. $this->page->getConfig()->publicBuild();
  161. return $this;
  162. }
  163. /**
  164. * Generate layout blocks
  165. *
  166. * @return $this
  167. */
  168. public function generateLayoutBlocks()
  169. {
  170. $this->page->getConfig()->publicBuild();
  171. return $this;
  172. }
  173. /**
  174. * Rendering layout
  175. *
  176. * @param string $output
  177. * @return $this
  178. */
  179. public function renderLayout($output = '')
  180. {
  181. if ($this->_actionFlag->get('', 'no-renderLayout')) {
  182. return $this;
  183. }
  184. \Magento\Framework\Profiler::start('LAYOUT');
  185. \Magento\Framework\Profiler::start('layout_render');
  186. if ('' !== $output) {
  187. $this->getLayout()->addOutputElement($output);
  188. }
  189. $this->_eventManager->dispatch('controller_action_layout_render_before');
  190. $this->_eventManager->dispatch(
  191. 'controller_action_layout_render_before_' . $this->_request->getFullActionName()
  192. );
  193. $this->page->renderResult($this->_response);
  194. \Magento\Framework\Profiler::stop('layout_render');
  195. \Magento\Framework\Profiler::stop('LAYOUT');
  196. return $this;
  197. }
  198. /**
  199. * Set isLayoutLoaded flag
  200. *
  201. * @param bool $value
  202. * @return void
  203. */
  204. public function setIsLayoutLoaded($value)
  205. {
  206. $this->_isLayoutLoaded = $value;
  207. }
  208. /**
  209. * Returns is layout loaded
  210. *
  211. * @return bool
  212. */
  213. public function isLayoutLoaded()
  214. {
  215. return $this->_isLayoutLoaded;
  216. }
  217. }