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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Ui\Config;
use Magento\Framework\Config\ConverterInterface as ConfigConverter;
use Magento\Framework\Config\Dom\ValidationException;
use Magento\Framework\Config\FileResolverInterface;
use Magento\Framework\Config\ReaderInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;
/**
* UI Component configuration reader
*/
class Reader implements ReaderInterface
{
/**
* List of identifier attributes for merging
*
* @var array
*/
private $idAttributes = ['/' => 'name'];
/**
* @var Reader\Definition
*/
private $definitionReader;
/**
* @var ReaderFactory
*/
private $readerFactory;
/**
* @var FileResolverInterface
*/
private $fileResolver;
/**
* @var ConfigConverter
*/
private $converter;
/**
* @var Reader\DomFactory
*/
private $readerDomFactory;
/**
* The name of file that stores Ui configuration
*
* @var string
*/
private $fileName;
/**
* Reader constructor.
*
* @param string $fileName
* @param FileResolverInterface $fileResolver
* @param ConfigConverter $converter
* @param Reader\Definition $definitionReader
* @param ReaderFactory $readerFactory
* @param Reader\DomFactory $readerDomFactory
* @param array $idAttributes
*/
public function __construct(
$fileName,
FileResolverInterface $fileResolver,
ConfigConverter $converter,
Reader\Definition $definitionReader,
ReaderFactory $readerFactory,
Reader\DomFactory $readerDomFactory,
array $idAttributes = []
) {
$this->fileName = $fileName;
$this->fileResolver = $fileResolver;
$this->converter = $converter;
$this->definitionReader = $definitionReader;
$this->readerFactory = $readerFactory;
$this->readerDomFactory = $readerDomFactory;
$this->idAttributes = array_replace($this->idAttributes, $idAttributes);
}
/**
* Load configuration scope
*
* @param string|null $scope
* @return array
*/
public function read($scope = null)
{
$scope = $scope ?: 'global';
$fileList = $this->fileResolver->get($this->fileName, $scope);
if (!count($fileList)) {
return [];
}
$output = $this->readFiles($fileList);
return $output;
}
/**
* Read, merge configuration files and validate resulted configuration
*
* @param array $fileList
* @return array
* @throws LocalizedException
*/
private function readFiles($fileList)
{
/** @var \Magento\Ui\Config\Reader\Dom $configMerger */
$configMerger = null;
$output = [];
foreach ($fileList as $key => $content) {
try {
$configMerger = $this->readerDomFactory->create(
[
'xml' => $content,
'idAttributes' => $this->idAttributes,
]
);
$output = array_replace_recursive($output, $this->converter->convert($configMerger->getDom()));
} catch (ValidationException $e) {
throw new LocalizedException(
new Phrase(
'The XML in file "%1" is invalid:' . "\n%2\nVerify the XML and try again.",
[$key, $e->getMessage()]
)
);
}
}
$definitionData = $this->definitionReader->read();
if (isset($output['attributes']['extends'])) {
$extendsReader = $this->readerFactory->create(
[
'fileName' => sprintf(
Data::SEARCH_PATTERN,
$output['attributes']['extends']
)
]
);
$extendsData = $extendsReader->read();
$output = array_replace_recursive($extendsData, $output);
}
$output = $this->mergeDefinition($output, $definitionData);
return $output;
}
/**
* Merge definition to ui component configuration
*
* @param array $component
* @param array $definitions
* @return array
*/
private function mergeDefinition(array $component, array $definitions)
{
foreach ($component['children'] as $name => $child) {
$component['children'][$name] = $this->mergeDefinition($child, $definitions);
}
if (isset($component['uiComponentType'])) {
$definition = isset($definitions[$component['uiComponentType']])
? $definitions[$component['uiComponentType']]
: [];
$component = array_replace_recursive($definition, $component);
unset($component['uiComponentType']);
}
return $component;
}
}