9
9
use Doctrine \ORM \Mapping ;
10
10
use Doctrine \ORM \Mapping \Builder \EntityListenerBuilder ;
11
11
use Doctrine \ORM \Mapping \ClassMetadata ;
12
+ use Doctrine \ORM \Mapping \MappingAttribute ;
12
13
use Doctrine \ORM \Mapping \MappingException ;
13
14
use Doctrine \Persistence \Mapping \ClassMetadata as PersistenceClassMetadata ;
14
15
use Doctrine \Persistence \Mapping \Driver \ColocatedMappingDriver ;
@@ -30,15 +31,20 @@ class AttributeDriver extends CompatibilityAnnotationDriver
30
31
{
31
32
use ColocatedMappingDriver;
32
33
33
- /** @var array<string,int> */
34
- // @phpcs:ignore
35
- protected $ entityAnnotationClasses = [
34
+ private const ENTITY_ATTRIBUTE_CLASSES = [
36
35
Mapping \Entity::class => 1 ,
37
36
Mapping \MappedSuperclass::class => 2 ,
38
37
];
39
38
40
39
/**
41
- * The annotation reader.
40
+ * @deprecated override isTransient() instead of overriding this property
41
+ *
42
+ * @var array<class-string<MappingAttribute>, int>
43
+ */
44
+ protected $ entityAnnotationClasses = self ::ENTITY_ATTRIBUTE_CLASSES ;
45
+
46
+ /**
47
+ * The attribute reader.
42
48
*
43
49
* @internal this property will be private in 3.0
44
50
*
@@ -58,6 +64,15 @@ public function __construct(array $paths)
58
64
59
65
$ this ->reader = new AttributeReader ();
60
66
$ this ->addPaths ($ paths );
67
+
68
+ if ($ this ->entityAnnotationClasses !== self ::ENTITY_ATTRIBUTE_CLASSES ) {
69
+ Deprecation::trigger (
70
+ 'doctrine/orm ' ,
71
+ 'https://github.com/doctrine/orm/pull/10204 ' ,
72
+ 'Changing the value of %s::$entityAnnotationClasses is deprecated and will have no effect in Doctrine ORM 3.0. ' ,
73
+ self ::class
74
+ );
75
+ }
61
76
}
62
77
63
78
/**
@@ -84,11 +99,11 @@ public function getReader()
84
99
*/
85
100
public function isTransient ($ className )
86
101
{
87
- $ classAnnotations = $ this ->reader ->getClassAnnotations (new ReflectionClass ($ className ));
102
+ $ classAttributes = $ this ->reader ->getClassAttributes (new ReflectionClass ($ className ));
88
103
89
- foreach ($ classAnnotations as $ a ) {
90
- $ annot = $ a instanceof RepeatableAttributeCollection ? $ a [0 ] : $ a ;
91
- if (isset ($ this ->entityAnnotationClasses [get_class ($ annot )])) {
104
+ foreach ($ classAttributes as $ a ) {
105
+ $ attr = $ a instanceof RepeatableAttributeCollection ? $ a [0 ] : $ a ;
106
+ if (isset ($ this ->entityAnnotationClasses [get_class ($ attr )])) {
92
107
return false ;
93
108
}
94
109
}
@@ -107,13 +122,13 @@ public function isTransient($className)
107
122
public function loadMetadataForClass ($ className , PersistenceClassMetadata $ metadata ): void
108
123
{
109
124
$ reflectionClass = $ metadata ->getReflectionClass ()
110
- // this happens when running annotation driver in combination with
125
+ // this happens when running attribute driver in combination with
111
126
// static reflection services. This is not the nicest fix
112
127
?? new ReflectionClass ($ metadata ->name );
113
128
114
- $ classAttributes = $ this ->reader ->getClassAnnotations ($ reflectionClass );
129
+ $ classAttributes = $ this ->reader ->getClassAttributes ($ reflectionClass );
115
130
116
- // Evaluate Entity annotation
131
+ // Evaluate Entity attribute
117
132
if (isset ($ classAttributes [Mapping \Entity::class])) {
118
133
$ entityAttribute = $ classAttributes [Mapping \Entity::class];
119
134
if ($ entityAttribute ->repositoryClass !== null ) {
@@ -226,7 +241,7 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
226
241
227
242
$ metadata ->setPrimaryTable ($ primaryTable );
228
243
229
- // Evaluate @ Cache annotation
244
+ // Evaluate #[ Cache] attribute
230
245
if (isset ($ classAttributes [Mapping \Cache::class])) {
231
246
$ cacheAttribute = $ classAttributes [Mapping \Cache::class];
232
247
$ cacheMap = [
@@ -237,7 +252,7 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
237
252
$ metadata ->enableCache ($ cacheMap );
238
253
}
239
254
240
- // Evaluate InheritanceType annotation
255
+ // Evaluate InheritanceType attribute
241
256
if (isset ($ classAttributes [Mapping \InheritanceType::class])) {
242
257
$ inheritanceTypeAttribute = $ classAttributes [Mapping \InheritanceType::class];
243
258
@@ -246,7 +261,7 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
246
261
);
247
262
248
263
if ($ metadata ->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE ) {
249
- // Evaluate DiscriminatorColumn annotation
264
+ // Evaluate DiscriminatorColumn attribute
250
265
if (isset ($ classAttributes [Mapping \DiscriminatorColumn::class])) {
251
266
$ discrColumnAttribute = $ classAttributes [Mapping \DiscriminatorColumn::class];
252
267
@@ -262,15 +277,15 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
262
277
$ metadata ->setDiscriminatorColumn (['name ' => 'dtype ' , 'type ' => 'string ' , 'length ' => 255 ]);
263
278
}
264
279
265
- // Evaluate DiscriminatorMap annotation
280
+ // Evaluate DiscriminatorMap attribute
266
281
if (isset ($ classAttributes [Mapping \DiscriminatorMap::class])) {
267
282
$ discrMapAttribute = $ classAttributes [Mapping \DiscriminatorMap::class];
268
283
$ metadata ->setDiscriminatorMap ($ discrMapAttribute ->value );
269
284
}
270
285
}
271
286
}
272
287
273
- // Evaluate DoctrineChangeTrackingPolicy annotation
288
+ // Evaluate DoctrineChangeTrackingPolicy attribute
274
289
if (isset ($ classAttributes [Mapping \ChangeTrackingPolicy::class])) {
275
290
$ changeTrackingAttribute = $ classAttributes [Mapping \ChangeTrackingPolicy::class];
276
291
$ metadata ->setChangeTrackingPolicy (constant ('Doctrine\ORM\Mapping\ClassMetadata::CHANGETRACKING_ ' . $ changeTrackingAttribute ->value ));
@@ -293,8 +308,8 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
293
308
$ mapping = [];
294
309
$ mapping ['fieldName ' ] = $ property ->getName ();
295
310
296
- // Evaluate @ Cache annotation
297
- $ cacheAttribute = $ this ->reader ->getPropertyAnnotation ($ property , Mapping \Cache::class);
311
+ // Evaluate #[ Cache] attribute
312
+ $ cacheAttribute = $ this ->reader ->getPropertyAttribute ($ property , Mapping \Cache::class);
298
313
if ($ cacheAttribute !== null ) {
299
314
assert ($ cacheAttribute instanceof Mapping \Cache);
300
315
@@ -307,46 +322,46 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
307
322
);
308
323
}
309
324
310
- // Check for JoinColumn/JoinColumns annotations
325
+ // Check for JoinColumn/JoinColumns attributes
311
326
$ joinColumns = [];
312
327
313
- $ joinColumnAttributes = $ this ->reader ->getPropertyAnnotationCollection ($ property , Mapping \JoinColumn::class);
328
+ $ joinColumnAttributes = $ this ->reader ->getPropertyAttributeCollection ($ property , Mapping \JoinColumn::class);
314
329
315
330
foreach ($ joinColumnAttributes as $ joinColumnAttribute ) {
316
331
$ joinColumns [] = $ this ->joinColumnToArray ($ joinColumnAttribute );
317
332
}
318
333
319
334
// Field can only be attributed with one of:
320
335
// Column, OneToOne, OneToMany, ManyToOne, ManyToMany, Embedded
321
- $ columnAttribute = $ this ->reader ->getPropertyAnnotation ($ property , Mapping \Column::class);
322
- $ oneToOneAttribute = $ this ->reader ->getPropertyAnnotation ($ property , Mapping \OneToOne::class);
323
- $ oneToManyAttribute = $ this ->reader ->getPropertyAnnotation ($ property , Mapping \OneToMany::class);
324
- $ manyToOneAttribute = $ this ->reader ->getPropertyAnnotation ($ property , Mapping \ManyToOne::class);
325
- $ manyToManyAttribute = $ this ->reader ->getPropertyAnnotation ($ property , Mapping \ManyToMany::class);
326
- $ embeddedAttribute = $ this ->reader ->getPropertyAnnotation ($ property , Mapping \Embedded::class);
336
+ $ columnAttribute = $ this ->reader ->getPropertyAttribute ($ property , Mapping \Column::class);
337
+ $ oneToOneAttribute = $ this ->reader ->getPropertyAttribute ($ property , Mapping \OneToOne::class);
338
+ $ oneToManyAttribute = $ this ->reader ->getPropertyAttribute ($ property , Mapping \OneToMany::class);
339
+ $ manyToOneAttribute = $ this ->reader ->getPropertyAttribute ($ property , Mapping \ManyToOne::class);
340
+ $ manyToManyAttribute = $ this ->reader ->getPropertyAttribute ($ property , Mapping \ManyToMany::class);
341
+ $ embeddedAttribute = $ this ->reader ->getPropertyAttribute ($ property , Mapping \Embedded::class);
327
342
328
343
if ($ columnAttribute !== null ) {
329
344
$ mapping = $ this ->columnToArray ($ property ->getName (), $ columnAttribute );
330
345
331
- if ($ this ->reader ->getPropertyAnnotation ($ property , Mapping \Id::class)) {
346
+ if ($ this ->reader ->getPropertyAttribute ($ property , Mapping \Id::class)) {
332
347
$ mapping ['id ' ] = true ;
333
348
}
334
349
335
- $ generatedValueAttribute = $ this ->reader ->getPropertyAnnotation ($ property , Mapping \GeneratedValue::class);
350
+ $ generatedValueAttribute = $ this ->reader ->getPropertyAttribute ($ property , Mapping \GeneratedValue::class);
336
351
337
352
if ($ generatedValueAttribute !== null ) {
338
353
$ metadata ->setIdGeneratorType (constant ('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_ ' . $ generatedValueAttribute ->strategy ));
339
354
}
340
355
341
- if ($ this ->reader ->getPropertyAnnotation ($ property , Mapping \Version::class)) {
356
+ if ($ this ->reader ->getPropertyAttribute ($ property , Mapping \Version::class)) {
342
357
$ metadata ->setVersionMapping ($ mapping );
343
358
}
344
359
345
360
$ metadata ->mapField ($ mapping );
346
361
347
362
// Check for SequenceGenerator/TableGenerator definition
348
- $ seqGeneratorAttribute = $ this ->reader ->getPropertyAnnotation ($ property , Mapping \SequenceGenerator::class);
349
- $ customGeneratorAttribute = $ this ->reader ->getPropertyAnnotation ($ property , Mapping \CustomIdGenerator::class);
363
+ $ seqGeneratorAttribute = $ this ->reader ->getPropertyAttribute ($ property , Mapping \SequenceGenerator::class);
364
+ $ customGeneratorAttribute = $ this ->reader ->getPropertyAttribute ($ property , Mapping \CustomIdGenerator::class);
350
365
351
366
if ($ seqGeneratorAttribute !== null ) {
352
367
$ metadata ->setSequenceGeneratorDefinition (
@@ -364,7 +379,7 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
364
379
);
365
380
}
366
381
} elseif ($ oneToOneAttribute !== null ) {
367
- if ($ this ->reader ->getPropertyAnnotation ($ property , Mapping \Id::class)) {
382
+ if ($ this ->reader ->getPropertyAttribute ($ property , Mapping \Id::class)) {
368
383
$ mapping ['id ' ] = true ;
369
384
}
370
385
@@ -384,15 +399,15 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
384
399
$ mapping ['orphanRemoval ' ] = $ oneToManyAttribute ->orphanRemoval ;
385
400
$ mapping ['fetch ' ] = $ this ->getFetchMode ($ className , $ oneToManyAttribute ->fetch );
386
401
387
- $ orderByAttribute = $ this ->reader ->getPropertyAnnotation ($ property , Mapping \OrderBy::class);
402
+ $ orderByAttribute = $ this ->reader ->getPropertyAttribute ($ property , Mapping \OrderBy::class);
388
403
389
404
if ($ orderByAttribute !== null ) {
390
405
$ mapping ['orderBy ' ] = $ orderByAttribute ->value ;
391
406
}
392
407
393
408
$ metadata ->mapOneToMany ($ mapping );
394
409
} elseif ($ manyToOneAttribute !== null ) {
395
- $ idAttribute = $ this ->reader ->getPropertyAnnotation ($ property , Mapping \Id::class);
410
+ $ idAttribute = $ this ->reader ->getPropertyAttribute ($ property , Mapping \Id::class);
396
411
397
412
if ($ idAttribute !== null ) {
398
413
$ mapping ['id ' ] = true ;
@@ -406,7 +421,7 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
406
421
$ metadata ->mapManyToOne ($ mapping );
407
422
} elseif ($ manyToManyAttribute !== null ) {
408
423
$ joinTable = [];
409
- $ joinTableAttribute = $ this ->reader ->getPropertyAnnotation ($ property , Mapping \JoinTable::class);
424
+ $ joinTableAttribute = $ this ->reader ->getPropertyAttribute ($ property , Mapping \JoinTable::class);
410
425
411
426
if ($ joinTableAttribute !== null ) {
412
427
$ joinTable = [
@@ -419,11 +434,11 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
419
434
}
420
435
}
421
436
422
- foreach ($ this ->reader ->getPropertyAnnotationCollection ($ property , Mapping \JoinColumn::class) as $ joinColumn ) {
437
+ foreach ($ this ->reader ->getPropertyAttributeCollection ($ property , Mapping \JoinColumn::class) as $ joinColumn ) {
423
438
$ joinTable ['joinColumns ' ][] = $ this ->joinColumnToArray ($ joinColumn );
424
439
}
425
440
426
- foreach ($ this ->reader ->getPropertyAnnotationCollection ($ property , Mapping \InverseJoinColumn::class) as $ joinColumn ) {
441
+ foreach ($ this ->reader ->getPropertyAttributeCollection ($ property , Mapping \InverseJoinColumn::class) as $ joinColumn ) {
427
442
$ joinTable ['inverseJoinColumns ' ][] = $ this ->joinColumnToArray ($ joinColumn );
428
443
}
429
444
@@ -436,7 +451,7 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
436
451
$ mapping ['orphanRemoval ' ] = $ manyToManyAttribute ->orphanRemoval ;
437
452
$ mapping ['fetch ' ] = $ this ->getFetchMode ($ className , $ manyToManyAttribute ->fetch );
438
453
439
- $ orderByAttribute = $ this ->reader ->getPropertyAnnotation ($ property , Mapping \OrderBy::class);
454
+ $ orderByAttribute = $ this ->reader ->getPropertyAttribute ($ property , Mapping \OrderBy::class);
440
455
441
456
if ($ orderByAttribute !== null ) {
442
457
$ mapping ['orderBy ' ] = $ orderByAttribute ->value ;
@@ -509,7 +524,7 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
509
524
}
510
525
}
511
526
512
- // Evaluate AttributeOverrides annotation
527
+ // Evaluate AttributeOverrides attribute
513
528
if (isset ($ classAttributes [Mapping \AttributeOverrides::class])) {
514
529
$ attributeOverridesAnnot = $ classAttributes [Mapping \AttributeOverrides::class];
515
530
@@ -520,7 +535,7 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
520
535
}
521
536
}
522
537
523
- // Evaluate EntityListeners annotation
538
+ // Evaluate EntityListeners attribute
524
539
if (isset ($ classAttributes [Mapping \EntityListeners::class])) {
525
540
$ entityListenersAttribute = $ classAttributes [Mapping \EntityListeners::class];
526
541
@@ -552,7 +567,7 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
552
567
}
553
568
}
554
569
555
- // Evaluate @ HasLifecycleCallbacks annotation
570
+ // Evaluate #[ HasLifecycleCallbacks] attribute
556
571
if (isset ($ classAttributes [Mapping \HasLifecycleCallbacks::class])) {
557
572
foreach ($ reflectionClass ->getMethods (ReflectionMethod::IS_PUBLIC ) as $ method ) {
558
573
assert ($ method instanceof ReflectionMethod);
@@ -604,7 +619,7 @@ private function getGeneratedMode(string $generatedMode): int
604
619
private function getMethodCallbacks (ReflectionMethod $ method ): array
605
620
{
606
621
$ callbacks = [];
607
- $ attributes = $ this ->reader ->getMethodAnnotations ($ method );
622
+ $ attributes = $ this ->reader ->getMethodAttributes ($ method );
608
623
609
624
foreach ($ attributes as $ attribute ) {
610
625
if ($ attribute instanceof Mapping \PrePersist) {
0 commit comments