-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathUiComponent.php
166 lines (146 loc) · 4.76 KB
/
UiComponent.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\View\Layout\Generator;
use Magento\Framework\View\Element\BlockFactory;
use Magento\Framework\View\Element\UiComponent\ContainerInterface;
use Magento\Framework\View\Element\UiComponent\ContextFactory as UiComponentContextFactory;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponentInterface;
use Magento\Framework\View\Layout\Data\Structure as DataStructure;
use Magento\Framework\View\Layout\Element;
use Magento\Framework\View\Layout\Generator\Context as GeneratorContext;
use Magento\Framework\View\Layout\GeneratorInterface;
use Magento\Framework\View\Layout\Reader\Context as ReaderContext;
use Magento\Framework\View\LayoutInterface;
/**
* Class UiComponent
*/
class UiComponent implements GeneratorInterface
{
/**
* Generator type
*/
const TYPE = 'uiComponent';
/**
* Block container for components
*/
const CONTAINER = \Magento\Framework\View\Element\UiComponent\ContainerInterface::class;
/**
* @var UiComponentFactory
*/
protected $uiComponentFactory;
/**
* @var UiComponentContextFactory
*/
protected $contextFactory;
/**
* @var BlockFactory
*/
private $blockFactory;
/**
* Constructor
*
* @param UiComponentFactory $uiComponentFactory
* @param BlockFactory $blockFactory
* @param UiComponentContextFactory $contextFactory
*/
public function __construct(
UiComponentFactory $uiComponentFactory,
BlockFactory $blockFactory,
UiComponentContextFactory $contextFactory
) {
$this->uiComponentFactory = $uiComponentFactory;
$this->blockFactory = $blockFactory;
$this->contextFactory = $contextFactory;
}
/**
* @inheritdoc
*/
public function getType()
{
return self::TYPE;
}
/**
* Creates UI Component object based on scheduled data and add it to the layout
*
* @param ReaderContext $readerContext
* @param GeneratorContext $generatorContext
* @return $this
*/
public function process(ReaderContext $readerContext, GeneratorContext $generatorContext)
{
$scheduledStructure = $readerContext->getScheduledStructure();
$scheduledElements = $scheduledStructure->getElements();
if (!$scheduledElements) {
return $this;
}
$structure = $generatorContext->getStructure();
$layout = $generatorContext->getLayout();
// Instantiate blocks and collect all actions data
foreach ($scheduledElements as $elementName => $element) {
list($elementType, $data) = $element;
if ($elementType !== Element::TYPE_UI_COMPONENT) {
continue;
}
$layout->setBlock(
$elementName,
$this->generateComponent($structure, $elementName, $data, $layout)
);
$scheduledStructure->unsetElement($elementName);
}
return $this;
}
/**
* Create component object
*
* @param DataStructure $structure
* @param string $elementName
* @param string $data
* @param LayoutInterface $layout
* @return ContainerInterface
*/
protected function generateComponent(DataStructure $structure, $elementName, $data, LayoutInterface $layout)
{
$attributes = $data['attributes'];
if (!empty($attributes['group'])) {
$structure->addToParentGroup($elementName, $attributes['group']);
}
$context = $this->contextFactory->create(
[
'namespace' => $elementName,
'pageLayout' => $layout
]
);
/**
* Structure is required for custom component factory like a 'htmlContent'
*/
$component = $this->uiComponentFactory->create(
$elementName,
null,
['context' => $context, 'structure' => $structure]
);
$this->prepareComponent($component);
/** @var ContainerInterface $blockContainer */
$blockContainer = $this->blockFactory->createBlock(static::CONTAINER, ['component' => $component]);
return $blockContainer;
}
/**
* Call prepare method in the component UI
*
* @param UiComponentInterface $component
* @return void
*/
protected function prepareComponent(UiComponentInterface $component)
{
$childComponents = $component->getChildComponents();
if (!empty($childComponents)) {
foreach ($childComponents as $child) {
$this->prepareComponent($child);
}
}
$component->prepare();
}
}