Builder.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Layout;
  7. use Magento\Framework\App;
  8. use Magento\Framework\Event;
  9. use Magento\Framework\Profiler;
  10. use Magento\Framework\View;
  11. /**
  12. * Class Builder
  13. */
  14. class Builder implements BuilderInterface
  15. {
  16. /**
  17. * @var \Magento\Framework\Event\ManagerInterface
  18. */
  19. protected $eventManager;
  20. /**
  21. * @var \Magento\Framework\App\Request\Http
  22. */
  23. protected $request;
  24. /**
  25. * @var \Magento\Framework\View\LayoutInterface
  26. */
  27. protected $layout;
  28. /**
  29. * @var bool
  30. */
  31. protected $isBuilt = false;
  32. /**
  33. * @param View\LayoutInterface $layout
  34. * @param App\Request\Http $request
  35. * @param Event\ManagerInterface $eventManager
  36. */
  37. public function __construct(
  38. View\LayoutInterface $layout,
  39. App\Request\Http $request,
  40. Event\ManagerInterface $eventManager
  41. ) {
  42. $this->layout = $layout;
  43. $this->request = $request;
  44. $this->eventManager = $eventManager;
  45. $this->layout->setBuilder($this);
  46. }
  47. /**
  48. * Build layout structure
  49. *
  50. * @return \Magento\Framework\View\LayoutInterface
  51. */
  52. public function build()
  53. {
  54. if (!$this->isBuilt) {
  55. $this->isBuilt = true;
  56. $this->loadLayoutUpdates();
  57. $this->generateLayoutXml();
  58. $this->generateLayoutBlocks();
  59. }
  60. return $this->layout;
  61. }
  62. /**
  63. * Load layout updates
  64. *
  65. * @return $this
  66. */
  67. protected function loadLayoutUpdates()
  68. {
  69. Profiler::start('LAYOUT');
  70. /* dispatch event for adding handles to layout update */
  71. $this->eventManager->dispatch(
  72. 'layout_load_before',
  73. ['full_action_name' => $this->request->getFullActionName(), 'layout' => $this->layout]
  74. );
  75. Profiler::start('layout_load');
  76. /* load layout updates by specified handles */
  77. $this->layout->getUpdate()->load();
  78. Profiler::stop('layout_load');
  79. Profiler::stop('LAYOUT');
  80. return $this;
  81. }
  82. /**
  83. * Generate layout xml
  84. *
  85. * @return $this
  86. */
  87. protected function generateLayoutXml()
  88. {
  89. Profiler::start('LAYOUT');
  90. Profiler::start('layout_generate_xml');
  91. /* generate xml from collected text updates */
  92. $this->layout->generateXml();
  93. Profiler::stop('layout_generate_xml');
  94. Profiler::stop('LAYOUT');
  95. return $this;
  96. }
  97. /**
  98. * Generate layout blocks
  99. *
  100. * @return $this
  101. */
  102. protected function generateLayoutBlocks()
  103. {
  104. $this->beforeGenerateBlock();
  105. Profiler::start('LAYOUT');
  106. /* dispatch event for adding xml layout elements */
  107. $this->eventManager->dispatch(
  108. 'layout_generate_blocks_before',
  109. ['full_action_name' => $this->request->getFullActionName(), 'layout' => $this->layout]
  110. );
  111. Profiler::start('layout_generate_blocks');
  112. /* generate blocks from xml layout */
  113. $this->layout->generateElements();
  114. Profiler::stop('layout_generate_blocks');
  115. $this->eventManager->dispatch(
  116. 'layout_generate_blocks_after',
  117. ['full_action_name' => $this->request->getFullActionName(), 'layout' => $this->layout]
  118. );
  119. Profiler::stop('LAYOUT');
  120. $this->afterGenerateBlock();
  121. return $this;
  122. }
  123. /**
  124. * @return $this
  125. */
  126. protected function beforeGenerateBlock()
  127. {
  128. return $this;
  129. }
  130. /**
  131. * @return $this
  132. */
  133. protected function afterGenerateBlock()
  134. {
  135. return $this;
  136. }
  137. }