-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathBlock.php
222 lines (206 loc) · 7.23 KB
/
Block.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\View\Layout\Generator;
use Magento\Framework\View\Layout;
/**
* Class Block
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Block implements Layout\GeneratorInterface
{
/**
* Type of generator
*/
const TYPE = 'block';
/**
* @var \Magento\Framework\View\Element\BlockFactory
*/
protected $blockFactory;
/**
* @var \Magento\Framework\Data\Argument\InterpreterInterface
*/
protected $argumentInterpreter;
/**
* @var \Magento\Framework\Event\ManagerInterface
*/
protected $eventManager;
/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* Constructor
*
* @param \Magento\Framework\View\Element\BlockFactory $blockFactory
* @param \Magento\Framework\Data\Argument\InterpreterInterface $argumentInterpreter
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(
\Magento\Framework\View\Element\BlockFactory $blockFactory,
\Magento\Framework\Data\Argument\InterpreterInterface $argumentInterpreter,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Psr\Log\LoggerInterface $logger
) {
$this->blockFactory = $blockFactory;
$this->argumentInterpreter = $argumentInterpreter;
$this->eventManager = $eventManager;
$this->logger = $logger;
}
/**
* {@inheritdoc}
*
* @return string
*/
public function getType()
{
return self::TYPE;
}
/**
* Creates block object based on data and add it to the layout
*
* @param Layout\Reader\Context $readerContext
* @param Context $generatorContext
* @return $this
*/
public function process(Layout\Reader\Context $readerContext, Layout\Generator\Context $generatorContext)
{
$scheduledStructure = $readerContext->getScheduledStructure();
$layout = $generatorContext->getLayout();
$structure = $generatorContext->getStructure();
/** @var $blocks \Magento\Framework\View\Element\AbstractBlock[] */
$blocks = [];
$blockActions = [];
// Instantiate blocks and collect all actions data
foreach ($scheduledStructure->getElements() as $elementName => $element) {
list($type, $data) = $element;
if ($type === self::TYPE) {
$block = $this->generateBlock($scheduledStructure, $structure, $elementName);
$blocks[$elementName] = $block;
$layout->setBlock($elementName, $block);
if (!empty($data['actions'])) {
$blockActions[$elementName] = $data['actions'];
}
}
}
// Set layout instance to all generated block (trigger _prepareLayout method)
foreach ($blocks as $elementName => $block) {
$block->setLayout($layout);
$this->eventManager->dispatch('core_layout_block_create_after', ['block' => $block]);
$scheduledStructure->unsetElement($elementName);
}
// Run all actions after layout initialization
foreach ($blockActions as $elementName => $actions) {
foreach ($actions as $action) {
list($methodName, $actionArguments) = $action;
$this->generateAction($blocks[$elementName], $methodName, $actionArguments);
}
}
return $this;
}
/**
* Create block and set related data
*
* @param \Magento\Framework\View\Layout\ScheduledStructure $scheduledStructure
* @param \Magento\Framework\View\Layout\Data\Structure $structure
* @param string $elementName
* @return \Magento\Framework\View\Element\AbstractBlock
*/
protected function generateBlock(
Layout\ScheduledStructure $scheduledStructure,
Layout\Data\Structure $structure,
$elementName
) {
list(, $data) = $scheduledStructure->getElement($elementName);
$attributes = $data['attributes'];
if (!empty($attributes['group'])) {
$structure->addToParentGroup($elementName, $attributes['group']);
}
// create block
$className = $attributes['class'];
$block = $this->createBlock($className, $elementName, [
'data' => $this->evaluateArguments($data['arguments'])
]);
if (!empty($attributes['template'])) {
$block->setTemplate($attributes['template']);
}
if (!empty($attributes['ttl'])) {
$ttl = (int)$attributes['ttl'];
$block->setTtl($ttl);
}
return $block;
}
/**
* Create block instance
*
* @param string|\Magento\Framework\View\Element\AbstractBlock $block
* @param string $name
* @param array $arguments
* @return \Magento\Framework\View\Element\AbstractBlock
*/
public function createBlock($block, $name, array $arguments = [])
{
$block = $this->getBlockInstance($block, $arguments);
$block->setType(get_class($block));
$block->setNameInLayout($name);
$block->addData(isset($arguments['data']) ? $arguments['data'] : []);
return $block;
}
/**
* Create block object instance based on block type
*
* @param string|\Magento\Framework\View\Element\AbstractBlock $block
* @param array $arguments
* @throws \Magento\Framework\Model\Exception
* @return \Magento\Framework\View\Element\AbstractBlock
*/
protected function getBlockInstance($block, array $arguments = [])
{
if ($block && is_string($block)) {
try {
$block = $this->blockFactory->createBlock($block, $arguments);
} catch (\ReflectionException $e) {
$this->logger->critical($e->getMessage());
}
}
if (!$block instanceof \Magento\Framework\View\Element\AbstractBlock) {
throw new \Magento\Framework\Model\Exception(
(string)new \Magento\Framework\Phrase('Invalid block type: %1', $block)
);
}
return $block;
}
/**
* Run action defined in layout update
*
* @param \Magento\Framework\View\Element\AbstractBlock $block
* @param string $methodName
* @param array $actionArguments
* @return void
*/
protected function generateAction($block, $methodName, $actionArguments)
{
$profilerKey = 'BLOCK_ACTION:' . $block->getNameInLayout() . '>' . $methodName;
\Magento\Framework\Profiler::start($profilerKey);
$args = $this->evaluateArguments($actionArguments);
call_user_func_array([$block, $methodName], $args);
\Magento\Framework\Profiler::stop($profilerKey);
}
/**
* Compute and return argument values
*
* @param array $arguments
* @return array
*/
protected function evaluateArguments(array $arguments)
{
$result = [];
foreach ($arguments as $argumentName => $argumentData) {
$result[$argumentName] = $this->argumentInterpreter->evaluate($argumentData);
}
return $result;
}
}