Skip to content

Commit cb6229e

Browse files
committed
add ability to add class attributes outside of orm namespace
1 parent a3740d7 commit cb6229e

File tree

3 files changed

+59
-27
lines changed

3 files changed

+59
-27
lines changed

src/Maker/MakeRegistrationForm.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
1515
use Doctrine\Common\Annotations\Annotation;
1616
use Doctrine\ORM\EntityManagerInterface;
17+
use Doctrine\ORM\Mapping\Column;
1718
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
1819
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
1920
use Symfony\Bundle\MakerBundle\ConsoleStyle;
@@ -371,13 +372,17 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
371372
);
372373
$userManipulator->setIo($io);
373374

374-
$userManipulator->addAnnotationToClass(
375-
UniqueEntity::class,
376-
[
377-
'fields' => [$usernameField],
378-
'message' => sprintf('There is already an account with this %s', $usernameField),
379-
]
380-
);
375+
if ($this->doctrineHelper->isDoctrineSupportingAttributes()) {
376+
$userManipulator->addAttributeToClass(new ClassNameDetails(UniqueEntity::class, '\\', null), ['fields' => [$usernameField], 'message' => sprintf('There is already an account with this %s', $usernameField)]);
377+
} else {
378+
$userManipulator->addAnnotationToClass(
379+
UniqueEntity::class,
380+
[
381+
'fields' => [$usernameField],
382+
'message' => sprintf('There is already an account with this %s', $usernameField),
383+
]
384+
);
385+
}
381386
$this->fileManager->dumpFile($classDetails->getPath(), $userManipulator->getSourceCode());
382387
}
383388

@@ -392,20 +397,11 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
392397
);
393398
$userManipulator->setIo($io);
394399

395-
// $annotation = [];
396-
// $attrNode = [];
397-
//
398-
// if ($usesAttributes) {
399-
// $attrNode = [$userManipulator->buildAttributeNode('ORM\Column', ['type' => 'boolean'])];
400-
// } else {
401-
// $annotation = ['@ORM\Column(type="boolean")'];
402-
// }
403-
404400
$userManipulator->addProperty(
405401
'isVerified',
406402
['@ORM\Column(type="boolean")'],
407403
false,
408-
[$userManipulator->buildAttributeNode('ORM\Column', ['type' => 'boolean'])]
404+
[$userManipulator->buildAttributeNode(new ClassNameDetails(Column::class, '\\', null, 'ORM'), ['type' => 'boolean'])]
409405
);
410406
$userManipulator->addAccessorMethod('isVerified', 'isVerified', 'bool', false);
411407
$userManipulator->addSetter('isVerified', 'bool', false);

src/Util/ClassNameDetails.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ final class ClassNameDetails
1818
private $fullClassName;
1919
private $namespacePrefix;
2020
private $suffix;
21+
private $substitutePrefix;
2122

22-
public function __construct(string $fullClassName, string $namespacePrefix, string $suffix = null)
23+
public function __construct(string $fullClassName, string $namespacePrefix, string $suffix = null, string $substitutePrefix = null)
2324
{
2425
$this->fullClassName = $fullClassName;
2526
$this->namespacePrefix = trim($namespacePrefix, '\\');
2627
$this->suffix = $suffix;
28+
$this->substitutePrefix = $substitutePrefix;
2729
}
2830

2931
public function getFullName(): string
@@ -52,4 +54,14 @@ public function getRelativeNameWithoutSuffix(): string
5254
{
5355
return Str::removeSuffix($this->getRelativeName(), $this->suffix);
5456
}
57+
58+
public function getShortNameWithSubstitutePrefix(): string
59+
{
60+
return sprintf('%s\\%s', $this->substitutePrefix, $this->getShortName());
61+
}
62+
63+
public function hasSubstitutePrefix(): bool
64+
{
65+
return null !== $this->substitutePrefix;
66+
}
5567
}

src/Util/ClassSourceManipulator.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313

