-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathServiceMapFactory.php
More file actions
240 lines (203 loc) · 6.71 KB
/
ServiceMapFactory.php
File metadata and controls
240 lines (203 loc) · 6.71 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
declare(strict_types=1);
namespace Rector\Symfony\ValueObjectFactory;
use Nette\Utils\FileSystem;
use Nette\Utils\Json;
use Nette\Utils\Strings;
use Rector\Symfony\Exception\XmlContainerNotExistsException;
use Rector\Symfony\ValueObject\ServiceDefinition;
use Rector\Symfony\ValueObject\ServiceMap\ServiceMap;
use Rector\Symfony\ValueObject\Tag;
use Rector\Symfony\ValueObject\Tag\EventListenerTag;
use SimpleXMLElement;
final class ServiceMapFactory
{
/**
* @var string
*/
private const TAG = 'tag';
public function createFromFileContent(string $configFilePath): ServiceMap
{
$fileContents = FileSystem::read($configFilePath);
// "@" intentionally
$xml = @simplexml_load_string($fileContents);
if ($xml === false) {
throw new XmlContainerNotExistsException(sprintf('Container "%s" cannot be parsed', $configFilePath));
}
/** @var ServiceDefinition[] $services */
$services = [];
/** @var ServiceDefinition[] $aliases */
$aliases = [];
foreach ($xml->services->service as $def) {
/** @var SimpleXMLElement $attrs */
$attrs = $def->attributes();
if (! (property_exists($attrs, 'id') && $attrs->id instanceof SimpleXMLElement)) {
continue;
}
$def = $this->convertXmlToArray($def);
$tags = $this->createTagFromXmlElement($def);
if (in_array('container.excluded', array_column($tags, 'name'), true)) {
continue;
}
$service = $this->createServiceFromXmlAndTagsData($attrs, $tags);
if ($service->getAlias() !== null) {
$aliases[] = $service;
} else {
$services[$service->getId()] = $service;
}
}
$services = $this->createAliasServiceDefinitions($aliases, $services);
return new ServiceMap($services);
}
public function createEmpty(): ServiceMap
{
return new ServiceMap([]);
}
/**
* @param mixed[] $def
* @return mixed[]
*/
private function createTagFromXmlElement(array $def): array
{
if (! isset($def[self::TAG])) {
return [];
}
$tags = [];
if (is_array($def[self::TAG])) {
$tags = $def[self::TAG];
} else {
$tags[] = $def[self::TAG];
}
return $tags;
}
/**
* @param mixed[] $tags
*/
private function createServiceFromXmlAndTagsData(SimpleXMLElement $attrs, array $tags): ServiceDefinition
{
$tags = $this->createTagsFromData($tags);
return new ServiceDefinition(
str_starts_with((string) $attrs->id, '.') ? Strings::substring(
(string) $attrs->id,
1
) : (string) $attrs->id,
property_exists(
$attrs,
'class'
) && $attrs->class instanceof SimpleXMLElement ? (string) $attrs->class : null,
! (property_exists(
$attrs,
'public'
) && $attrs->public instanceof SimpleXMLElement) || (string) $attrs->public !== 'false',
property_exists(
$attrs,
'synthetic'
) && $attrs->synthetic instanceof SimpleXMLElement && (string) $attrs->synthetic === 'true',
property_exists(
$attrs,
'alias'
) && $attrs->alias instanceof SimpleXMLElement ? (string) $attrs->alias : null,
$tags
);
}
/**
* @param ServiceDefinition[] $aliases
* @param ServiceDefinition[] $services
* @return ServiceDefinition[]
*/
private function createAliasServiceDefinitions(array $aliases, array $services): array
{
foreach ($aliases as $service) {
$alias = $service->getAlias();
if ($alias === null) {
continue;
}
if (! isset($services[$alias])) {
continue;
}
$id = $service->getId();
$services[$id] = new ServiceDefinition(
$id,
$services[$alias]->getClass(),
$service->isPublic(),
$service->isSynthetic(),
$alias,
[]
);
}
return $services;
}
/**
* @param mixed[] $tagsData
* @return Tag[]|EventListenerTag[]
*/
private function createTagsFromData(array $tagsData): array
{
$tagValueObjects = [];
foreach ($tagsData as $key => $tag) {
if (is_string($tag)) {
$tagValueObjects[$key] = new Tag($tag);
continue;
}
$data = $tag;
$name = $data['name'] ?? '';
if ($name === 'kernel.event_listener') {
$tagValueObjects[$key] = new EventListenerTag(
$data['event'] ?? '',
$data['method'] ?? '',
(int) ($data['priority'] ?? 0)
);
} else {
unset($data['name']);
$tagValueObjects[$key] = new Tag($name, $data ?? []);
}
}
return $tagValueObjects;
}
/**
* @return mixed[]
*/
private function convertXmlToArray(SimpleXMLElement $simpleXMLElement): array
{
$data = Json::decode(Json::encode((array) $simpleXMLElement), true);
$data = $this->unWrapAttributes($data);
foreach ($data as $key => $value) {
if (is_array($value)) {
$data = $this->convertedNestedArrayOrXml($value, $data, $key);
} elseif ($value instanceof SimpleXMLElement) {
$data[$key] = $this->convertXmlToArray($value);
}
}
return $data;
}
/**
* @param mixed[] $data
* @return mixed[]
*/
private function unWrapAttributes(array $data): array
{
if (isset($data['@attributes'])) {
foreach ($data['@attributes'] as $key => $value) {
$data[$key] = $value;
}
unset($data['@attributes']);
}
return $data;
}
/**
* @param mixed[] $value
* @param mixed[] $data
* @return mixed[]
*/
private function convertedNestedArrayOrXml(array $value, array $data, string|int $key): array
{
foreach ($value as $subKey => $subValue) {
if ($subValue instanceof SimpleXMLElement) {
$data[$key][$subKey] = $this->convertXmlToArray($subValue);
} elseif (is_array($subValue)) {
$data[$key][$subKey] = $this->unWrapAttributes($subValue);
}
}
return $data;
}
}