|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* DocumentMap class file |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\DocumentBundle\DependencyInjection\Compiler\Utils; |
|
7
|
|
|
|
|
8
|
|
|
use Symfony\Component\Finder\Finder; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Document map |
|
12
|
|
|
* |
|
13
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
14
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
15
|
|
|
* @link http://swisscom.ch |
|
16
|
|
|
*/ |
|
17
|
|
|
class DocumentMap |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $mappings = []; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var Document[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $documents = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Constructor |
|
30
|
|
|
* |
|
31
|
|
|
* @param Finder $doctrineFinder Doctrine mapping finder |
|
32
|
|
|
* @param Finder $serializerFinder Serializer mapping finder |
|
33
|
|
|
* @param Finder $validationFinder Validation mapping finder |
|
34
|
|
|
*/ |
|
35
|
8 |
|
public function __construct(Finder $doctrineFinder, Finder $serializerFinder, Finder $validationFinder) |
|
36
|
|
|
{ |
|
37
|
8 |
|
$doctrineMap = $this->loadDoctrineClassMap($doctrineFinder); |
|
38
|
8 |
|
$serializerMap = $this->loadSerializerClassMap($serializerFinder); |
|
39
|
8 |
|
$validationMap = $this->loadValidationClassMap($validationFinder); |
|
40
|
|
|
|
|
41
|
8 |
|
foreach ($doctrineMap as $className => $doctrineMapping) { |
|
42
|
8 |
|
$this->mappings[$className] = [ |
|
43
|
8 |
|
'doctrine' => $doctrineMap[$className], |
|
44
|
8 |
|
'serializer' => isset($serializerMap[$className]) ? $serializerMap[$className] : null, |
|
45
|
8 |
|
'validation' => isset($validationMap[$className]) ? $validationMap[$className] : null, |
|
46
|
|
|
]; |
|
47
|
|
|
} |
|
48
|
8 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get document |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $className Document class |
|
54
|
|
|
* @return Document |
|
55
|
|
|
*/ |
|
56
|
8 |
|
public function getDocument($className) |
|
57
|
|
|
{ |
|
58
|
8 |
|
if (isset($this->documents[$className])) { |
|
59
|
8 |
|
return $this->documents[$className]; |
|
60
|
|
|
} |
|
61
|
8 |
|
if (!isset($this->mappings[$className])) { |
|
62
|
|
|
throw new \InvalidArgumentException(sprintf('No XML mapping found for document "%s"', $className)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
8 |
|
return $this->documents[$className] = $this->processDocument( |
|
66
|
|
|
$className, |
|
67
|
8 |
|
$this->mappings[$className]['doctrine'], |
|
68
|
8 |
|
$this->mappings[$className]['serializer'], |
|
69
|
8 |
|
$this->mappings[$className]['validation'] |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get all documents |
|
75
|
|
|
* |
|
76
|
|
|
* @return Document[] |
|
77
|
|
|
*/ |
|
78
|
6 |
|
public function getDocuments() |
|
79
|
|
|
{ |
|
80
|
6 |
|
return array_map([$this, 'getDocument'], array_keys($this->mappings)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Process document |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $className Class name |
|
87
|
|
|
* @param \DOMElement $doctrineMapping Doctrine XML mapping |
|
88
|
|
|
* @param \DOMElement $serializerMapping Serializer XML mapping |
|
89
|
|
|
* @param \DOMElement $validationMapping Validation XML mapping |
|
90
|
|
|
* @return Document |
|
91
|
|
|
*/ |
|
92
|
8 |
|
private function processDocument( |
|
93
|
|
|
$className, |
|
94
|
|
|
\DOMElement $doctrineMapping, |
|
95
|
|
|
\DOMElement $serializerMapping = null, |
|
96
|
|
|
\DOMElement $validationMapping = null |
|
97
|
|
|
) { |
|
98
|
8 |
|
if ($serializerMapping === null) { |
|
99
|
2 |
|
$serializerFields = []; |
|
100
|
|
|
} else { |
|
101
|
8 |
|
$serializerFields = array_reduce( |
|
102
|
8 |
|
$this->getSerializerFields($serializerMapping), |
|
103
|
|
|
function (array $fields, array $field) { |
|
104
|
8 |
|
$fields[$field['fieldName']] = $field; |
|
105
|
8 |
|
return $fields; |
|
106
|
8 |
|
}, |
|
107
|
8 |
|
[] |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
8 |
|
if ($validationMapping === null) { |
|
112
|
2 |
|
$validationFields = []; |
|
113
|
|
|
} else { |
|
114
|
8 |
|
$validationFields = array_reduce( |
|
115
|
8 |
|
$this->getValidationFields($validationMapping), |
|
116
|
|
|
function (array $fields, array $field) { |
|
117
|
5 |
|
$fields[$field['fieldName']] = $field; |
|
118
|
5 |
|
return $fields; |
|
119
|
8 |
|
}, |
|
120
|
8 |
|
[] |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
8 |
|
$fields = []; |
|
125
|
8 |
|
foreach ($this->getDoctrineFields($doctrineMapping) as $doctrineField) { |
|
126
|
8 |
|
$serializerField = isset($serializerFields[$doctrineField['name']]) ? |
|
127
|
8 |
|
$serializerFields[$doctrineField['name']] : |
|
128
|
8 |
|
null; |
|
129
|
8 |
|
$validationField = isset($validationFields[$doctrineField['name']]) ? |
|
130
|
5 |
|
$validationFields[$doctrineField['name']] : |
|
131
|
8 |
|
null; |
|
132
|
|
|
|
|
133
|
8 |
|
if ($doctrineField['type'] === 'collection') { |
|
134
|
2 |
|
$fields[] = new ArrayField( |
|
135
|
2 |
|
$serializerField === null ? 'array<string>' : $serializerField['fieldType'], |
|
136
|
2 |
|
$doctrineField['name'], |
|
137
|
2 |
|
$serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'], |
|
138
|
2 |
|
$serializerField === null ? false : $serializerField['readOnly'], |
|
139
|
2 |
|
$validationField === null ? false : $validationField['required'], |
|
140
|
2 |
|
$serializerField === null ? false : $serializerField['searchable'] |
|
|
|
|
|
|
141
|
|
|
); |
|
142
|
|
|
} else { |
|
143
|
8 |
|
$fields[] = new Field( |
|
144
|
8 |
|
$doctrineField['type'], |
|
145
|
8 |
|
$doctrineField['name'], |
|
146
|
8 |
|
$serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'], |
|
147
|
8 |
|
$serializerField === null ? false : $serializerField['readOnly'], |
|
148
|
8 |
|
$validationField === null ? false : $validationField['required'], |
|
149
|
8 |
|
$serializerField === null ? false : $serializerField['searchable'] |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
8 |
|
foreach ($this->getDoctrineEmbedOneFields($doctrineMapping) as $doctrineField) { |
|
154
|
8 |
|
$serializerField = isset($serializerFields[$doctrineField['name']]) ? |
|
155
|
8 |
|
$serializerFields[$doctrineField['name']] : |
|
156
|
8 |
|
null; |
|
157
|
8 |
|
$validationField = isset($validationFields[$doctrineField['name']]) ? |
|
158
|
5 |
|
$validationFields[$doctrineField['name']] : |
|
159
|
8 |
|
null; |
|
160
|
|
|
|
|
161
|
8 |
|
$fields[] = new EmbedOne( |
|
162
|
8 |
|
$this->getDocument($doctrineField['type']), |
|
163
|
8 |
|
$doctrineField['name'], |
|
164
|
8 |
|
$serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'], |
|
165
|
8 |
|
$serializerField === null ? false : $serializerField['readOnly'], |
|
166
|
8 |
|
$validationField === null ? false : $validationField['required'], |
|
167
|
8 |
|
$serializerField === null ? false : $serializerField['searchable'] |
|
|
|
|
|
|
168
|
|
|
); |
|
169
|
|
|
} |
|
170
|
8 |
|
foreach ($this->getDoctrineEmbedManyFields($doctrineMapping) as $doctrineField) { |
|
171
|
8 |
|
$serializerField = isset($serializerFields[$doctrineField['name']]) ? |
|
172
|
8 |
|
$serializerFields[$doctrineField['name']] : |
|
173
|
8 |
|
null; |
|
174
|
8 |
|
$validationField = isset($validationFields[$doctrineField['name']]) ? |
|
175
|
2 |
|
$validationFields[$doctrineField['name']] : |
|
176
|
8 |
|
null; |
|
177
|
|
|
|
|
178
|
8 |
|
$fields[] = new EmbedMany( |
|
179
|
8 |
|
$this->getDocument($doctrineField['type']), |
|
180
|
8 |
|
$doctrineField['name'], |
|
181
|
8 |
|
$serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'], |
|
182
|
8 |
|
$serializerField === null ? false : $serializerField['readOnly'], |
|
183
|
8 |
|
$validationField === null ? false : $validationField['required'] |
|
184
|
|
|
); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
8 |
|
return new Document($className, $fields); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Load doctrine class map |
|
192
|
|
|
* |
|
193
|
|
|
* @param Finder $finder Mapping finder |
|
194
|
|
|
* @return array |
|
195
|
|
|
*/ |
|
196
|
8 |
View Code Duplication |
private function loadDoctrineClassMap(Finder $finder) |
|
|
|
|
|
|
197
|
|
|
{ |
|
198
|
8 |
|
$classMap = []; |
|
199
|
8 |
|
foreach ($finder as $file) { |
|
200
|
8 |
|
$document = new \DOMDocument(); |
|
201
|
8 |
|
$document->load($file); |
|
202
|
|
|
|
|
203
|
8 |
|
$xpath = new \DOMXPath($document); |
|
204
|
8 |
|
$xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping'); |
|
205
|
|
|
|
|
206
|
8 |
|
$classMap = array_reduce( |
|
207
|
8 |
|
iterator_to_array($xpath->query('//*[self::doctrine:document or self::doctrine:embedded-document]')), |
|
208
|
|
|
function (array $classMap, \DOMElement $element) { |
|
209
|
8 |
|
$classMap[$element->getAttribute('name')] = $element; |
|
210
|
8 |
|
return $classMap; |
|
211
|
8 |
|
}, |
|
212
|
|
|
$classMap |
|
213
|
|
|
); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
8 |
|
return $classMap; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Load serializer class map |
|
221
|
|
|
* |
|
222
|
|
|
* @param Finder $finder Mapping finder |
|
223
|
|
|
* @return array |
|
224
|
|
|
*/ |
|
225
|
8 |
View Code Duplication |
private function loadSerializerClassMap(Finder $finder) |
|
|
|
|
|
|
226
|
|
|
{ |
|
227
|
8 |
|
$classMap = []; |
|
228
|
8 |
|
foreach ($finder as $file) { |
|
229
|
8 |
|
$document = new \DOMDocument(); |
|
230
|
8 |
|
$document->load($file); |
|
231
|
|
|
|
|
232
|
8 |
|
$xpath = new \DOMXPath($document); |
|
233
|
|
|
|
|
234
|
8 |
|
$classMap = array_reduce( |
|
235
|
8 |
|
iterator_to_array($xpath->query('//class')), |
|
236
|
|
|
function (array $classMap, \DOMElement $element) { |
|
237
|
8 |
|
$classMap[$element->getAttribute('name')] = $element; |
|
238
|
8 |
|
return $classMap; |
|
239
|
8 |
|
}, |
|
240
|
|
|
$classMap |
|
241
|
|
|
); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
8 |
|
return $classMap; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Load validation class map |
|
249
|
|
|
* |
|
250
|
|
|
* @param Finder $finder Mapping finder |
|
251
|
|
|
* @return array |
|
252
|
|
|
*/ |
|
253
|
8 |
View Code Duplication |
private function loadValidationClassMap(Finder $finder) |
|
|
|
|
|
|
254
|
|
|
{ |
|
255
|
8 |
|
$classMap = []; |
|
256
|
8 |
|
foreach ($finder as $file) { |
|
257
|
8 |
|
$document = new \DOMDocument(); |
|
258
|
8 |
|
$document->load($file); |
|
259
|
|
|
|
|
260
|
8 |
|
$xpath = new \DOMXPath($document); |
|
261
|
8 |
|
$xpath->registerNamespace('constraint', 'http://symfony.com/schema/dic/constraint-mapping'); |
|
262
|
|
|
|
|
263
|
8 |
|
$classMap = array_reduce( |
|
264
|
8 |
|
iterator_to_array($xpath->query('//constraint:class')), |
|
265
|
|
|
function (array $classMap, \DOMElement $element) { |
|
266
|
8 |
|
$classMap[$element->getAttribute('name')] = $element; |
|
267
|
8 |
|
return $classMap; |
|
268
|
8 |
|
}, |
|
269
|
|
|
$classMap |
|
270
|
|
|
); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
8 |
|
return $classMap; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Get serializer fields |
|
278
|
|
|
* |
|
279
|
|
|
* @param \DOMElement $mapping Serializer XML mapping |
|
280
|
|
|
* @return array |
|
281
|
|
|
*/ |
|
282
|
8 |
|
private function getSerializerFields(\DOMElement $mapping) |
|
283
|
|
|
{ |
|
284
|
8 |
|
$xpath = new \DOMXPath($mapping->ownerDocument); |
|
285
|
|
|
|
|
286
|
8 |
|
return array_map( |
|
287
|
|
|
function (\DOMElement $element) { |
|
288
|
|
|
return [ |
|
289
|
8 |
|
'fieldName' => $element->getAttribute('name'), |
|
290
|
8 |
|
'fieldType' => $this->getSerializerFieldType($element), |
|
291
|
8 |
|
'exposedName' => $element->getAttribute('serialized-name') ?: $element->getAttribute('name'), |
|
292
|
8 |
|
'readOnly' => $element->getAttribute('read-only') === 'true', |
|
293
|
8 |
|
'searchable' => $element->getAttribute('searchable') === 'true', |
|
294
|
|
|
]; |
|
295
|
8 |
|
}, |
|
296
|
8 |
|
iterator_to_array($xpath->query('property', $mapping)) |
|
297
|
|
|
); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Get serializer field type |
|
302
|
|
|
* |
|
303
|
|
|
* @param \DOMElement $field Field node |
|
304
|
|
|
* @return string|null |
|
305
|
|
|
*/ |
|
306
|
8 |
|
private function getSerializerFieldType(\DOMElement $field) |
|
307
|
|
|
{ |
|
308
|
8 |
|
if ($field->getAttribute('type')) { |
|
309
|
8 |
|
return $field->getAttribute('type'); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
2 |
|
$xpath = new \DOMXPath($field->ownerDocument); |
|
313
|
|
|
|
|
314
|
2 |
|
$type = $xpath->query('type', $field)->item(0); |
|
315
|
2 |
|
return $type === null ? null : $type->nodeValue; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Get validation fields |
|
320
|
|
|
* |
|
321
|
|
|
* @param \DOMElement $mapping Validation XML mapping |
|
322
|
|
|
* @return array |
|
323
|
|
|
*/ |
|
324
|
8 |
|
private function getValidationFields(\DOMElement $mapping) |
|
325
|
|
|
{ |
|
326
|
8 |
|
$xpath = new \DOMXPath($mapping->ownerDocument); |
|
327
|
8 |
|
$xpath->registerNamespace('constraint', 'http://symfony.com/schema/dic/constraint-mapping'); |
|
328
|
|
|
|
|
329
|
8 |
|
return array_map( |
|
330
|
|
|
function (\DOMElement $element) use ($xpath) { |
|
331
|
5 |
|
$constraints = $xpath->query('constraint:constraint[@name="NotBlank" or @name="NotNull"]', $element); |
|
332
|
|
|
return [ |
|
333
|
5 |
|
'fieldName' => $element->getAttribute('name'), |
|
334
|
5 |
|
'required' => $constraints->length > 0, |
|
335
|
|
|
]; |
|
336
|
8 |
|
}, |
|
337
|
8 |
|
iterator_to_array($xpath->query('constraint:property', $mapping)) |
|
338
|
|
|
); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* Get doctrine document fields |
|
343
|
|
|
* |
|
344
|
|
|
* @param \DOMElement $mapping Doctrine XML mapping |
|
345
|
|
|
* @return array |
|
346
|
|
|
*/ |
|
347
|
8 |
View Code Duplication |
private function getDoctrineFields(\DOMElement $mapping) |
|
|
|
|
|
|
348
|
|
|
{ |
|
349
|
8 |
|
$xpath = new \DOMXPath($mapping->ownerDocument); |
|
350
|
8 |
|
$xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping'); |
|
351
|
|
|
|
|
352
|
8 |
|
return array_map( |
|
353
|
|
|
function (\DOMElement $element) { |
|
354
|
|
|
return [ |
|
355
|
8 |
|
'name' => $element->getAttribute('fieldName'), |
|
356
|
8 |
|
'type' => $element->getAttribute('type'), |
|
357
|
|
|
]; |
|
358
|
8 |
|
}, |
|
359
|
8 |
|
iterator_to_array($xpath->query('doctrine:field', $mapping)) |
|
360
|
|
|
); |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* Get doctrine document embed-one fields |
|
365
|
|
|
* |
|
366
|
|
|
* @param \DOMElement $mapping Doctrine XML mapping |
|
367
|
|
|
* @return array |
|
368
|
|
|
*/ |
|
369
|
8 |
View Code Duplication |
private function getDoctrineEmbedOneFields(\DOMElement $mapping) |
|
|
|
|
|
|
370
|
|
|
{ |
|
371
|
8 |
|
$xpath = new \DOMXPath($mapping->ownerDocument); |
|
372
|
8 |
|
$xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping'); |
|
373
|
|
|
|
|
374
|
8 |
|
return array_map( |
|
375
|
|
|
function (\DOMElement $element) { |
|
376
|
|
|
return [ |
|
377
|
8 |
|
'name' => $element->getAttribute('field'), |
|
378
|
8 |
|
'type' => $element->getAttribute('target-document'), |
|
379
|
|
|
]; |
|
380
|
8 |
|
}, |
|
381
|
8 |
|
iterator_to_array($xpath->query('*[self::doctrine:embed-one or self::doctrine:reference-one]', $mapping)) |
|
382
|
|
|
); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* Get doctrine document embed-many fields |
|
387
|
|
|
* |
|
388
|
|
|
* @param \DOMElement $mapping Doctrine XML mapping |
|
389
|
|
|
* @return array |
|
390
|
|
|
*/ |
|
391
|
8 |
View Code Duplication |
private function getDoctrineEmbedManyFields(\DOMElement $mapping) |
|
|
|
|
|
|
392
|
|
|
{ |
|
393
|
8 |
|
$xpath = new \DOMXPath($mapping->ownerDocument); |
|
394
|
8 |
|
$xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping'); |
|
395
|
|
|
|
|
396
|
8 |
|
return array_map( |
|
397
|
8 |
|
function (\DOMElement $element) { |
|
398
|
|
|
return [ |
|
399
|
8 |
|
'name' => $element->getAttribute('field'), |
|
400
|
8 |
|
'type' => $element->getAttribute('target-document'), |
|
401
|
|
|
]; |
|
402
|
8 |
|
}, |
|
403
|
8 |
|
iterator_to_array($xpath->query('*[self::doctrine:embed-many or self::doctrine:reference-many]', $mapping)) |
|
404
|
|
|
); |
|
405
|
|
|
} |
|
406
|
|
|
} |
|
407
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.