Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Builder.php
Go to the documentation of this file.
1 <?php
7 
12 
16 class Builder implements BuilderInterface
17 {
21  protected $eventManager;
22 
26  protected $request;
27 
31  protected $layout;
32 
36  protected $isBuilt = false;
37 
43  public function __construct(
45  App\Request\Http $request,
47  ) {
48  $this->layout = $layout;
49  $this->request = $request;
50  $this->eventManager = $eventManager;
51  $this->layout->setBuilder($this);
52  }
53 
59  public function build()
60  {
61  if (!$this->isBuilt) {
62  $this->isBuilt = true;
63  $this->loadLayoutUpdates();
64  $this->generateLayoutXml();
65  $this->generateLayoutBlocks();
66  }
67  return $this->layout;
68  }
69 
75  protected function loadLayoutUpdates()
76  {
77  Profiler::start('LAYOUT');
78  /* dispatch event for adding handles to layout update */
79  $this->eventManager->dispatch(
80  'layout_load_before',
81  ['full_action_name' => $this->request->getFullActionName(), 'layout' => $this->layout]
82  );
83  Profiler::start('layout_load');
84 
85  /* load layout updates by specified handles */
86  $this->layout->getUpdate()->load();
87 
88  Profiler::stop('layout_load');
89  Profiler::stop('LAYOUT');
90  return $this;
91  }
92 
98  protected function generateLayoutXml()
99  {
100  Profiler::start('LAYOUT');
101  Profiler::start('layout_generate_xml');
102 
103  /* generate xml from collected text updates */
104  $this->layout->generateXml();
105 
106  Profiler::stop('layout_generate_xml');
107  Profiler::stop('LAYOUT');
108  return $this;
109  }
110 
116  protected function generateLayoutBlocks()
117  {
118  $this->beforeGenerateBlock();
119 
120  Profiler::start('LAYOUT');
121  /* dispatch event for adding xml layout elements */
122  $this->eventManager->dispatch(
123  'layout_generate_blocks_before',
124  ['full_action_name' => $this->request->getFullActionName(), 'layout' => $this->layout]
125  );
126  Profiler::start('layout_generate_blocks');
127 
128  /* generate blocks from xml layout */
129  $this->layout->generateElements();
130 
131  Profiler::stop('layout_generate_blocks');
132  $this->eventManager->dispatch(
133  'layout_generate_blocks_after',
134  ['full_action_name' => $this->request->getFullActionName(), 'layout' => $this->layout]
135  );
136  Profiler::stop('LAYOUT');
137 
138  $this->afterGenerateBlock();
139 
140  return $this;
141  }
142 
146  protected function beforeGenerateBlock()
147  {
148  return $this;
149  }
150 
154  protected function afterGenerateBlock()
155  {
156  return $this;
157  }
158 }
__construct(View\LayoutInterface $layout, App\Request\Http $request, Event\ManagerInterface $eventManager)
Definition: Builder.php:43