1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\ORM\Mapping\Driver; |
21
|
|
|
|
22
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
23
|
|
|
use Doctrine\ORM\Events; |
24
|
|
|
use Doctrine\ORM\Mapping; |
25
|
|
|
use Doctrine\ORM\Mapping\Builder\EntityListenerBuilder; |
26
|
|
|
use Doctrine\ORM\Mapping\MappingException; |
27
|
|
|
use Doctrine\Persistence\Mapping\ClassMetadata; |
28
|
|
|
use Doctrine\Persistence\Mapping\Driver\AnnotationDriver as AbstractAnnotationDriver; |
29
|
|
|
use function interface_exists; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The AnnotationDriver reads the mapping metadata from docblock annotations. |
33
|
|
|
* |
34
|
|
|
* @since 2.0 |
35
|
|
|
* @author Benjamin Eberlei <[email protected]> |
36
|
|
|
* @author Guilherme Blanco <[email protected]> |
37
|
|
|
* @author Jonathan H. Wage <[email protected]> |
38
|
|
|
* @author Roman Borschel <[email protected]> |
39
|
|
|
*/ |
40
|
|
|
class AnnotationDriver extends AbstractAnnotationDriver |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* {@inheritDoc} |
44
|
|
|
*/ |
45
|
|
|
protected $entityAnnotationClasses = [ |
46
|
|
|
Mapping\Entity::class => 1, |
47
|
|
|
Mapping\MappedSuperclass::class => 2, |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritDoc} |
52
|
|
|
*/ |
53
|
438 |
|
public function loadMetadataForClass($className, ClassMetadata $metadata) |
54
|
|
|
{ |
55
|
|
|
/* @var $metadata \Doctrine\ORM\Mapping\ClassMetadataInfo */ |
56
|
438 |
|
$class = $metadata->getReflectionClass(); |
57
|
|
|
|
58
|
438 |
|
if ( ! $class) { |
|
|
|
|
59
|
|
|
// this happens when running annotation driver in combination with |
60
|
|
|
// static reflection services. This is not the nicest fix |
61
|
1 |
|
$class = new \ReflectionClass($metadata->name); |
62
|
|
|
} |
63
|
|
|
|
64
|
438 |
|
$classAnnotations = $this->reader->getClassAnnotations($class); |
65
|
|
|
|
66
|
438 |
|
if ($classAnnotations) { |
|
|
|
|
67
|
432 |
|
foreach ($classAnnotations as $key => $annot) { |
68
|
432 |
|
if ( ! is_numeric($key)) { |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
|
72
|
432 |
|
$classAnnotations[get_class($annot)] = $annot; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Evaluate Entity annotation |
77
|
438 |
|
if (isset($classAnnotations[Mapping\Entity::class])) { |
78
|
427 |
|
$entityAnnot = $classAnnotations[Mapping\Entity::class]; |
79
|
427 |
|
if ($entityAnnot->repositoryClass !== null) { |
80
|
8 |
|
$metadata->setCustomRepositoryClass($entityAnnot->repositoryClass); |
81
|
|
|
} |
82
|
|
|
|
83
|
427 |
|
if ($entityAnnot->readOnly) { |
84
|
427 |
|
$metadata->markReadOnly(); |
85
|
|
|
} |
86
|
47 |
|
} else if (isset($classAnnotations[Mapping\MappedSuperclass::class])) { |
87
|
31 |
|
$mappedSuperclassAnnot = $classAnnotations[Mapping\MappedSuperclass::class]; |
88
|
|
|
|
89
|
31 |
|
$metadata->setCustomRepositoryClass($mappedSuperclassAnnot->repositoryClass); |
90
|
31 |
|
$metadata->isMappedSuperclass = true; |
91
|
18 |
|
} else if (isset($classAnnotations[Mapping\Embeddable::class])) { |
92
|
12 |
|
$metadata->isEmbeddedClass = true; |
93
|
|
|
} else { |
94
|
6 |
|
throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Evaluate Table annotation |
98
|
432 |
|
if (isset($classAnnotations[Mapping\Table::class])) { |
99
|
222 |
|
$tableAnnot = $classAnnotations[Mapping\Table::class]; |
100
|
|
|
$primaryTable = [ |
101
|
222 |
|
'name' => $tableAnnot->name, |
102
|
222 |
|
'schema' => $tableAnnot->schema |
103
|
|
|
]; |
104
|
|
|
|
105
|
222 |
|
if ($tableAnnot->indexes !== null) { |
106
|
16 |
|
foreach ($tableAnnot->indexes as $indexAnnot) { |
107
|
16 |
|
$index = ['columns' => $indexAnnot->columns]; |
108
|
|
|
|
109
|
16 |
|
if ( ! empty($indexAnnot->flags)) { |
110
|
1 |
|
$index['flags'] = $indexAnnot->flags; |
111
|
|
|
} |
112
|
|
|
|
113
|
16 |
|
if ( ! empty($indexAnnot->options)) { |
114
|
1 |
|
$index['options'] = $indexAnnot->options; |
115
|
|
|
} |
116
|
|
|
|
117
|
16 |
|
if ( ! empty($indexAnnot->name)) { |
118
|
15 |
|
$primaryTable['indexes'][$indexAnnot->name] = $index; |
119
|
|
|
} else { |
120
|
16 |
|
$primaryTable['indexes'][] = $index; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
222 |
|
if ($tableAnnot->uniqueConstraints !== null) { |
126
|
9 |
|
foreach ($tableAnnot->uniqueConstraints as $uniqueConstraintAnnot) { |
127
|
9 |
|
$uniqueConstraint = ['columns' => $uniqueConstraintAnnot->columns]; |
128
|
|
|
|
129
|
9 |
|
if ( ! empty($uniqueConstraintAnnot->options)) { |
130
|
3 |
|
$uniqueConstraint['options'] = $uniqueConstraintAnnot->options; |
131
|
|
|
} |
132
|
|
|
|
133
|
9 |
|
if ( ! empty($uniqueConstraintAnnot->name)) { |
134
|
9 |
|
$primaryTable['uniqueConstraints'][$uniqueConstraintAnnot->name] = $uniqueConstraint; |
135
|
|
|
} else { |
136
|
9 |
|
$primaryTable['uniqueConstraints'][] = $uniqueConstraint; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
222 |
|
if ($tableAnnot->options) { |
142
|
6 |
|
$primaryTable['options'] = $tableAnnot->options; |
143
|
|
|
} |
144
|
|
|
|
145
|
222 |
|
$metadata->setPrimaryTable($primaryTable); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// Evaluate @Cache annotation |
149
|
432 |
|
if (isset($classAnnotations[Mapping\Cache::class])) { |
150
|
19 |
|
$cacheAnnot = $classAnnotations[Mapping\Cache::class]; |
151
|
|
|
$cacheMap = [ |
152
|
19 |
|
'region' => $cacheAnnot->region, |
153
|
19 |
|
'usage' => constant('Doctrine\ORM\Mapping\ClassMetadata::CACHE_USAGE_' . $cacheAnnot->usage), |
154
|
|
|
]; |
155
|
|
|
|
156
|
19 |
|
$metadata->enableCache($cacheMap); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// Evaluate NamedNativeQueries annotation |
160
|
432 |
|
if (isset($classAnnotations[Mapping\NamedNativeQueries::class])) { |
161
|
17 |
|
$namedNativeQueriesAnnot = $classAnnotations[Mapping\NamedNativeQueries::class]; |
162
|
|
|
|
163
|
17 |
|
foreach ($namedNativeQueriesAnnot->value as $namedNativeQuery) { |
164
|
17 |
|
$metadata->addNamedNativeQuery( |
165
|
|
|
[ |
166
|
17 |
|
'name' => $namedNativeQuery->name, |
167
|
17 |
|
'query' => $namedNativeQuery->query, |
168
|
17 |
|
'resultClass' => $namedNativeQuery->resultClass, |
169
|
17 |
|
'resultSetMapping' => $namedNativeQuery->resultSetMapping, |
170
|
|
|
] |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// Evaluate SqlResultSetMappings annotation |
176
|
432 |
|
if (isset($classAnnotations[Mapping\SqlResultSetMappings::class])) { |
177
|
17 |
|
$sqlResultSetMappingsAnnot = $classAnnotations[Mapping\SqlResultSetMappings::class]; |
178
|
|
|
|
179
|
17 |
|
foreach ($sqlResultSetMappingsAnnot->value as $resultSetMapping) { |
180
|
17 |
|
$entities = []; |
181
|
17 |
|
$columns = []; |
182
|
17 |
|
foreach ($resultSetMapping->entities as $entityResultAnnot) { |
183
|
|
|
$entityResult = [ |
184
|
17 |
|
'fields' => [], |
185
|
17 |
|
'entityClass' => $entityResultAnnot->entityClass, |
186
|
17 |
|
'discriminatorColumn' => $entityResultAnnot->discriminatorColumn, |
187
|
|
|
]; |
188
|
|
|
|
189
|
17 |
|
foreach ($entityResultAnnot->fields as $fieldResultAnnot) { |
190
|
17 |
|
$entityResult['fields'][] = [ |
191
|
17 |
|
'name' => $fieldResultAnnot->name, |
192
|
17 |
|
'column' => $fieldResultAnnot->column |
193
|
|
|
]; |
194
|
|
|
} |
195
|
|
|
|
196
|
17 |
|
$entities[] = $entityResult; |
197
|
|
|
} |
198
|
|
|
|
199
|
17 |
|
foreach ($resultSetMapping->columns as $columnResultAnnot) { |
200
|
10 |
|
$columns[] = [ |
201
|
10 |
|
'name' => $columnResultAnnot->name, |
202
|
|
|
]; |
203
|
|
|
} |
204
|
|
|
|
205
|
17 |
|
$metadata->addSqlResultSetMapping( |
206
|
|
|
[ |
207
|
17 |
|
'name' => $resultSetMapping->name, |
208
|
17 |
|
'entities' => $entities, |
209
|
17 |
|
'columns' => $columns |
210
|
|
|
] |
211
|
|
|
); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// Evaluate NamedQueries annotation |
216
|
432 |
|
if (isset($classAnnotations[Mapping\NamedQueries::class])) { |
217
|
11 |
|
$namedQueriesAnnot = $classAnnotations[Mapping\NamedQueries::class]; |
218
|
|
|
|
219
|
11 |
|
if ( ! is_array($namedQueriesAnnot->value)) { |
220
|
|
|
throw new \UnexpectedValueException("@NamedQueries should contain an array of @NamedQuery annotations."); |
221
|
|
|
} |
222
|
|
|
|
223
|
11 |
|
foreach ($namedQueriesAnnot->value as $namedQuery) { |
224
|
11 |
|
if ( ! ($namedQuery instanceof Mapping\NamedQuery)) { |
225
|
|
|
throw new \UnexpectedValueException("@NamedQueries should contain an array of @NamedQuery annotations."); |
226
|
|
|
} |
227
|
11 |
|
$metadata->addNamedQuery( |
228
|
|
|
[ |
229
|
11 |
|
'name' => $namedQuery->name, |
230
|
11 |
|
'query' => $namedQuery->query |
231
|
|
|
] |
232
|
|
|
); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// Evaluate InheritanceType annotation |
237
|
432 |
|
if (isset($classAnnotations[Mapping\InheritanceType::class])) { |
238
|
86 |
|
$inheritanceTypeAnnot = $classAnnotations[Mapping\InheritanceType::class]; |
239
|
|
|
|
240
|
86 |
|
$metadata->setInheritanceType( |
241
|
86 |
|
constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceTypeAnnot->value) |
242
|
|
|
); |
243
|
|
|
|
244
|
86 |
|
if ($metadata->inheritanceType != Mapping\ClassMetadata::INHERITANCE_TYPE_NONE) { |
245
|
|
|
// Evaluate DiscriminatorColumn annotation |
246
|
86 |
|
if (isset($classAnnotations[Mapping\DiscriminatorColumn::class])) { |
247
|
69 |
|
$discrColumnAnnot = $classAnnotations[Mapping\DiscriminatorColumn::class]; |
248
|
|
|
|
249
|
69 |
|
$metadata->setDiscriminatorColumn( |
250
|
|
|
[ |
251
|
69 |
|
'name' => $discrColumnAnnot->name, |
252
|
69 |
|
'type' => $discrColumnAnnot->type ?: 'string', |
253
|
69 |
|
'length' => $discrColumnAnnot->length ?: 255, |
254
|
69 |
|
'columnDefinition' => $discrColumnAnnot->columnDefinition, |
255
|
|
|
] |
256
|
|
|
); |
257
|
|
|
} else { |
258
|
20 |
|
$metadata->setDiscriminatorColumn(['name' => 'dtype', 'type' => 'string', 'length' => 255]); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
// Evaluate DiscriminatorMap annotation |
262
|
86 |
|
if (isset($classAnnotations[Mapping\DiscriminatorMap::class])) { |
263
|
86 |
|
$discrMapAnnot = $classAnnotations[Mapping\DiscriminatorMap::class]; |
264
|
86 |
|
$metadata->setDiscriminatorMap($discrMapAnnot->value); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
|
270
|
|
|
// Evaluate DoctrineChangeTrackingPolicy annotation |
271
|
432 |
|
if (isset($classAnnotations[Mapping\ChangeTrackingPolicy::class])) { |
272
|
7 |
|
$changeTrackingAnnot = $classAnnotations[Mapping\ChangeTrackingPolicy::class]; |
273
|
7 |
|
$metadata->setChangeTrackingPolicy(constant('Doctrine\ORM\Mapping\ClassMetadata::CHANGETRACKING_' . $changeTrackingAnnot->value)); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
// Evaluate annotations on properties/fields |
277
|
|
|
/* @var $property \ReflectionProperty */ |
278
|
432 |
|
foreach ($class->getProperties() as $property) { |
279
|
430 |
|
if ($metadata->isMappedSuperclass && ! $property->isPrivate() |
280
|
|
|
|| |
281
|
430 |
|
$metadata->isInheritedField($property->name) |
282
|
|
|
|| |
283
|
430 |
|
$metadata->isInheritedAssociation($property->name) |
284
|
|
|
|| |
285
|
430 |
|
$metadata->isInheritedEmbeddedClass($property->name)) { |
286
|
83 |
|
continue; |
287
|
|
|
} |
288
|
|
|
|
289
|
430 |
|
$mapping = []; |
290
|
430 |
|
$mapping['fieldName'] = $property->getName(); |
291
|
|
|
|
292
|
|
|
// Evaluate @Cache annotation |
293
|
430 |
|
if (($cacheAnnot = $this->reader->getPropertyAnnotation($property, Mapping\Cache::class)) !== null) { |
294
|
15 |
|
$mapping['cache'] = $metadata->getAssociationCacheDefaults( |
295
|
15 |
|
$mapping['fieldName'], |
296
|
|
|
[ |
297
|
15 |
|
'usage' => constant('Doctrine\ORM\Mapping\ClassMetadata::CACHE_USAGE_' . $cacheAnnot->usage), |
298
|
15 |
|
'region' => $cacheAnnot->region, |
299
|
|
|
] |
300
|
|
|
); |
301
|
|
|
} |
302
|
|
|
// Check for JoinColumn/JoinColumns annotations |
303
|
429 |
|
$joinColumns = []; |
304
|
|
|
|
305
|
429 |
|
if ($joinColumnAnnot = $this->reader->getPropertyAnnotation($property, Mapping\JoinColumn::class)) { |
306
|
158 |
|
$joinColumns[] = $this->joinColumnToArray($joinColumnAnnot); |
307
|
427 |
|
} else if ($joinColumnsAnnot = $this->reader->getPropertyAnnotation($property, Mapping\JoinColumns::class)) { |
308
|
19 |
|
foreach ($joinColumnsAnnot->value as $joinColumn) { |
309
|
19 |
|
$joinColumns[] = $this->joinColumnToArray($joinColumn); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
// Field can only be annotated with one of: |
314
|
|
|
// @Column, @OneToOne, @OneToMany, @ManyToOne, @ManyToMany |
315
|
429 |
|
if ($columnAnnot = $this->reader->getPropertyAnnotation($property, Mapping\Column::class)) { |
316
|
420 |
|
if ($columnAnnot->type == null) { |
317
|
|
|
throw MappingException::propertyTypeIsRequired($className, $property->getName()); |
318
|
|
|
} |
319
|
|
|
|
320
|
420 |
|
$mapping = $this->columnToArray($property->getName(), $columnAnnot); |
321
|
|
|
|
322
|
420 |
|
if ($idAnnot = $this->reader->getPropertyAnnotation($property, Mapping\Id::class)) { |
|
|
|
|
323
|
414 |
|
$mapping['id'] = true; |
324
|
|
|
} |
325
|
|
|
|
326
|
420 |
|
if ($generatedValueAnnot = $this->reader->getPropertyAnnotation($property, Mapping\GeneratedValue::class)) { |
327
|
352 |
|
$metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' . $generatedValueAnnot->strategy)); |
328
|
|
|
} |
329
|
|
|
|
330
|
420 |
|
if ($this->reader->getPropertyAnnotation($property, Mapping\Version::class)) { |
331
|
18 |
|
$metadata->setVersionMapping($mapping); |
332
|
|
|
} |
333
|
|
|
|
334
|
420 |
|
$metadata->mapField($mapping); |
335
|
|
|
|
336
|
|
|
// Check for SequenceGenerator/TableGenerator definition |
337
|
420 |
|
if ($seqGeneratorAnnot = $this->reader->getPropertyAnnotation($property, Mapping\SequenceGenerator::class)) { |
338
|
9 |
|
$metadata->setSequenceGeneratorDefinition( |
339
|
|
|
[ |
340
|
9 |
|
'sequenceName' => $seqGeneratorAnnot->sequenceName, |
341
|
9 |
|
'allocationSize' => $seqGeneratorAnnot->allocationSize, |
342
|
9 |
|
'initialValue' => $seqGeneratorAnnot->initialValue |
343
|
|
|
] |
344
|
|
|
); |
345
|
417 |
|
} else if ($this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\TableGenerator')) { |
346
|
|
|
throw MappingException::tableIdGeneratorNotImplemented($className); |
347
|
417 |
|
} else if ($customGeneratorAnnot = $this->reader->getPropertyAnnotation($property, Mapping\CustomIdGenerator::class)) { |
348
|
3 |
|
$metadata->setCustomGeneratorDefinition( |
349
|
|
|
[ |
350
|
420 |
|
'class' => $customGeneratorAnnot->class |
351
|
|
|
] |
352
|
|
|
); |
353
|
|
|
} |
354
|
298 |
|
} else if ($oneToOneAnnot = $this->reader->getPropertyAnnotation($property, Mapping\OneToOne::class)) { |
355
|
125 |
|
if ($idAnnot = $this->reader->getPropertyAnnotation($property, Mapping\Id::class)) { |
356
|
12 |
|
$mapping['id'] = true; |
357
|
|
|
} |
358
|
|
|
|
359
|
125 |
|
$mapping['targetEntity'] = $oneToOneAnnot->targetEntity; |
360
|
125 |
|
$mapping['joinColumns'] = $joinColumns; |
361
|
125 |
|
$mapping['mappedBy'] = $oneToOneAnnot->mappedBy; |
362
|
125 |
|
$mapping['inversedBy'] = $oneToOneAnnot->inversedBy; |
363
|
125 |
|
$mapping['cascade'] = $oneToOneAnnot->cascade; |
364
|
125 |
|
$mapping['orphanRemoval'] = $oneToOneAnnot->orphanRemoval; |
365
|
125 |
|
$mapping['fetch'] = $this->getFetchMode($className, $oneToOneAnnot->fetch); |
366
|
125 |
|
$metadata->mapOneToOne($mapping); |
367
|
244 |
|
} else if ($oneToManyAnnot = $this->reader->getPropertyAnnotation($property, Mapping\OneToMany::class)) { |
368
|
121 |
|
$mapping['mappedBy'] = $oneToManyAnnot->mappedBy; |
369
|
121 |
|
$mapping['targetEntity'] = $oneToManyAnnot->targetEntity; |
370
|
121 |
|
$mapping['cascade'] = $oneToManyAnnot->cascade; |
371
|
121 |
|
$mapping['indexBy'] = $oneToManyAnnot->indexBy; |
372
|
121 |
|
$mapping['orphanRemoval'] = $oneToManyAnnot->orphanRemoval; |
373
|
121 |
|
$mapping['fetch'] = $this->getFetchMode($className, $oneToManyAnnot->fetch); |
374
|
|
|
|
375
|
121 |
|
if ($orderByAnnot = $this->reader->getPropertyAnnotation($property, Mapping\OrderBy::class)) { |
376
|
18 |
|
$mapping['orderBy'] = $orderByAnnot->value; |
377
|
|
|
} |
378
|
|
|
|
379
|
121 |
|
$metadata->mapOneToMany($mapping); |
380
|
240 |
|
} else if ($manyToOneAnnot = $this->reader->getPropertyAnnotation($property, Mapping\ManyToOne::class)) { |
381
|
157 |
|
if ($idAnnot = $this->reader->getPropertyAnnotation($property, Mapping\Id::class)) { |
382
|
36 |
|
$mapping['id'] = true; |
383
|
|
|
} |
384
|
|
|
|
385
|
157 |
|
$mapping['joinColumns'] = $joinColumns; |
386
|
157 |
|
$mapping['cascade'] = $manyToOneAnnot->cascade; |
387
|
157 |
|
$mapping['inversedBy'] = $manyToOneAnnot->inversedBy; |
388
|
157 |
|
$mapping['targetEntity'] = $manyToOneAnnot->targetEntity; |
389
|
157 |
|
$mapping['fetch'] = $this->getFetchMode($className, $manyToOneAnnot->fetch); |
390
|
157 |
|
$metadata->mapManyToOne($mapping); |
391
|
134 |
|
} else if ($manyToManyAnnot = $this->reader->getPropertyAnnotation($property, Mapping\ManyToMany::class)) { |
392
|
99 |
|
$joinTable = []; |
393
|
|
|
|
394
|
99 |
|
if ($joinTableAnnot = $this->reader->getPropertyAnnotation($property, Mapping\JoinTable::class)) { |
395
|
|
|
$joinTable = [ |
396
|
79 |
|
'name' => $joinTableAnnot->name, |
397
|
79 |
|
'schema' => $joinTableAnnot->schema |
398
|
|
|
]; |
399
|
|
|
|
400
|
79 |
|
foreach ($joinTableAnnot->joinColumns as $joinColumn) { |
401
|
77 |
|
$joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumn); |
402
|
|
|
} |
403
|
|
|
|
404
|
79 |
|
foreach ($joinTableAnnot->inverseJoinColumns as $joinColumn) { |
405
|
78 |
|
$joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumn); |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|
409
|
99 |
|
$mapping['joinTable'] = $joinTable; |
410
|
99 |
|
$mapping['targetEntity'] = $manyToManyAnnot->targetEntity; |
411
|
99 |
|
$mapping['mappedBy'] = $manyToManyAnnot->mappedBy; |
412
|
99 |
|
$mapping['inversedBy'] = $manyToManyAnnot->inversedBy; |
413
|
99 |
|
$mapping['cascade'] = $manyToManyAnnot->cascade; |
414
|
99 |
|
$mapping['indexBy'] = $manyToManyAnnot->indexBy; |
415
|
99 |
|
$mapping['orphanRemoval'] = $manyToManyAnnot->orphanRemoval; |
416
|
99 |
|
$mapping['fetch'] = $this->getFetchMode($className, $manyToManyAnnot->fetch); |
417
|
|
|
|
418
|
99 |
|
if ($orderByAnnot = $this->reader->getPropertyAnnotation($property, Mapping\OrderBy::class)) { |
419
|
3 |
|
$mapping['orderBy'] = $orderByAnnot->value; |
420
|
|
|
} |
421
|
|
|
|
422
|
99 |
|
$metadata->mapManyToMany($mapping); |
423
|
55 |
|
} else if ($embeddedAnnot = $this->reader->getPropertyAnnotation($property, Mapping\Embedded::class)) { |
424
|
13 |
|
$mapping['class'] = $embeddedAnnot->class; |
425
|
13 |
|
$mapping['columnPrefix'] = $embeddedAnnot->columnPrefix; |
426
|
|
|
|
427
|
428 |
|
$metadata->mapEmbedded($mapping); |
428
|
|
|
} |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
// Evaluate AssociationOverrides annotation |
432
|
430 |
|
if (isset($classAnnotations[Mapping\AssociationOverrides::class])) { |
433
|
5 |
|
$associationOverridesAnnot = $classAnnotations[Mapping\AssociationOverrides::class]; |
434
|
|
|
|
435
|
5 |
|
foreach ($associationOverridesAnnot->value as $associationOverride) { |
436
|
5 |
|
$override = []; |
437
|
5 |
|
$fieldName = $associationOverride->name; |
438
|
|
|
|
439
|
|
|
// Check for JoinColumn/JoinColumns annotations |
440
|
5 |
|
if ($associationOverride->joinColumns) { |
441
|
3 |
|
$joinColumns = []; |
442
|
|
|
|
443
|
3 |
|
foreach ($associationOverride->joinColumns as $joinColumn) { |
444
|
3 |
|
$joinColumns[] = $this->joinColumnToArray($joinColumn); |
445
|
|
|
} |
446
|
|
|
|
447
|
3 |
|
$override['joinColumns'] = $joinColumns; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
// Check for JoinTable annotations |
451
|
5 |
|
if ($associationOverride->joinTable) { |
452
|
2 |
|
$joinTableAnnot = $associationOverride->joinTable; |
453
|
|
|
$joinTable = [ |
454
|
2 |
|
'name' => $joinTableAnnot->name, |
455
|
2 |
|
'schema' => $joinTableAnnot->schema |
456
|
|
|
]; |
457
|
|
|
|
458
|
2 |
|
foreach ($joinTableAnnot->joinColumns as $joinColumn) { |
459
|
2 |
|
$joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumn); |
460
|
|
|
} |
461
|
|
|
|
462
|
2 |
|
foreach ($joinTableAnnot->inverseJoinColumns as $joinColumn) { |
463
|
2 |
|
$joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumn); |
464
|
|
|
} |
465
|
|
|
|
466
|
2 |
|
$override['joinTable'] = $joinTable; |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
// Check for inversedBy |
470
|
5 |
|
if ($associationOverride->inversedBy) { |
471
|
1 |
|
$override['inversedBy'] = $associationOverride->inversedBy; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
// Check for `fetch` |
475
|
5 |
|
if ($associationOverride->fetch) { |
476
|
1 |
|
$override['fetch'] = constant(Mapping\ClassMetadata::class . '::FETCH_' . $associationOverride->fetch); |
477
|
|
|
} |
478
|
|
|
|
479
|
5 |
|
$metadata->setAssociationOverride($fieldName, $override); |
480
|
|
|
} |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
// Evaluate AttributeOverrides annotation |
484
|
430 |
|
if (isset($classAnnotations[Mapping\AttributeOverrides::class])) { |
485
|
3 |
|
$attributeOverridesAnnot = $classAnnotations[Mapping\AttributeOverrides::class]; |
486
|
|
|
|
487
|
3 |
|
foreach ($attributeOverridesAnnot->value as $attributeOverrideAnnot) { |
488
|
3 |
|
$attributeOverride = $this->columnToArray($attributeOverrideAnnot->name, $attributeOverrideAnnot->column); |
489
|
|
|
|
490
|
3 |
|
$metadata->setAttributeOverride($attributeOverrideAnnot->name, $attributeOverride); |
491
|
|
|
} |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
// Evaluate EntityListeners annotation |
495
|
430 |
|
if (isset($classAnnotations[Mapping\EntityListeners::class])) { |
496
|
12 |
|
$entityListenersAnnot = $classAnnotations[Mapping\EntityListeners::class]; |
497
|
|
|
|
498
|
12 |
|
foreach ($entityListenersAnnot->value as $item) { |
499
|
12 |
|
$listenerClassName = $metadata->fullyQualifiedClassName($item); |
500
|
|
|
|
501
|
12 |
|
if ( ! class_exists($listenerClassName)) { |
502
|
|
|
throw MappingException::entityListenerClassNotFound($listenerClassName, $className); |
503
|
|
|
} |
504
|
|
|
|
505
|
12 |
|
$hasMapping = false; |
506
|
12 |
|
$listenerClass = new \ReflectionClass($listenerClassName); |
507
|
|
|
|
508
|
|
|
/* @var $method \ReflectionMethod */ |
509
|
12 |
|
foreach ($listenerClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
510
|
|
|
// find method callbacks. |
511
|
12 |
|
$callbacks = $this->getMethodCallbacks($method); |
512
|
12 |
|
$hasMapping = $hasMapping ?: ( ! empty($callbacks)); |
513
|
|
|
|
514
|
12 |
|
foreach ($callbacks as $value) { |
515
|
12 |
|
$metadata->addEntityListener($value[1], $listenerClassName, $value[0]); |
516
|
|
|
} |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
// Evaluate the listener using naming convention. |
520
|
12 |
|
if ( ! $hasMapping ) { |
521
|
12 |
|
EntityListenerBuilder::bindEntityListener($metadata, $listenerClassName); |
522
|
|
|
} |
523
|
|
|
} |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
// Evaluate @HasLifecycleCallbacks annotation |
527
|
430 |
|
if (isset($classAnnotations[Mapping\HasLifecycleCallbacks::class])) { |
528
|
|
|
/* @var $method \ReflectionMethod */ |
529
|
19 |
|
foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
530
|
18 |
|
foreach ($this->getMethodCallbacks($method) as $value) { |
531
|
18 |
|
$metadata->addLifecycleCallback($value[0], $value[1]); |
532
|
|
|
} |
533
|
|
|
} |
534
|
|
|
} |
535
|
430 |
|
} |
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* Attempts to resolve the fetch mode. |
539
|
|
|
* |
540
|
|
|
* @param string $className The class name. |
541
|
|
|
* @param string $fetchMode The fetch mode. |
542
|
|
|
* |
543
|
|
|
* @return integer The fetch mode as defined in ClassMetadata. |
544
|
|
|
* |
545
|
|
|
* @throws MappingException If the fetch mode is not valid. |
546
|
|
|
*/ |
547
|
283 |
|
private function getFetchMode($className, $fetchMode) |
548
|
|
|
{ |
549
|
283 |
|
if ( ! defined('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $fetchMode)) { |
550
|
|
|
throw MappingException::invalidFetchMode($className, $fetchMode); |
551
|
|
|
} |
552
|
|
|
|
553
|
283 |
|
return constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $fetchMode); |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
/** |
557
|
|
|
* Parses the given method. |
558
|
|
|
* |
559
|
|
|
* @param \ReflectionMethod $method |
560
|
|
|
* |
561
|
|
|
* @return array |
562
|
|
|
*/ |
563
|
28 |
|
private function getMethodCallbacks(\ReflectionMethod $method) |
564
|
|
|
{ |
565
|
28 |
|
$callbacks = []; |
566
|
28 |
|
$annotations = $this->reader->getMethodAnnotations($method); |
567
|
|
|
|
568
|
28 |
|
foreach ($annotations as $annot) { |
569
|
22 |
|
if ($annot instanceof Mapping\PrePersist) { |
570
|
15 |
|
$callbacks[] = [$method->name, Events::prePersist]; |
571
|
|
|
} |
572
|
|
|
|
573
|
22 |
|
if ($annot instanceof Mapping\PostPersist) { |
574
|
11 |
|
$callbacks[] = [$method->name, Events::postPersist]; |
575
|
|
|
} |
576
|
|
|
|
577
|
22 |
|
if ($annot instanceof Mapping\PreUpdate) { |
578
|
11 |
|
$callbacks[] = [$method->name, Events::preUpdate]; |
579
|
|
|
} |
580
|
|
|
|
581
|
22 |
|
if ($annot instanceof Mapping\PostUpdate) { |
582
|
7 |
|
$callbacks[] = [$method->name, Events::postUpdate]; |
583
|
|
|
} |
584
|
|
|
|
585
|
22 |
|
if ($annot instanceof Mapping\PreRemove) { |
586
|
8 |
|
$callbacks[] = [$method->name, Events::preRemove]; |
587
|
|
|
} |
588
|
|
|
|
589
|
22 |
|
if ($annot instanceof Mapping\PostRemove) { |
590
|
6 |
|
$callbacks[] = [$method->name, Events::postRemove]; |
591
|
|
|
} |
592
|
|
|
|
593
|
22 |
|
if ($annot instanceof Mapping\PostLoad) { |
594
|
11 |
|
$callbacks[] = [$method->name, Events::postLoad]; |
595
|
|
|
} |
596
|
|
|
|
597
|
22 |
|
if ($annot instanceof Mapping\PreFlush) { |
598
|
22 |
|
$callbacks[] = [$method->name, Events::preFlush]; |
599
|
|
|
} |
600
|
|
|
} |
601
|
|
|
|
602
|
28 |
|
return $callbacks; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
/** |
606
|
|
|
* Parse the given JoinColumn as array |
607
|
|
|
* |
608
|
|
|
* @param Mapping\JoinColumn $joinColumn |
609
|
|
|
* @return array |
610
|
|
|
*/ |
611
|
197 |
|
private function joinColumnToArray(Mapping\JoinColumn $joinColumn) |
612
|
|
|
{ |
613
|
|
|
return [ |
614
|
197 |
|
'name' => $joinColumn->name, |
615
|
197 |
|
'unique' => $joinColumn->unique, |
616
|
197 |
|
'nullable' => $joinColumn->nullable, |
617
|
197 |
|
'onDelete' => $joinColumn->onDelete, |
618
|
197 |
|
'columnDefinition' => $joinColumn->columnDefinition, |
619
|
197 |
|
'referencedColumnName' => $joinColumn->referencedColumnName, |
620
|
|
|
]; |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
/** |
624
|
|
|
* Parse the given Column as array |
625
|
|
|
* |
626
|
|
|
* @param string $fieldName |
627
|
|
|
* @param Mapping\Column $column |
628
|
|
|
* |
629
|
|
|
* @return array |
630
|
|
|
*/ |
631
|
420 |
|
private function columnToArray($fieldName, Mapping\Column $column) |
632
|
|
|
{ |
633
|
|
|
$mapping = [ |
634
|
420 |
|
'fieldName' => $fieldName, |
635
|
420 |
|
'type' => $column->type, |
636
|
420 |
|
'scale' => $column->scale, |
637
|
420 |
|
'length' => $column->length, |
638
|
420 |
|
'unique' => $column->unique, |
639
|
420 |
|
'nullable' => $column->nullable, |
640
|
420 |
|
'precision' => $column->precision |
641
|
|
|
]; |
642
|
|
|
|
643
|
420 |
|
if ($column->options) { |
|
|
|
|
644
|
11 |
|
$mapping['options'] = $column->options; |
645
|
|
|
} |
646
|
|
|
|
647
|
420 |
|
if (isset($column->name)) { |
648
|
87 |
|
$mapping['columnName'] = $column->name; |
649
|
|
|
} |
650
|
|
|
|
651
|
420 |
|
if (isset($column->columnDefinition)) { |
652
|
6 |
|
$mapping['columnDefinition'] = $column->columnDefinition; |
653
|
|
|
} |
654
|
|
|
|
655
|
420 |
|
return $mapping; |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
/** |
659
|
|
|
* Factory method for the Annotation Driver. |
660
|
|
|
* |
661
|
|
|
* @param array|string $paths |
662
|
|
|
* @param AnnotationReader|null $reader |
663
|
|
|
* |
664
|
|
|
* @return AnnotationDriver |
665
|
|
|
*/ |
666
|
|
|
static public function create($paths = [], AnnotationReader $reader = null) |
667
|
|
|
{ |
668
|
|
|
if ($reader == null) { |
669
|
|
|
$reader = new AnnotationReader(); |
670
|
|
|
} |
671
|
|
|
|
672
|
|
|
return new self($reader, $paths); |
673
|
|
|
} |
674
|
|
|
} |
675
|
|
|
|
676
|
|
|
interface_exists(ClassMetadata::class); |
677
|
|
|
|