1414
use Doctrine\Common\Collections\ArrayCollection;
1515
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;
1623
use PhpParser\Builder;
1724
use PhpParser\BuilderHelpers;
1825
use PhpParser\Comment\Doc;
@@ -93,7 +100,7 @@ public function addEntityField(string $propertyName, array $columnOptions, array
93100
$attributes = [];
94101

95102
if ($this->useAttributesForDoctrineMapping) {
96-
$attributes[] = $this->buildAttributeNode('ORM\Column', $columnOptions);
103+
$attributes[] = $this->buildAttributeNode(new ClassNameDetails(Column::class, '\\', null, 'ORM'), $columnOptions);
97104
} else {
98105
$comments[] = $this->buildAnnotationLine('@ORM\Column', $columnOptions);
99106
}
@@ -138,7 +145,7 @@ public function addEmbeddedEntity(string $propertyName, string $className): void
138145
} else {
139146
$attributes = [
140147
$this->buildAttributeNode(
141-
'ORM\\Embedded',
148+
new ClassNameDetails(Embedded::class, '\\', null, 'ORM'),
142149
[
143150
'class' => new ClassNameValue($className, $typeHint),
144151
]
@@ -365,6 +372,17 @@ public function addProperty(string $name, array $annotationLines = [], $defaultV
365372
$this->addNodeAfterProperties($newPropertyNode);
366373
}
367374

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+
368386
public function addAnnotationToClass(string $annotationClass, array $options): void
369387
{
370388
$annotationClassAlias = $this->addUseStatementIfNecessary($annotationClass);
@@ -537,9 +555,11 @@ private function addSingularRelation(BaseRelation $relation): void
537555
),
538556
];
539557
} else {
558+
$relationClass = $relation instanceof RelationManyToOne ? ManyToOne::class : OneToOne::class;
559+
540560
$attributes = [
541561
$this->buildAttributeNode(
542-
$relation instanceof RelationManyToOne ? 'ORM\\ManyToOne' : 'ORM\\OneToOne',
562+
new ClassNameDetails($relationClass, '\\', null, 'ORM'),
543563
$annotationOptions
544564
),
545565
];
@@ -551,7 +571,7 @@ private function addSingularRelation(BaseRelation $relation): void
551571
'nullable' => false,
552572
]);
553573
} else {
554-
$attributes[] = $this->buildAttributeNode('ORM\\JoinColumn', [
574+
$attributes[] = $this->buildAttributeNode(new ClassNameDetails(JoinColumn::class, '\\', null, 'ORM'), [
555575
'nullable' => false,
556576
]);
557577
}
@@ -633,9 +653,11 @@ private function addCollectionRelation(BaseCollectionRelation $relation): void
633653
),
634654
];
635655
} else {
656+
$relationClassName = $relation instanceof RelationManyToMany ? ManyToMany::class : OneToMany::class;
657+
636658
$attributes = [
637659
$this->buildAttributeNode(
638-
$relation instanceof RelationManyToMany ? 'ORM\\ManyToMany' : 'ORM\\OneToMany',
660+
new ClassNameDetails($relationClassName, '\\', null, 'ORM'),
639661
$annotationOptions
640662
),
641663
];
@@ -1431,20 +1453,22 @@ private function buildNodeExprByValue($value): Node\Expr
14311453
/**
14321454
* builds an PHPParser attribute node.
14331455
*
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
14351457
* @param array $options the named arguments for the attribute ($key = argument name, $value = argument value)
14361458
*/
1437-
public function buildAttributeNode(string $attributeClass, array $options): Node\Attribute
1459+
public function buildAttributeNode(ClassNameDetails $attributeClass, array $options): Node\Attribute
14381460
{
1439-
$options = $this->sortOptionsByClassConstructorParameters($options, $attributeClass);
1461+
$options = $this->sortOptionsByClassConstructorParameters($options, $attributeClass->getFullName());
14401462

14411463
$context = $this;
14421464
$nodeArguments = array_map(static function ($option, $value) use ($context) {
14431465
return new Node\Arg($context->buildNodeExprByValue($value), false, false, [], new Node\Identifier($option));
14441466
}, array_keys($options), array_values($options));
14451467

1468+
$class = $attributeClass->hasSubstitutePrefix() ? $attributeClass->getShortNameWithSubstitutePrefix() : $attributeClass->getShortName();
1469+
14461470
return new Node\Attribute(
1447-
new Node\Name($attributeClass),
1471+
new Node\Name($class),
14481472
$nodeArguments
14491473
);
14501474
}

0 commit comments

Comments
 (0)