13
13
14
14
use Doctrine \Common \Collections \ArrayCollection ;
15
15
use Doctrine \Common \Collections \Collection ;
16
+ use Doctrine \ORM \Mapping \Column ;
17
+ use Doctrine \ORM \Mapping \Embedded ;
18
+ use Doctrine \ORM \Mapping \JoinColumn ;
19
+ use Doctrine \ORM \Mapping \ManyToMany ;
20
+ use Doctrine \ORM \Mapping \ManyToOne ;
21
+ use Doctrine \ORM \Mapping \OneToMany ;
22
+ use Doctrine \ORM \Mapping \OneToOne ;
16
23
use PhpParser \Builder ;
17
24
use PhpParser \BuilderHelpers ;
18
25
use PhpParser \Comment \Doc ;
@@ -93,7 +100,7 @@ public function addEntityField(string $propertyName, array $columnOptions, array
93
100
$ attributes = [];
94
101
95
102
if ($ this ->useAttributesForDoctrineMapping ) {
96
- $ attributes [] = $ this ->buildAttributeNode (' ORM\Column ' , $ columnOptions );
103
+ $ attributes [] = $ this ->buildAttributeNode (new ClassNameDetails (Column::class, '\\' , null , ' ORM ' ) , $ columnOptions );
97
104
} else {
98
105
$ comments [] = $ this ->buildAnnotationLine ('@ORM\Column ' , $ columnOptions );
99
106
}
@@ -138,7 +145,7 @@ public function addEmbeddedEntity(string $propertyName, string $className): void
138
145
} else {
139
146
$ attributes = [
140
147
$ this ->buildAttributeNode (
141
- ' ORM \\ Embedded ' ,
148
+ new ClassNameDetails (Embedded::class, '\\' , null , ' ORM ' ) ,
142
149
[
143
150
'class ' => new ClassNameValue ($ className , $ typeHint ),
144
151
]
@@ -365,6 +372,17 @@ public function addProperty(string $name, array $annotationLines = [], $defaultV
365
372
$ this ->addNodeAfterProperties ($ newPropertyNode );
366
373
}
367
374
375
+ public function addAttributeToClass (ClassNameDetails $ attributeClass , array $ options ): void
376
+ {
377
+ $ this ->addUseStatementIfNecessary ($ attributeClass ->getFullName ());
378
+
379
+ $ classNode = $ this ->getClassNode ();
380
+
381
+ $ classNode ->attrGroups [] = new Node \AttributeGroup ([$ this ->buildAttributeNode ($ attributeClass , $ options )]);
382
+
383
+ $ this ->updateSourceCodeFromNewStmts ();
384
+ }
385
+
368
386
public function addAnnotationToClass (string $ annotationClass , array $ options ): void
369
387
{
370
388
$ annotationClassAlias = $ this ->addUseStatementIfNecessary ($ annotationClass );
@@ -537,9 +555,11 @@ private function addSingularRelation(BaseRelation $relation): void
537
555
),
538
556
];
539
557
} else {
558
+ $ relationClass = $ relation instanceof RelationManyToOne ? ManyToOne::class : OneToOne::class;
559
+
540
560
$ attributes = [
541
561
$ this ->buildAttributeNode (
542
- $ relation instanceof RelationManyToOne ? ' ORM \\ ManyToOne ' : 'ORM \\ OneToOne ' ,
562
+ new ClassNameDetails ( $ relationClass , '\\' , null , 'ORM ' ) ,
543
563
$ annotationOptions
544
564
),
545
565
];
@@ -551,7 +571,7 @@ private function addSingularRelation(BaseRelation $relation): void
551
571
'nullable ' => false ,
552
572
]);
553
573
} else {
554
- $ attributes [] = $ this ->buildAttributeNode (' ORM \\ JoinColumn ' , [
574
+ $ attributes [] = $ this ->buildAttributeNode (new ClassNameDetails (JoinColumn::class, '\\' , null , ' ORM ' ) , [
555
575
'nullable ' => false ,
556
576
]);
557
577
}
@@ -633,9 +653,11 @@ private function addCollectionRelation(BaseCollectionRelation $relation): void
633
653
),
634
654
];
635
655
} else {
656
+ $ relationClassName = $ relation instanceof RelationManyToMany ? ManyToMany::class : OneToMany::class;
657
+
636
658
$ attributes = [
637
659
$ this ->buildAttributeNode (
638
- $ relation instanceof RelationManyToMany ? ' ORM \\ ManyToMany ' : 'ORM \\ OneToMany ' ,
660
+ new ClassNameDetails ( $ relationClassName , '\\' , null , 'ORM ' ) ,
639
661
$ annotationOptions
640
662
),
641
663
];
@@ -1431,20 +1453,22 @@ private function buildNodeExprByValue($value): Node\Expr
1431
1453
/**
1432
1454
* builds an PHPParser attribute node.
1433
1455
*
1434
- * @param string $attributeClass the attribute class which should be used for the attribute
1456
+ * @param ClassNameDetails $attributeClass the attribute class which should be used for the attribute
1435
1457
* @param array $options the named arguments for the attribute ($key = argument name, $value = argument value)
1436
1458
*/
1437
- public function buildAttributeNode (string $ attributeClass , array $ options ): Node \Attribute
1459
+ public function buildAttributeNode (ClassNameDetails $ attributeClass , array $ options ): Node \Attribute
1438
1460
{
1439
- $ options = $ this ->sortOptionsByClassConstructorParameters ($ options , $ attributeClass );
1461
+ $ options = $ this ->sortOptionsByClassConstructorParameters ($ options , $ attributeClass-> getFullName () );
1440
1462
1441
1463
$ context = $ this ;
1442
1464
$ nodeArguments = array_map (static function ($ option , $ value ) use ($ context ) {
1443
1465
return new Node \Arg ($ context ->buildNodeExprByValue ($ value ), false , false , [], new Node \Identifier ($ option ));
1444
1466
}, array_keys ($ options ), array_values ($ options ));
1445
1467
1468
+ $ class = $ attributeClass ->hasSubstitutePrefix () ? $ attributeClass ->getShortNameWithSubstitutePrefix () : $ attributeClass ->getShortName ();
1469
+
1446
1470
return new Node \Attribute (
1447
- new Node \Name ($ attributeClass ),
1471
+ new Node \Name ($ class ),
1448
1472
$ nodeArguments
1449
1473
);
1450
1474
}
0 commit comments