1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Mapping\Driver; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\Criteria; |
8
|
|
|
use Doctrine\DBAL\DBALException; |
9
|
|
|
use Doctrine\ORM\Annotation; |
10
|
|
|
use Doctrine\ORM\Events; |
11
|
|
|
use Doctrine\ORM\Mapping; |
12
|
|
|
use Doctrine\ORM\Mapping\Builder; |
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
use SimpleXMLElement; |
15
|
|
|
use function class_exists; |
16
|
|
|
use function constant; |
17
|
|
|
use function explode; |
18
|
|
|
use function file_get_contents; |
19
|
|
|
use function in_array; |
20
|
|
|
use function simplexml_load_string; |
21
|
|
|
use function sprintf; |
22
|
|
|
use function str_replace; |
23
|
|
|
use function strtoupper; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* XmlDriver is a metadata driver that enables mapping through XML files. |
27
|
|
|
*/ |
28
|
|
|
class XmlDriver extends FileDriver |
29
|
|
|
{ |
30
|
|
|
public const DEFAULT_FILE_EXTENSION = '.dcm.xml'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritDoc} |
34
|
|
|
*/ |
35
|
41 |
|
public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION) |
36
|
|
|
{ |
37
|
41 |
|
parent::__construct($locator, $fileExtension); |
38
|
41 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
* |
43
|
|
|
* @throws DBALException |
44
|
|
|
*/ |
45
|
36 |
|
public function loadMetadataForClass( |
46
|
|
|
string $className, |
47
|
|
|
?Mapping\ComponentMetadata $parent, |
48
|
|
|
Mapping\ClassMetadataBuildingContext $metadataBuildingContext |
49
|
|
|
) : Mapping\ComponentMetadata { |
50
|
36 |
|
$metadata = new Mapping\ClassMetadata($className, $parent, $metadataBuildingContext); |
51
|
|
|
|
52
|
|
|
/** @var SimpleXMLElement $xmlRoot */ |
53
|
36 |
|
$xmlRoot = $this->getElement($className); |
54
|
|
|
|
55
|
34 |
|
if ($xmlRoot->getName() === 'entity') { |
56
|
34 |
|
if (isset($xmlRoot['repository-class'])) { |
57
|
|
|
$metadata->setCustomRepositoryClassName((string) $xmlRoot['repository-class']); |
58
|
|
|
} |
59
|
|
|
|
60
|
34 |
|
if (isset($xmlRoot['read-only']) && $this->evaluateBoolean($xmlRoot['read-only'])) { |
61
|
34 |
|
$metadata->asReadOnly(); |
62
|
|
|
} |
63
|
5 |
|
} elseif ($xmlRoot->getName() === 'mapped-superclass') { |
64
|
5 |
|
if (isset($xmlRoot['repository-class'])) { |
65
|
1 |
|
$metadata->setCustomRepositoryClassName((string) $xmlRoot['repository-class']); |
66
|
|
|
} |
67
|
|
|
|
68
|
5 |
|
$metadata->isMappedSuperclass = true; |
69
|
|
|
} elseif ($xmlRoot->getName() === 'embeddable') { |
70
|
|
|
$metadata->isEmbeddedClass = true; |
71
|
|
|
} else { |
72
|
|
|
throw Mapping\MappingException::classIsNotAValidEntityOrMappedSuperClass($className); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Process table information |
76
|
34 |
|
$parent = $metadata->getParent(); |
77
|
|
|
|
78
|
34 |
|
if ($parent && $parent->inheritanceType === Mapping\InheritanceType::SINGLE_TABLE) { |
79
|
|
|
// Handle the case where a middle mapped super class inherits from a single table inheritance tree. |
80
|
|
|
do { |
81
|
2 |
|
if (! $parent->isMappedSuperclass) { |
82
|
2 |
|
$metadata->setTable($parent->table); |
83
|
|
|
|
84
|
2 |
|
break; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$parent = $parent->getParent(); |
88
|
2 |
|
} while ($parent !== null); |
89
|
|
|
} else { |
90
|
34 |
|
$tableAnnotation = new Annotation\Table(); |
91
|
|
|
|
92
|
|
|
// Evaluate <entity...> attributes |
93
|
34 |
|
if (isset($xmlRoot['table'])) { |
94
|
12 |
|
$tableAnnotation->name = (string) $xmlRoot['table']; |
95
|
|
|
} |
96
|
|
|
|
97
|
34 |
|
if (isset($xmlRoot['schema'])) { |
98
|
2 |
|
$tableAnnotation->schema = (string) $xmlRoot['schema']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Evaluate <indexes...> |
102
|
34 |
|
if (isset($xmlRoot->indexes)) { |
103
|
4 |
|
$tableAnnotation->indexes = $this->parseIndexes($xmlRoot->indexes->children()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Evaluate <unique-constraints..> |
107
|
34 |
|
if (isset($xmlRoot->{'unique-constraints'})) { |
108
|
3 |
|
$tableAnnotation->uniqueConstraints = $this->parseUniqueConstraints($xmlRoot->{'unique-constraints'}->children()); |
109
|
|
|
} |
110
|
|
|
|
111
|
34 |
|
if (isset($xmlRoot->options)) { |
112
|
3 |
|
$tableAnnotation->options = $this->parseOptions($xmlRoot->options->children()); |
113
|
|
|
} |
114
|
|
|
|
115
|
34 |
|
$tableBuilder = new Builder\TableMetadataBuilder($metadataBuildingContext); |
116
|
|
|
|
117
|
|
|
$tableBuilder |
118
|
34 |
|
->withEntityClassMetadata($metadata) |
119
|
34 |
|
->withTableAnnotation($tableAnnotation); |
120
|
|
|
|
121
|
34 |
|
$metadata->setTable($tableBuilder->build()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Evaluate second level cache |
125
|
34 |
|
if (isset($xmlRoot->cache)) { |
126
|
2 |
|
$cacheBuilder = new Builder\CacheMetadataBuilder($metadataBuildingContext); |
127
|
|
|
|
128
|
|
|
$cacheBuilder |
129
|
2 |
|
->withComponentMetadata($metadata) |
130
|
2 |
|
->withCacheAnnotation($this->convertCacheElementToCacheAnnotation($xmlRoot->cache)); |
131
|
|
|
|
132
|
2 |
|
$metadata->setCache($cacheBuilder->build()); |
133
|
|
|
} |
134
|
|
|
|
135
|
34 |
|
if (isset($xmlRoot['inheritance-type'])) { |
136
|
10 |
|
$inheritanceType = strtoupper((string) $xmlRoot['inheritance-type']); |
137
|
|
|
|
138
|
10 |
|
$metadata->setInheritanceType( |
139
|
10 |
|
constant(sprintf('%s::%s', Mapping\InheritanceType::class, $inheritanceType)) |
140
|
|
|
); |
141
|
|
|
|
142
|
10 |
|
if ($metadata->inheritanceType !== Mapping\InheritanceType::NONE) { |
143
|
10 |
|
$discriminatorColumnBuilder = new Builder\DiscriminatorColumnMetadataBuilder($metadataBuildingContext); |
144
|
|
|
|
145
|
|
|
$discriminatorColumnBuilder |
146
|
10 |
|
->withComponentMetadata($metadata) |
147
|
10 |
|
->withDiscriminatorColumnAnnotation( |
148
|
10 |
|
isset($xmlRoot->{'discriminator-column'}) |
149
|
8 |
|
? $this->convertDiscrimininatorColumnElementToDiscriminatorColumnAnnotation($xmlRoot->{'discriminator-column'}) |
150
|
10 |
|
: null |
151
|
|
|
); |
152
|
|
|
|
153
|
10 |
|
$metadata->setDiscriminatorColumn($discriminatorColumnBuilder->build()); |
154
|
|
|
|
155
|
|
|
// Evaluate <discriminator-map...> |
156
|
10 |
|
if (isset($xmlRoot->{'discriminator-map'})) { |
157
|
10 |
|
$map = []; |
158
|
|
|
|
159
|
10 |
|
foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} as $discrMapElement) { |
160
|
10 |
|
$map[(string) $discrMapElement['value']] = (string) $discrMapElement['class']; |
161
|
|
|
} |
162
|
|
|
|
163
|
10 |
|
$metadata->setDiscriminatorMap($map); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// Evaluate <change-tracking-policy...> |
169
|
34 |
|
if (isset($xmlRoot['change-tracking-policy'])) { |
170
|
|
|
$changeTrackingPolicy = strtoupper((string) $xmlRoot['change-tracking-policy']); |
171
|
|
|
|
172
|
|
|
$metadata->setChangeTrackingPolicy( |
173
|
|
|
constant(sprintf('%s::%s', Mapping\ChangeTrackingPolicy::class, $changeTrackingPolicy)) |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
// Evaluate <field ...> mappings |
178
|
34 |
|
if (isset($xmlRoot->field)) { |
179
|
20 |
|
$fieldBuilder = new Builder\FieldMetadataBuilder($metadataBuildingContext); |
180
|
|
|
|
181
|
|
|
$fieldBuilder |
182
|
20 |
|
->withComponentMetadata($metadata); |
183
|
|
|
|
184
|
20 |
|
foreach ($xmlRoot->field as $fieldElement) { |
185
|
20 |
|
$versionAnnotation = isset($fieldElement['version']) && $this->evaluateBoolean($fieldElement['version']) |
186
|
3 |
|
? new Annotation\Version() |
187
|
20 |
|
: null; |
188
|
|
|
|
189
|
|
|
$fieldBuilder |
190
|
20 |
|
->withFieldName((string) $fieldElement['name']) |
191
|
20 |
|
->withColumnAnnotation($this->convertFieldElementToColumnAnnotation($fieldElement)) |
192
|
20 |
|
->withIdAnnotation(null) |
193
|
20 |
|
->withVersionAnnotation($versionAnnotation); |
194
|
|
|
|
195
|
20 |
|
$fieldMetadata = $fieldBuilder->build(); |
196
|
|
|
|
197
|
|
|
// Prevent column duplication |
198
|
20 |
|
if ($metadata->checkPropertyDuplication($fieldMetadata->getColumnName())) { |
|
|
|
|
199
|
|
|
throw Mapping\MappingException::duplicateColumnName( |
200
|
|
|
$metadata->getClassName(), |
201
|
|
|
$fieldMetadata->getColumnName() |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
|
205
|
20 |
|
$metadata->addProperty($fieldMetadata); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
34 |
|
if (isset($xmlRoot->embedded)) { |
210
|
|
|
foreach ($xmlRoot->embedded as $embeddedMapping) { |
211
|
|
|
$columnPrefix = isset($embeddedMapping['column-prefix']) |
212
|
|
|
? (string) $embeddedMapping['column-prefix'] |
213
|
|
|
: null; |
214
|
|
|
|
215
|
|
|
$useColumnPrefix = isset($embeddedMapping['use-column-prefix']) |
216
|
|
|
? $this->evaluateBoolean($embeddedMapping['use-column-prefix']) |
217
|
|
|
: true; |
218
|
|
|
|
219
|
|
|
$mapping = [ |
220
|
|
|
'fieldName' => (string) $embeddedMapping['name'], |
221
|
|
|
'class' => (string) $embeddedMapping['class'], |
222
|
|
|
'columnPrefix' => $useColumnPrefix ? $columnPrefix : false, |
223
|
|
|
]; |
224
|
|
|
|
225
|
|
|
$metadata->mapEmbedded($mapping); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
// Evaluate <id ...> mappings |
230
|
34 |
|
$associationIds = []; |
231
|
|
|
|
232
|
34 |
|
$fieldBuilder = new Builder\FieldMetadataBuilder($metadataBuildingContext); |
233
|
|
|
|
234
|
|
|
$fieldBuilder |
235
|
34 |
|
->withComponentMetadata($metadata); |
236
|
|
|
|
237
|
34 |
|
foreach ($xmlRoot->id as $idElement) { |
238
|
29 |
|
$fieldName = (string) $idElement['name']; |
239
|
|
|
|
240
|
29 |
|
if (isset($idElement['association-key']) && $this->evaluateBoolean($idElement['association-key'])) { |
241
|
2 |
|
$associationIds[$fieldName] = true; |
242
|
|
|
|
243
|
2 |
|
continue; |
244
|
|
|
} |
245
|
|
|
|
246
|
28 |
|
$versionAnnotation = isset($idElement['version']) && $this->evaluateBoolean($idElement['version']) |
247
|
|
|
? new Annotation\Version() |
248
|
28 |
|
: null; |
249
|
|
|
|
250
|
|
|
$fieldMetadata = $fieldBuilder |
251
|
28 |
|
->withFieldName($fieldName) |
252
|
28 |
|
->withColumnAnnotation($this->convertFieldElementToColumnAnnotation($idElement)) |
253
|
28 |
|
->withIdAnnotation(new Annotation\Id()) |
254
|
28 |
|
->withVersionAnnotation($versionAnnotation) |
255
|
28 |
|
->withGeneratedValueAnnotation( |
256
|
28 |
|
isset($idElement->generator) |
257
|
27 |
|
? $this->convertGeneratorElementToGeneratedValueAnnotation($idElement->generator) |
258
|
28 |
|
: null |
259
|
|
|
) |
260
|
28 |
|
->withSequenceGeneratorAnnotation( |
261
|
28 |
|
isset($idElement->{'sequence-generator'}) |
262
|
3 |
|
? $this->convertSequenceGeneratorElementToSequenceGeneratorAnnotation($idElement->{'sequence-generator'}) |
263
|
28 |
|
: null |
264
|
|
|
) |
265
|
28 |
|
->withCustomIdGeneratorAnnotation( |
266
|
28 |
|
isset($idElement->{'custom-id-generator'}) |
267
|
2 |
|
? $this->convertCustomIdGeneratorElementToCustomIdGeneratorAnnotation($idElement->{'custom-id-generator'}) |
268
|
28 |
|
: null |
269
|
|
|
) |
270
|
28 |
|
->build(); |
271
|
|
|
|
272
|
|
|
// Prevent column duplication |
273
|
28 |
|
if ($metadata->checkPropertyDuplication($fieldMetadata->getColumnName())) { |
274
|
|
|
throw Mapping\MappingException::duplicateColumnName( |
275
|
|
|
$metadata->getClassName(), |
276
|
|
|
$fieldMetadata->getColumnName() |
277
|
|
|
); |
278
|
|
|
} |
279
|
|
|
|
280
|
28 |
|
$metadata->fieldNames[$fieldMetadata->getColumnName()] = $fieldMetadata->getName(); |
281
|
|
|
|
282
|
28 |
|
$metadata->addProperty($fieldMetadata); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
// Evaluate <one-to-one ...> mappings |
286
|
34 |
|
if (isset($xmlRoot->{'one-to-one'})) { |
287
|
7 |
|
$oneToOneAssociationBuilder = new Builder\OneToOneAssociationMetadataBuilder($metadataBuildingContext); |
288
|
|
|
|
289
|
|
|
$oneToOneAssociationBuilder |
290
|
7 |
|
->withComponentMetadata($metadata); |
291
|
|
|
|
292
|
7 |
|
foreach ($xmlRoot->{'one-to-one'} as $oneToOneElement) { |
293
|
7 |
|
$fieldName = (string) $oneToOneElement['field']; |
294
|
|
|
$associationMetadata = $oneToOneAssociationBuilder |
295
|
7 |
|
->withFieldName($fieldName) |
296
|
7 |
|
->withCacheAnnotation($this->convertCacheElementToCacheAnnotation($oneToOneElement->cache)) |
297
|
7 |
|
->withOneToOneAnnotation($this->convertOneToOneElementToOneToOneAnnotation($oneToOneElement)) |
298
|
7 |
|
->withIdAnnotation(isset($associationIds[$fieldName]) ? new Annotation\Id() : null) |
299
|
7 |
|
->withJoinColumnAnnotation( |
300
|
7 |
|
isset($oneToOneElement->{'join-column'}) |
301
|
5 |
|
? $this->convertJoinColumnElementToJoinColumnAnnotation($oneToOneElement->{'join-column'}) |
302
|
7 |
|
: null |
303
|
|
|
) |
304
|
7 |
|
->withJoinColumnsAnnotation( |
305
|
7 |
|
isset($oneToOneElement->{'join-columns'}) |
306
|
|
|
? $this->convertJoinColumnsElementToJoinColumnsAnnotation($oneToOneElement->{'join-columns'}) |
307
|
7 |
|
: null |
308
|
|
|
) |
309
|
7 |
|
->build(); |
310
|
|
|
|
311
|
|
|
// Prevent column duplication |
312
|
7 |
|
foreach ($associationMetadata->getJoinColumns() as $joinColumnMetadata) { |
313
|
5 |
|
$columnName = $joinColumnMetadata->getColumnName(); |
314
|
|
|
|
315
|
|
|
// @todo guilhermeblanco Open an issue to discuss making this scenario impossible. |
316
|
|
|
//if ($metadata->checkPropertyDuplication($columnName)) { |
317
|
|
|
// throw Mapping\MappingException::duplicateColumnName($metadata->getClassName(), $columnName); |
318
|
|
|
//} |
319
|
|
|
|
320
|
5 |
|
if ($associationMetadata->isOwningSide()) { |
321
|
5 |
|
$metadata->fieldNames[$columnName] = $associationMetadata->getName(); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
325
|
7 |
|
$metadata->addProperty($associationMetadata); |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
// Evaluate <many-to-one ...> mappings |
330
|
34 |
|
if (isset($xmlRoot->{'many-to-one'})) { |
331
|
8 |
|
$manyToOneAssociationBuilder = new Builder\ManyToOneAssociationMetadataBuilder($metadataBuildingContext); |
332
|
|
|
|
333
|
|
|
$manyToOneAssociationBuilder |
334
|
8 |
|
->withComponentMetadata($metadata); |
335
|
|
|
|
336
|
8 |
|
foreach ($xmlRoot->{'many-to-one'} as $manyToOneElement) { |
337
|
8 |
|
$fieldName = (string) $manyToOneElement['field']; |
338
|
|
|
$associationMetadata = $manyToOneAssociationBuilder |
339
|
8 |
|
->withFieldName($fieldName) |
340
|
8 |
|
->withCacheAnnotation($this->convertCacheElementToCacheAnnotation($manyToOneElement->cache)) |
341
|
8 |
|
->withManyToOneAnnotation($this->convertManyToOneElementToManyToOneAnnotation($manyToOneElement)) |
342
|
8 |
|
->withIdAnnotation(isset($associationIds[$fieldName]) ? new Annotation\Id() : null) |
343
|
8 |
|
->withJoinColumnAnnotation( |
344
|
8 |
|
isset($manyToOneElement->{'join-column'}) |
345
|
7 |
|
? $this->convertJoinColumnElementToJoinColumnAnnotation($manyToOneElement->{'join-column'}) |
346
|
8 |
|
: null |
347
|
|
|
) |
348
|
8 |
|
->withJoinColumnsAnnotation( |
349
|
8 |
|
isset($manyToOneElement->{'join-columns'}) |
350
|
1 |
|
? $this->convertJoinColumnsElementToJoinColumnsAnnotation($manyToOneElement->{'join-columns'}) |
351
|
8 |
|
: null |
352
|
|
|
) |
353
|
8 |
|
->build(); |
354
|
|
|
|
355
|
|
|
// Prevent column duplication |
356
|
8 |
|
foreach ($associationMetadata->getJoinColumns() as $joinColumnMetadata) { |
357
|
8 |
|
$columnName = $joinColumnMetadata->getColumnName(); |
358
|
|
|
|
359
|
|
|
// @todo guilhermeblanco Open an issue to discuss making this scenario impossible. |
360
|
|
|
//if ($metadata->checkPropertyDuplication($columnName)) { |
361
|
|
|
// throw Mapping\MappingException::duplicateColumnName($metadata->getClassName(), $columnName); |
362
|
|
|
//} |
363
|
|
|
|
364
|
8 |
|
if ($associationMetadata->isOwningSide()) { |
365
|
8 |
|
$metadata->fieldNames[$columnName] = $associationMetadata->getName(); |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
|
369
|
8 |
|
$metadata->addProperty($associationMetadata); |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
// Evaluate <one-to-many ...> mappings |
374
|
34 |
|
if (isset($xmlRoot->{'one-to-many'})) { |
375
|
9 |
|
$oneToManyAssociationBuilder = new Builder\OneToManyAssociationMetadataBuilder($metadataBuildingContext); |
376
|
|
|
|
377
|
|
|
$oneToManyAssociationBuilder |
378
|
9 |
|
->withComponentMetadata($metadata); |
379
|
|
|
|
380
|
9 |
|
foreach ($xmlRoot->{'one-to-many'} as $oneToManyElement) { |
381
|
9 |
|
$fieldName = (string) $oneToManyElement['field']; |
382
|
|
|
$associationMetadata = $oneToManyAssociationBuilder |
383
|
9 |
|
->withFieldName($fieldName) |
384
|
9 |
|
->withCacheAnnotation($this->convertCacheElementToCacheAnnotation($oneToManyElement->cache)) |
385
|
9 |
|
->withOneToManyAnnotation($this->convertOneToManyElementToOneToManyAnnotation($oneToManyElement)) |
386
|
9 |
|
->withOrderByAnnotation( |
387
|
9 |
|
isset($oneToManyElement->{'order-by'}) |
388
|
5 |
|
? $this->convertOrderByElementToOrderByAnnotation($oneToManyElement->{'order-by'}) |
389
|
9 |
|
: null |
390
|
|
|
) |
391
|
9 |
|
->withIdAnnotation(isset($associationIds[$fieldName]) ? new Annotation\Id() : null) |
392
|
9 |
|
->build(); |
393
|
|
|
|
394
|
9 |
|
$metadata->addProperty($associationMetadata); |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
// Evaluate <many-to-many ...> mappings |
399
|
34 |
|
if (isset($xmlRoot->{'many-to-many'})) { |
400
|
14 |
|
$manyToManyAssociationBuilder = new Builder\ManyToManyAssociationMetadataBuilder($metadataBuildingContext); |
401
|
|
|
|
402
|
|
|
$manyToManyAssociationBuilder |
403
|
14 |
|
->withComponentMetadata($metadata); |
404
|
|
|
|
405
|
14 |
|
foreach ($xmlRoot->{'many-to-many'} as $manyToManyElement) { |
406
|
14 |
|
$fieldName = (string) $manyToManyElement['field']; |
407
|
|
|
$associationMetadata = $manyToManyAssociationBuilder |
408
|
14 |
|
->withFieldName($fieldName) |
409
|
14 |
|
->withCacheAnnotation($this->convertCacheElementToCacheAnnotation($manyToManyElement->cache)) |
410
|
14 |
|
->withManyToManyAnnotation($this->convertManyToManyElementToManyToManyAnnotation($manyToManyElement)) |
411
|
14 |
|
->withJoinTableAnnotation( |
412
|
14 |
|
isset($manyToManyElement->{'join-table'}) |
413
|
8 |
|
? $this->convertJoinTableElementToJoinTableAnnotation($manyToManyElement->{'join-table'}) |
414
|
14 |
|
: null |
415
|
|
|
) |
416
|
14 |
|
->withOrderByAnnotation( |
417
|
14 |
|
isset($manyToManyElement->{'order-by'}) |
418
|
1 |
|
? $this->convertOrderByElementToOrderByAnnotation($manyToManyElement->{'order-by'}) |
419
|
14 |
|
: null |
420
|
|
|
) |
421
|
14 |
|
->withIdAnnotation(isset($associationIds[$fieldName]) ? new Annotation\Id() : null) |
422
|
14 |
|
->build(); |
423
|
|
|
|
424
|
14 |
|
$metadata->addProperty($associationMetadata); |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
// Evaluate association-overrides |
429
|
34 |
|
if (isset($xmlRoot->{'attribute-overrides'})) { |
430
|
2 |
|
$fieldBuilder = new Builder\FieldMetadataBuilder($metadataBuildingContext); |
431
|
|
|
|
432
|
|
|
$fieldBuilder |
433
|
2 |
|
->withComponentMetadata($metadata); |
434
|
|
|
|
435
|
2 |
|
foreach ($xmlRoot->{'attribute-overrides'}->{'attribute-override'} as $overrideElement) { |
436
|
2 |
|
$fieldName = (string) $overrideElement['name']; |
437
|
2 |
|
$property = $metadata->getProperty($fieldName); |
438
|
|
|
|
439
|
2 |
|
if (! $property) { |
440
|
|
|
throw Mapping\MappingException::invalidOverrideFieldName($metadata->getClassName(), $fieldName); |
441
|
|
|
} |
442
|
|
|
|
443
|
2 |
|
foreach ($overrideElement->field as $fieldElement) { |
444
|
2 |
|
$versionAnnotation = isset($fieldElement['version']) && $this->evaluateBoolean($fieldElement['version']) |
445
|
|
|
? new Annotation\Version() |
446
|
2 |
|
: null; |
447
|
|
|
|
448
|
|
|
$fieldBuilder |
449
|
2 |
|
->withFieldName($fieldName) |
450
|
2 |
|
->withColumnAnnotation($this->convertFieldElementToColumnAnnotation($fieldElement)) |
451
|
2 |
|
->withIdAnnotation(null) |
452
|
2 |
|
->withVersionAnnotation($versionAnnotation); |
453
|
|
|
|
454
|
2 |
|
$fieldMetadata = $fieldBuilder->build(); |
455
|
2 |
|
$columnName = $fieldMetadata->getColumnName(); |
456
|
|
|
|
457
|
|
|
// Prevent column duplication |
458
|
2 |
|
if ($metadata->checkPropertyDuplication($columnName)) { |
459
|
|
|
throw Mapping\MappingException::duplicateColumnName($metadata->getClassName(), $columnName); |
460
|
|
|
} |
461
|
|
|
|
462
|
2 |
|
$metadata->fieldNames[$fieldMetadata->getColumnName()] = $fieldName; |
463
|
|
|
|
464
|
2 |
|
$metadata->setPropertyOverride($fieldMetadata); |
465
|
|
|
} |
466
|
|
|
} |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
// Evaluate association-overrides |
470
|
34 |
|
if (isset($xmlRoot->{'association-overrides'})) { |
471
|
4 |
|
foreach ($xmlRoot->{'association-overrides'}->{'association-override'} as $overrideElement) { |
472
|
4 |
|
$fieldName = (string) $overrideElement['name']; |
473
|
4 |
|
$property = $metadata->getProperty($fieldName); |
474
|
|
|
|
475
|
4 |
|
if (! $property) { |
476
|
|
|
throw Mapping\MappingException::invalidOverrideFieldName($metadata->getClassName(), $fieldName); |
477
|
|
|
} |
478
|
|
|
|
479
|
4 |
|
$override = clone $property; |
480
|
|
|
|
481
|
|
|
// Check for join-columns |
482
|
4 |
|
if (isset($overrideElement->{'join-columns'})) { |
483
|
2 |
|
$joinColumnBuilder = new Builder\JoinColumnMetadataBuilder($metadataBuildingContext); |
484
|
|
|
|
485
|
|
|
$joinColumnBuilder |
486
|
2 |
|
->withComponentMetadata($metadata) |
487
|
2 |
|
->withFieldName($override->getName()); |
488
|
|
|
|
489
|
2 |
|
$joinColumns = []; |
490
|
|
|
|
491
|
2 |
|
foreach ($overrideElement->{'join-columns'}->{'join-column'} as $joinColumnElement) { |
492
|
2 |
|
$joinColumnBuilder->withJoinColumnAnnotation( |
493
|
2 |
|
$this->convertJoinColumnElementToJoinColumnAnnotation($joinColumnElement) |
494
|
|
|
); |
495
|
|
|
|
496
|
2 |
|
$joinColumnMetadata = $joinColumnBuilder->build(); |
497
|
2 |
|
$columnName = $joinColumnMetadata->getColumnName(); |
498
|
|
|
|
499
|
|
|
// @todo guilhermeblanco Open an issue to discuss making this scenario impossible. |
500
|
|
|
//if ($metadata->checkPropertyDuplication($columnName)) { |
501
|
|
|
// throw Mapping\MappingException::duplicateColumnName($metadata->getClassName(), $columnName); |
502
|
|
|
//} |
503
|
|
|
|
504
|
2 |
|
if ($override->isOwningSide()) { |
|
|
|
|
505
|
2 |
|
$metadata->fieldNames[$columnName] = $fieldName; |
506
|
|
|
} |
507
|
|
|
|
508
|
2 |
|
$joinColumns[] = $joinColumnMetadata; |
509
|
|
|
} |
510
|
|
|
|
511
|
2 |
|
$override->setJoinColumns($joinColumns); |
|
|
|
|
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
// Check for join-table |
515
|
4 |
|
if ($overrideElement->{'join-table'}) { |
516
|
2 |
|
$joinTableElement = $overrideElement->{'join-table'}; |
517
|
2 |
|
$joinTableAnnotation = $this->convertJoinTableElementToJoinTableAnnotation($joinTableElement); |
518
|
2 |
|
$joinTableBuilder = new Builder\JoinTableMetadataBuilder($metadataBuildingContext); |
519
|
|
|
|
520
|
|
|
$joinTableBuilder |
521
|
2 |
|
->withComponentMetadata($metadata) |
522
|
2 |
|
->withFieldName($property->getName()) |
523
|
2 |
|
->withTargetEntity($property->getTargetEntity()) |
|
|
|
|
524
|
2 |
|
->withJoinTableAnnotation($joinTableAnnotation); |
525
|
|
|
|
526
|
2 |
|
$override->setJoinTable($joinTableBuilder->build()); |
|
|
|
|
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
// Check for inversed-by |
530
|
4 |
|
if (isset($overrideElement->{'inversed-by'})) { |
531
|
1 |
|
$override->setInversedBy((string) $overrideElement->{'inversed-by'}['name']); |
|
|
|
|
532
|
|
|
} |
533
|
|
|
|
534
|
|
|
// Check for fetch |
535
|
4 |
|
if (isset($overrideElement['fetch'])) { |
536
|
1 |
|
$override->setFetchMode( |
|
|
|
|
537
|
1 |
|
constant('Doctrine\ORM\Mapping\FetchMode::' . (string) $overrideElement['fetch']) |
538
|
|
|
); |
539
|
|
|
} |
540
|
|
|
|
541
|
4 |
|
$metadata->setPropertyOverride($override); |
542
|
|
|
} |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
// Evaluate <lifecycle-callbacks...> |
546
|
34 |
|
if (isset($xmlRoot->{'lifecycle-callbacks'})) { |
547
|
3 |
|
foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) { |
548
|
3 |
|
$eventName = constant(Events::class . '::' . (string) $lifecycleCallback['type']); |
549
|
3 |
|
$methodName = (string) $lifecycleCallback['method']; |
550
|
|
|
|
551
|
3 |
|
$metadata->addLifecycleCallback($eventName, $methodName); |
552
|
|
|
} |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
// Evaluate entity listener |
556
|
34 |
|
if (isset($xmlRoot->{'entity-listeners'})) { |
557
|
2 |
|
foreach ($xmlRoot->{'entity-listeners'}->{'entity-listener'} as $listenerElement) { |
558
|
2 |
|
$listenerClassName = (string) $listenerElement['class']; |
559
|
|
|
|
560
|
2 |
|
if (! class_exists($listenerClassName)) { |
561
|
|
|
throw Mapping\MappingException::entityListenerClassNotFound( |
562
|
|
|
$listenerClassName, |
563
|
|
|
$metadata->getClassName() |
564
|
|
|
); |
565
|
|
|
} |
566
|
|
|
|
567
|
2 |
|
foreach ($listenerElement as $callbackElement) { |
568
|
2 |
|
$eventName = (string) $callbackElement['type']; |
569
|
2 |
|
$methodName = (string) $callbackElement['method']; |
570
|
|
|
|
571
|
2 |
|
$metadata->addEntityListener($eventName, $listenerClassName, $methodName); |
572
|
|
|
} |
573
|
|
|
} |
574
|
|
|
} |
575
|
|
|
|
576
|
34 |
|
return $metadata; |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
/** |
580
|
|
|
* Parses (nested) index elements. |
581
|
|
|
* |
582
|
|
|
* @param SimpleXMLElement $indexes The XML element. |
583
|
|
|
* |
584
|
|
|
* @return Annotation\Index[] The indexes array. |
585
|
|
|
*/ |
586
|
4 |
|
private function parseIndexes(SimpleXMLElement $indexes) : array |
587
|
|
|
{ |
588
|
4 |
|
$array = []; |
589
|
|
|
|
590
|
|
|
/** @var SimpleXMLElement $index */ |
591
|
4 |
|
foreach ($indexes as $index) { |
592
|
4 |
|
$indexAnnotation = new Annotation\Index(); |
593
|
|
|
|
594
|
4 |
|
$indexAnnotation->columns = explode(',', (string) $index['columns']); |
595
|
4 |
|
$indexAnnotation->options = isset($index->options) ? $this->parseOptions($index->options->children()) : []; |
596
|
4 |
|
$indexAnnotation->flags = isset($index['flags']) ? explode(',', (string) $index['flags']) : []; |
597
|
|
|
|
598
|
4 |
|
if (isset($index['name'])) { |
599
|
3 |
|
$indexAnnotation->name = (string) $index['name']; |
600
|
|
|
} |
601
|
|
|
|
602
|
4 |
|
if (isset($index['unique'])) { |
603
|
|
|
$indexAnnotation->unique = $this->evaluateBoolean($index['unique']); |
604
|
|
|
} |
605
|
|
|
|
606
|
4 |
|
$array[] = $indexAnnotation; |
607
|
|
|
} |
608
|
|
|
|
609
|
4 |
|
return $array; |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
/** |
613
|
|
|
* Parses (nested) unique constraint elements. |
614
|
|
|
* |
615
|
|
|
* @param SimpleXMLElement $uniqueConstraints The XML element. |
616
|
|
|
* |
617
|
|
|
* @return Annotation\UniqueConstraint[] The unique constraints array. |
618
|
|
|
*/ |
619
|
3 |
|
private function parseUniqueConstraints(SimpleXMLElement $uniqueConstraints) : array |
620
|
|
|
{ |
621
|
3 |
|
$array = []; |
622
|
|
|
|
623
|
|
|
/** @var SimpleXMLElement $uniqueConstraint */ |
624
|
3 |
|
foreach ($uniqueConstraints as $uniqueConstraint) { |
625
|
3 |
|
$uniqueConstraintAnnotation = new Annotation\UniqueConstraint(); |
626
|
|
|
|
627
|
3 |
|
$uniqueConstraintAnnotation->columns = explode(',', (string) $uniqueConstraint['columns']); |
628
|
3 |
|
$uniqueConstraintAnnotation->options = isset($uniqueConstraint->options) ? $this->parseOptions($uniqueConstraint->options->children()) : []; |
629
|
3 |
|
$uniqueConstraintAnnotation->flags = isset($uniqueConstraint['flags']) ? explode(',', (string) $uniqueConstraint['flags']) : []; |
630
|
|
|
|
631
|
3 |
|
if (isset($uniqueConstraint['name'])) { |
632
|
3 |
|
$uniqueConstraintAnnotation->name = (string) $uniqueConstraint['name']; |
633
|
|
|
} |
634
|
|
|
|
635
|
3 |
|
$array[] = $uniqueConstraintAnnotation; |
636
|
|
|
} |
637
|
|
|
|
638
|
3 |
|
return $array; |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
/** |
642
|
|
|
* Parses (nested) option elements. |
643
|
|
|
* |
644
|
|
|
* @param SimpleXMLElement $options The XML element. |
645
|
|
|
* |
646
|
|
|
* @return mixed[] The options array. |
647
|
|
|
*/ |
648
|
4 |
|
private function parseOptions(SimpleXMLElement $options) : array |
649
|
|
|
{ |
650
|
4 |
|
$array = []; |
651
|
|
|
|
652
|
|
|
/** @var SimpleXMLElement $option */ |
653
|
4 |
|
foreach ($options as $option) { |
654
|
4 |
|
if ($option->count()) { |
655
|
3 |
|
$value = $this->parseOptions($option->children()); |
656
|
|
|
} else { |
657
|
4 |
|
$value = (string) $option; |
658
|
|
|
} |
659
|
|
|
|
660
|
4 |
|
$attributes = $option->attributes(); |
661
|
|
|
|
662
|
4 |
|
if (isset($attributes->name)) { |
663
|
4 |
|
$nameAttribute = (string) $attributes->name; |
664
|
|
|
|
665
|
4 |
|
$array[$nameAttribute] = in_array($nameAttribute, ['unsigned', 'fixed'], true) |
666
|
3 |
|
? $this->evaluateBoolean($value) |
667
|
4 |
|
: $value; |
668
|
|
|
} else { |
669
|
|
|
$array[] = $value; |
670
|
|
|
} |
671
|
|
|
} |
672
|
|
|
|
673
|
4 |
|
return $array; |
674
|
|
|
} |
675
|
|
|
|
676
|
7 |
|
private function convertOneToOneElementToOneToOneAnnotation( |
677
|
|
|
SimpleXMLElement $oneToOneElement |
678
|
|
|
) : Annotation\OneToOne { |
679
|
7 |
|
$oneToOneAnnotation = new Annotation\OneToOne(); |
680
|
|
|
|
681
|
7 |
|
$oneToOneAnnotation->targetEntity = (string) $oneToOneElement['target-entity']; |
682
|
|
|
|
683
|
7 |
|
if (isset($oneToOneElement['mapped-by'])) { |
684
|
3 |
|
$oneToOneAnnotation->mappedBy = (string) $oneToOneElement['mapped-by']; |
685
|
|
|
} |
686
|
|
|
|
687
|
7 |
|
if (isset($oneToOneElement['inversed-by'])) { |
688
|
4 |
|
$oneToOneAnnotation->inversedBy = (string) $oneToOneElement['inversed-by']; |
689
|
|
|
} |
690
|
|
|
|
691
|
7 |
|
if (isset($oneToOneElement['orphan-removal'])) { |
692
|
|
|
$oneToOneAnnotation->orphanRemoval = $this->evaluateBoolean($oneToOneElement['orphan-removal']); |
693
|
|
|
} |
694
|
|
|
|
695
|
7 |
|
if (isset($oneToOneElement['fetch'])) { |
696
|
3 |
|
$oneToOneAnnotation->fetch = (string) $oneToOneElement['fetch']; |
697
|
|
|
} |
698
|
|
|
|
699
|
7 |
|
if (isset($oneToOneElement->cascade)) { |
700
|
7 |
|
$oneToOneAnnotation->cascade = $this->getCascadeMappings($oneToOneElement->cascade); |
701
|
|
|
} |
702
|
|
|
|
703
|
7 |
|
return $oneToOneAnnotation; |
704
|
|
|
} |
705
|
|
|
|
706
|
8 |
|
private function convertManyToOneElementToManyToOneAnnotation( |
707
|
|
|
SimpleXMLElement $manyToOneElement |
708
|
|
|
) : Annotation\ManyToOne { |
709
|
8 |
|
$manyToOneAnnotation = new Annotation\ManyToOne(); |
710
|
|
|
|
711
|
8 |
|
$manyToOneAnnotation->targetEntity = (string) $manyToOneElement['target-entity']; |
712
|
|
|
|
713
|
8 |
|
if (isset($manyToOneElement['inversed-by'])) { |
714
|
2 |
|
$manyToOneAnnotation->inversedBy = (string) $manyToOneElement['inversed-by']; |
715
|
|
|
} |
716
|
|
|
|
717
|
8 |
|
if (isset($manyToOneElement['fetch'])) { |
718
|
|
|
$manyToOneAnnotation->fetch = (string) $manyToOneElement['fetch']; |
719
|
|
|
} |
720
|
|
|
|
721
|
8 |
|
if (isset($manyToOneElement->cascade)) { |
722
|
4 |
|
$manyToOneAnnotation->cascade = $this->getCascadeMappings($manyToOneElement->cascade); |
723
|
|
|
} |
724
|
|
|
|
725
|
8 |
|
return $manyToOneAnnotation; |
726
|
|
|
} |
727
|
|
|
|
728
|
9 |
|
private function convertOneToManyElementToOneToManyAnnotation( |
729
|
|
|
SimpleXMLElement $oneToManyElement |
730
|
|
|
) : Annotation\OneToMany { |
731
|
9 |
|
$oneToManyAnnotation = new Annotation\OneToMany(); |
732
|
|
|
|
733
|
9 |
|
$oneToManyAnnotation->targetEntity = (string) $oneToManyElement['target-entity']; |
734
|
|
|
|
735
|
9 |
|
if (isset($oneToManyElement['mapped-by'])) { |
736
|
9 |
|
$oneToManyAnnotation->mappedBy = (string) $oneToManyElement['mapped-by']; |
737
|
|
|
} |
738
|
|
|
|
739
|
9 |
|
if (isset($manyToOneElement['fetch'])) { |
|
|
|
|
740
|
|
|
$oneToManyAnnotation->fetch = (string) $oneToManyElement['fetch']; |
741
|
|
|
} |
742
|
|
|
|
743
|
9 |
|
if (isset($oneToManyElement->cascade)) { |
744
|
6 |
|
$oneToManyAnnotation->cascade = $this->getCascadeMappings($oneToManyElement->cascade); |
745
|
|
|
} |
746
|
|
|
|
747
|
9 |
|
if (isset($oneToManyElement['orphan-removal'])) { |
748
|
3 |
|
$oneToManyAnnotation->orphanRemoval = $this->evaluateBoolean($oneToManyElement['orphan-removal']); |
749
|
|
|
} |
750
|
|
|
|
751
|
9 |
|
if (isset($oneToManyElement['index-by'])) { |
752
|
3 |
|
$oneToManyAnnotation->indexBy = (string) $oneToManyElement['index-by']; |
753
|
6 |
|
} elseif (isset($oneToManyElement->{'index-by'})) { |
754
|
|
|
throw new InvalidArgumentException('<index-by /> is not a valid tag'); |
755
|
|
|
} |
756
|
|
|
|
757
|
9 |
|
return $oneToManyAnnotation; |
758
|
|
|
} |
759
|
|
|
|
760
|
14 |
|
private function convertManyToManyElementToManyToManyAnnotation( |
761
|
|
|
SimpleXMLElement $manyToManyElement |
762
|
|
|
) : Annotation\ManyToMany { |
763
|
14 |
|
$manyToManyAnnotation = new Annotation\ManyToMany(); |
764
|
|
|
|
765
|
14 |
|
$manyToManyAnnotation->targetEntity = (string) $manyToManyElement['target-entity']; |
766
|
|
|
|
767
|
14 |
|
if (isset($manyToManyElement['mapped-by'])) { |
768
|
5 |
|
$manyToManyAnnotation->mappedBy = (string) $manyToManyElement['mapped-by']; |
769
|
|
|
} |
770
|
|
|
|
771
|
14 |
|
if (isset($manyToManyElement['inversed-by'])) { |
772
|
6 |
|
$manyToManyAnnotation->inversedBy = (string) $manyToManyElement['inversed-by']; |
773
|
|
|
} |
774
|
|
|
|
775
|
14 |
|
if (isset($manyToManyElement['fetch'])) { |
776
|
4 |
|
$manyToManyAnnotation->fetch = (string) $manyToManyElement['fetch']; |
777
|
|
|
} |
778
|
|
|
|
779
|
14 |
|
if (isset($manyToManyElement->cascade)) { |
780
|
8 |
|
$manyToManyAnnotation->cascade = $this->getCascadeMappings($manyToManyElement->cascade); |
781
|
|
|
} |
782
|
|
|
|
783
|
14 |
|
if (isset($manyToManyElement['orphan-removal'])) { |
784
|
|
|
$manyToManyAnnotation->orphanRemoval = $this->evaluateBoolean($manyToManyElement['orphan-removal']); |
785
|
|
|
} |
786
|
|
|
|
787
|
14 |
|
if (isset($manyToManyElement['index-by'])) { |
788
|
|
|
$manyToManyAnnotation->indexBy = (string) $manyToManyElement['index-by']; |
789
|
14 |
|
} elseif (isset($manyToManyElement->{'index-by'})) { |
790
|
|
|
throw new InvalidArgumentException('<index-by /> is not a valid tag'); |
791
|
|
|
} |
792
|
|
|
|
793
|
14 |
|
return $manyToManyAnnotation; |
794
|
|
|
} |
795
|
|
|
|
796
|
30 |
|
private function convertFieldElementToColumnAnnotation( |
797
|
|
|
SimpleXMLElement $fieldElement |
798
|
|
|
) : Annotation\Column { |
799
|
30 |
|
$columnAnnotation = new Annotation\Column(); |
800
|
|
|
|
801
|
30 |
|
$columnAnnotation->type = isset($fieldElement['type']) ? (string) $fieldElement['type'] : 'string'; |
802
|
|
|
|
803
|
30 |
|
if (isset($fieldElement['column'])) { |
804
|
20 |
|
$columnAnnotation->name = (string) $fieldElement['column']; |
805
|
|
|
} |
806
|
|
|
|
807
|
30 |
|
if (isset($fieldElement['length'])) { |
808
|
6 |
|
$columnAnnotation->length = (int) $fieldElement['length']; |
809
|
|
|
} |
810
|
|
|
|
811
|
30 |
|
if (isset($fieldElement['precision'])) { |
812
|
1 |
|
$columnAnnotation->precision = (int) $fieldElement['precision']; |
813
|
|
|
} |
814
|
|
|
|
815
|
30 |
|
if (isset($fieldElement['scale'])) { |
816
|
1 |
|
$columnAnnotation->scale = (int) $fieldElement['scale']; |
817
|
|
|
} |
818
|
|
|
|
819
|
30 |
|
if (isset($fieldElement['unique'])) { |
820
|
7 |
|
$columnAnnotation->unique = $this->evaluateBoolean($fieldElement['unique']); |
821
|
|
|
} |
822
|
|
|
|
823
|
30 |
|
if (isset($fieldElement['nullable'])) { |
824
|
7 |
|
$columnAnnotation->nullable = $this->evaluateBoolean($fieldElement['nullable']); |
825
|
|
|
} |
826
|
|
|
|
827
|
30 |
|
if (isset($fieldElement['column-definition'])) { |
828
|
4 |
|
$columnAnnotation->columnDefinition = (string) $fieldElement['column-definition']; |
829
|
|
|
} |
830
|
|
|
|
831
|
30 |
|
if (isset($fieldElement->options)) { |
832
|
3 |
|
$columnAnnotation->options = $this->parseOptions($fieldElement->options->children()); |
833
|
|
|
} |
834
|
|
|
|
835
|
30 |
|
return $columnAnnotation; |
836
|
|
|
} |
837
|
|
|
|
838
|
27 |
|
private function convertGeneratorElementToGeneratedValueAnnotation( |
839
|
|
|
SimpleXMLElement $generatorElement |
840
|
|
|
) : Annotation\GeneratedValue { |
841
|
27 |
|
$generatedValueAnnotation = new Annotation\GeneratedValue(); |
842
|
|
|
|
843
|
27 |
|
$generatedValueAnnotation->strategy = (string) ($generatorElement['strategy'] ?? 'AUTO'); |
844
|
|
|
|
845
|
27 |
|
return $generatedValueAnnotation; |
846
|
|
|
} |
847
|
|
|
|
848
|
3 |
|
private function convertSequenceGeneratorElementToSequenceGeneratorAnnotation( |
849
|
|
|
SimpleXMLElement $sequenceGeneratorElement |
850
|
|
|
) : Annotation\SequenceGenerator { |
851
|
3 |
|
$sequenceGeneratorAnnotation = new Annotation\SequenceGenerator(); |
852
|
|
|
|
853
|
3 |
|
$sequenceGeneratorAnnotation->sequenceName = (string) ($sequenceGeneratorElement['sequence-name'] ?? null); |
854
|
3 |
|
$sequenceGeneratorAnnotation->allocationSize = (int) ($sequenceGeneratorElement['allocation-size'] ?? 1); |
855
|
|
|
|
856
|
3 |
|
return $sequenceGeneratorAnnotation; |
857
|
|
|
} |
858
|
|
|
|
859
|
2 |
|
private function convertCustomIdGeneratorElementToCustomIdGeneratorAnnotation( |
860
|
|
|
SimpleXMLElement $customIdGeneratorElement |
861
|
|
|
) : Annotation\CustomIdGenerator { |
862
|
2 |
|
$customIdGeneratorAnnotation = new Annotation\CustomIdGenerator(); |
863
|
|
|
|
864
|
2 |
|
$customIdGeneratorAnnotation->class = (string) $customIdGeneratorElement['class']; |
865
|
2 |
|
$customIdGeneratorAnnotation->arguments = []; |
866
|
|
|
|
867
|
2 |
|
return $customIdGeneratorAnnotation; |
868
|
|
|
} |
869
|
|
|
|
870
|
|
|
/** |
871
|
|
|
* Constructs a JoinTable annotation based on the information |
872
|
|
|
* found in the given SimpleXMLElement. |
873
|
|
|
* |
874
|
|
|
* @param SimpleXMLElement $joinTableElement The XML element. |
875
|
|
|
*/ |
876
|
8 |
|
private function convertJoinTableElementToJoinTableAnnotation( |
877
|
|
|
SimpleXMLElement $joinTableElement |
878
|
|
|
) : Annotation\JoinTable { |
879
|
8 |
|
$joinTableAnnotation = new Annotation\JoinTable(); |
880
|
|
|
|
881
|
8 |
|
if (isset($joinTableElement['name'])) { |
882
|
8 |
|
$joinTableAnnotation->name = (string) $joinTableElement['name']; |
883
|
|
|
} |
884
|
|
|
|
885
|
8 |
|
if (isset($joinTableElement['schema'])) { |
886
|
|
|
$joinTableAnnotation->schema = (string) $joinTableElement['schema']; |
887
|
|
|
} |
888
|
|
|
|
889
|
8 |
|
if (isset($joinTableElement->{'join-columns'})) { |
890
|
8 |
|
$joinColumns = []; |
891
|
|
|
|
892
|
8 |
|
foreach ($joinTableElement->{'join-columns'}->{'join-column'} as $joinColumnElement) { |
893
|
8 |
|
$joinColumns[] = $this->convertJoinColumnElementToJoinColumnAnnotation($joinColumnElement); |
894
|
|
|
} |
895
|
|
|
|
896
|
8 |
|
$joinTableAnnotation->joinColumns = $joinColumns; |
897
|
|
|
} |
898
|
|
|
|
899
|
8 |
|
if (isset($joinTableElement->{'inverse-join-columns'})) { |
900
|
8 |
|
$joinColumns = []; |
901
|
|
|
|
902
|
8 |
|
foreach ($joinTableElement->{'inverse-join-columns'}->{'join-column'} as $joinColumnElement) { |
903
|
8 |
|
$joinColumns[] = $this->convertJoinColumnElementToJoinColumnAnnotation($joinColumnElement); |
904
|
|
|
} |
905
|
|
|
|
906
|
8 |
|
$joinTableAnnotation->inverseJoinColumns = $joinColumns; |
907
|
|
|
} |
908
|
|
|
|
909
|
8 |
|
return $joinTableAnnotation; |
910
|
|
|
} |
911
|
|
|
|
912
|
1 |
|
private function convertJoinColumnsElementToJoinColumnsAnnotation( |
913
|
|
|
SimpleXMLElement $joinColumnsElement |
914
|
|
|
) : Annotation\JoinColumns { |
915
|
1 |
|
$joinColumnsAnnotation = new Annotation\JoinColumns(); |
916
|
1 |
|
$joinColumns = []; |
917
|
|
|
|
918
|
1 |
|
foreach ($joinColumnsElement->{'join-column'} as $joinColumnElement) { |
919
|
1 |
|
$joinColumns[] = $this->convertJoinColumnElementToJoinColumnAnnotation($joinColumnElement); |
920
|
|
|
} |
921
|
|
|
|
922
|
1 |
|
$joinColumnsAnnotation->value = $joinColumns; |
923
|
|
|
|
924
|
1 |
|
return $joinColumnsAnnotation; |
925
|
|
|
} |
926
|
|
|
|
927
|
|
|
/** |
928
|
|
|
* Constructs a JoinColumn annotation based on the information |
929
|
|
|
* found in the given SimpleXMLElement. |
930
|
|
|
* |
931
|
|
|
* @param SimpleXMLElement $joinColumnElement The XML element. |
932
|
|
|
*/ |
933
|
13 |
|
private function convertJoinColumnElementToJoinColumnAnnotation( |
934
|
|
|
SimpleXMLElement $joinColumnElement |
935
|
|
|
) : Annotation\JoinColumn { |
936
|
13 |
|
$joinColumnAnnotation = new Annotation\JoinColumn(); |
937
|
|
|
|
938
|
13 |
|
$joinColumnAnnotation->name = (string) $joinColumnElement['name']; |
939
|
13 |
|
$joinColumnAnnotation->referencedColumnName = (string) $joinColumnElement['referenced-column-name']; |
940
|
|
|
|
941
|
13 |
|
if (isset($joinColumnElement['column-definition'])) { |
942
|
3 |
|
$joinColumnAnnotation->columnDefinition = (string) $joinColumnElement['column-definition']; |
943
|
|
|
} |
944
|
|
|
|
945
|
13 |
|
if (isset($joinColumnElement['field-name'])) { |
946
|
|
|
$joinColumnAnnotation->fieldName = (string) $joinColumnElement['field-name']; |
947
|
|
|
} |
948
|
|
|
|
949
|
13 |
|
if (isset($joinColumnElement['nullable'])) { |
950
|
4 |
|
$joinColumnAnnotation->nullable = $this->evaluateBoolean($joinColumnElement['nullable']); |
951
|
|
|
} |
952
|
|
|
|
953
|
13 |
|
if (isset($joinColumnElement['unique'])) { |
954
|
3 |
|
$joinColumnAnnotation->unique = $this->evaluateBoolean($joinColumnElement['unique']); |
955
|
|
|
} |
956
|
|
|
|
957
|
13 |
|
if (isset($joinColumnElement['on-delete'])) { |
958
|
3 |
|
$joinColumnAnnotation->onDelete = strtoupper((string) $joinColumnElement['on-delete']); |
959
|
|
|
} |
960
|
|
|
|
961
|
13 |
|
return $joinColumnAnnotation; |
962
|
|
|
} |
963
|
|
|
|
964
|
|
|
/** |
965
|
|
|
* Parse the given Cache as CacheMetadata |
966
|
|
|
*/ |
967
|
17 |
|
private function convertCacheElementToCacheAnnotation(SimpleXMLElement $cacheMapping) : Annotation\Cache |
968
|
|
|
{ |
969
|
17 |
|
$cacheAnnotation = new Annotation\Cache(); |
970
|
|
|
|
971
|
17 |
|
if (isset($cacheMapping['region'])) { |
972
|
|
|
$cacheAnnotation->region = (string) $cacheMapping['region']; |
973
|
|
|
} |
974
|
|
|
|
975
|
17 |
|
if (isset($cacheMapping['usage'])) { |
976
|
2 |
|
$cacheAnnotation->usage = strtoupper((string) $cacheMapping['usage']); |
977
|
|
|
} |
978
|
|
|
|
979
|
17 |
|
return $cacheAnnotation; |
980
|
|
|
} |
981
|
|
|
|
982
|
8 |
|
private function convertDiscrimininatorColumnElementToDiscriminatorColumnAnnotation( |
983
|
|
|
SimpleXMLElement $discriminatorColumnElement |
984
|
|
|
) : Annotation\DiscriminatorColumn { |
985
|
8 |
|
$discriminatorColumnAnnotation = new Annotation\DiscriminatorColumn(); |
986
|
|
|
|
987
|
8 |
|
$discriminatorColumnAnnotation->type = (string) ($discriminatorColumnElement['type'] ?? 'string'); |
988
|
8 |
|
$discriminatorColumnAnnotation->name = (string) $discriminatorColumnElement['name']; |
989
|
|
|
|
990
|
8 |
|
if (isset($discriminatorColumnElement['column-definition'])) { |
991
|
1 |
|
$discriminatorColumnAnnotation->columnDefinition = (string) $discriminatorColumnElement['column-definition']; |
992
|
|
|
} |
993
|
|
|
|
994
|
8 |
|
if (isset($discriminatorColumnElement['length'])) { |
995
|
3 |
|
$discriminatorColumnAnnotation->length = (int) $discriminatorColumnElement['length']; |
996
|
|
|
} |
997
|
|
|
|
998
|
8 |
|
return $discriminatorColumnAnnotation; |
999
|
|
|
} |
1000
|
|
|
|
1001
|
6 |
|
private function convertOrderByElementToOrderByAnnotation( |
1002
|
|
|
SimpleXMLElement $orderByElement |
1003
|
|
|
) : Annotation\OrderBy { |
1004
|
6 |
|
$orderByAnnotation = new Annotation\OrderBy(); |
1005
|
6 |
|
$orderBy = []; |
1006
|
|
|
|
1007
|
6 |
|
foreach ($orderByElement->{'order-by-field'} as $orderByField) { |
1008
|
6 |
|
$orderBy[(string) $orderByField['name']] = isset($orderByField['direction']) |
1009
|
4 |
|
? (string) $orderByField['direction'] |
1010
|
2 |
|
: Criteria::ASC; |
1011
|
|
|
} |
1012
|
|
|
|
1013
|
6 |
|
$orderByAnnotation->value = $orderBy; |
1014
|
|
|
|
1015
|
6 |
|
return $orderByAnnotation; |
1016
|
|
|
} |
1017
|
|
|
|
1018
|
|
|
/** |
1019
|
|
|
* Gathers a list of cascade options found in the given cascade element. |
1020
|
|
|
* |
1021
|
|
|
* @param SimpleXMLElement $cascadeElement The cascade element. |
1022
|
|
|
* |
1023
|
|
|
* @return string[] The list of cascade options. |
1024
|
|
|
*/ |
1025
|
10 |
|
private function getCascadeMappings(SimpleXMLElement $cascadeElement) : array |
1026
|
|
|
{ |
1027
|
10 |
|
$cascades = []; |
1028
|
|
|
|
1029
|
|
|
/** @var SimpleXMLElement $action */ |
1030
|
10 |
|
foreach ($cascadeElement->children() as $action) { |
1031
|
|
|
// According to the JPA specifications, XML uses "cascade-persist" |
1032
|
|
|
// instead of "persist". Here, both variations are supported |
1033
|
|
|
// because Annotation use "persist" and we want to make sure that |
1034
|
|
|
// this driver doesn't need to know anything about the supported |
1035
|
|
|
// cascading actions |
1036
|
10 |
|
$cascades[] = str_replace('cascade-', '', $action->getName()); |
1037
|
|
|
} |
1038
|
|
|
|
1039
|
10 |
|
return $cascades; |
1040
|
|
|
} |
1041
|
|
|
|
1042
|
|
|
/** |
1043
|
|
|
* {@inheritDoc} |
1044
|
|
|
*/ |
1045
|
36 |
|
protected function loadMappingFile($file) |
1046
|
|
|
{ |
1047
|
36 |
|
$result = []; |
1048
|
|
|
// Note: we do not use `simplexml_load_file()` because of https://bugs.php.net/bug.php?id=62577 |
1049
|
36 |
|
$xmlElement = simplexml_load_string(file_get_contents($file)); |
1050
|
|
|
|
1051
|
36 |
|
if (isset($xmlElement->entity)) { |
1052
|
35 |
|
foreach ($xmlElement->entity as $entityElement) { |
1053
|
35 |
|
$entityName = (string) $entityElement['name']; |
1054
|
35 |
|
$result[$entityName] = $entityElement; |
1055
|
|
|
} |
1056
|
6 |
|
} elseif (isset($xmlElement->{'mapped-superclass'})) { |
1057
|
5 |
|
foreach ($xmlElement->{'mapped-superclass'} as $mappedSuperClass) { |
1058
|
5 |
|
$className = (string) $mappedSuperClass['name']; |
1059
|
5 |
|
$result[$className] = $mappedSuperClass; |
1060
|
|
|
} |
1061
|
1 |
|
} elseif (isset($xmlElement->embeddable)) { |
1062
|
|
|
foreach ($xmlElement->embeddable as $embeddableElement) { |
1063
|
|
|
$embeddableName = (string) $embeddableElement['name']; |
1064
|
|
|
$result[$embeddableName] = $embeddableElement; |
1065
|
|
|
} |
1066
|
|
|
} |
1067
|
|
|
|
1068
|
36 |
|
return $result; |
1069
|
|
|
} |
1070
|
|
|
|
1071
|
|
|
/** |
1072
|
|
|
* @param mixed $element |
1073
|
|
|
* |
1074
|
|
|
* @return bool |
1075
|
|
|
*/ |
1076
|
9 |
|
protected function evaluateBoolean($element) |
1077
|
|
|
{ |
1078
|
9 |
|
$flag = (string) $element; |
1079
|
|
|
|
1080
|
9 |
|
return $flag === 'true' || $flag === '1'; |
1081
|
|
|
} |
1082
|
|
|
} |
1083
|
|
|
|