Skip to content

Commit

Permalink
refactor(EntityGenerator): Simplify namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Oct 10, 2024
1 parent 0369b3a commit ca4c092
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 100 deletions.
31 changes: 18 additions & 13 deletions src/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\Literal;
use Nette\PhpGenerator\PhpFile;
use Nette\PhpGenerator\PhpNamespace;
use Nette\PhpGenerator\PsrPrinter;
use RZ\Roadiz\Contracts\NodeType\NodeTypeFieldInterface;
use RZ\Roadiz\Contracts\NodeType\NodeTypeInterface;
Expand Down Expand Up @@ -161,15 +162,19 @@ public function getClassContent(): string

$namespace = $file
->addNamespace(trim($this->options['namespace'], '\\'))
->addUse('ApiPlatform\Metadata\ApiFilter')
->addUse('ApiPlatform\Metadata\ApiProperty')
->addUse('ApiPlatform\Serializer\Filter\PropertyFilter')
->addUse('ApiPlatform\Doctrine\Orm\Filter', 'Filter')
->addUse('Doctrine\Common\Collections\Collection')
->addUse($this->options['parent_class'])
->addUse('Doctrine\ORM\Mapping', 'ORM')
->addUse('JMS\Serializer\Annotation', 'JMS')
->addUse('Symfony\Component\Serializer\Attribute', 'Serializer')
->addUse('Gedmo\Mapping\Annotation', 'Gedmo')
->addUse('ApiPlatform\Metadata\ApiFilter')
->addUse('JMS\Serializer\Annotation', 'JMS')
->addUse('RZ\Roadiz\CoreBundle\Entity\Node')
->addUse('RZ\Roadiz\CoreBundle\Entity\Translation')
->addUse('ApiPlatform\Metadata\ApiProperty')
->addUse('Doctrine\Common\Collections\Collection')
->addUse('RZ\Roadiz\CoreBundle\Entity\UserLogEntry')
->addUse('Symfony\Component\Serializer\Attribute', 'Serializer')
;

$classType = $namespace->addClass($this->nodeType->getSourceEntityClassName())
Expand All @@ -178,25 +183,25 @@ public function getClassContent(): string
->addComment($this->nodeType->getDescription() ?? '');

$this
->addClassAttributes($classType)
->addClassFields($classType)
->addClassAttributes($classType, $namespace)
->addClassFields($classType, $namespace)
->addClassConstructor($classType)
->addClassCloneMethod($classType)
->addClassMethods($classType)
;
return (new PsrPrinter())->printFile($file);
}

private function addClassAttributes(ClassType $classType): self
private function addClassAttributes(ClassType $classType, PhpNamespace $namespace): self
{
$classType
->addAttribute(
'Gedmo\Mapping\Annotation\Loggable',
['logEntryClass' => new Literal('\RZ\Roadiz\CoreBundle\Entity\UserLogEntry::class')]
['logEntryClass' => new Literal('UserLogEntry::class')]
)
->addAttribute(
'Doctrine\ORM\Mapping\Entity',
['repositoryClass' => new Literal($this->options['repository_class'] . '::class')]
['repositoryClass' => new Literal($namespace->simplifyName($this->options['repository_class']) . '::class')]
)
->addAttribute(
'Doctrine\ORM\Mapping\Table',
Expand All @@ -211,18 +216,18 @@ private function addClassAttributes(ClassType $classType): self
if ($this->options['use_api_platform_filters'] === true) {
$classType->addAttribute(
'ApiPlatform\Metadata\ApiFilter',
[new Literal('\ApiPlatform\Serializer\Filter\PropertyFilter::class')]
[new Literal($namespace->simplifyName('\ApiPlatform\Serializer\Filter\PropertyFilter') . '::class')]
);
}

return $this;
}


private function addClassFields(ClassType $classType): self
private function addClassFields(ClassType $classType, PhpNamespace $namespace): self
{
foreach ($this->fieldGenerators as $fieldGenerator) {
$fieldGenerator->addField($classType);
$fieldGenerator->addField($classType, $namespace);
}
return $this;
}
Expand Down
35 changes: 18 additions & 17 deletions src/Field/AbstractFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\Literal;
use Nette\PhpGenerator\Method;
use Nette\PhpGenerator\PhpNamespace;
use Nette\PhpGenerator\Property;
use RZ\Roadiz\Contracts\NodeType\NodeTypeFieldInterface;
use RZ\Roadiz\Contracts\NodeType\SerializableInterface;
Expand All @@ -24,14 +25,14 @@ public function __construct(
/**
* Generate PHP code for current doctrine field.
*/
public function addField(ClassType $classType): void
public function addField(ClassType $classType, PhpNamespace $namespace): void
{
$property = $this->getFieldProperty($classType);

$this
->addFieldAnnotation($property)
->addFieldAttributes($property, $this->isExcludingFieldFromJmsSerialization())
->addFieldGetter($classType)
->addFieldAttributes($property, $namespace, $this->isExcludingFieldFromJmsSerialization())
->addFieldGetter($classType, $namespace)
->addFieldAlternativeGetter($classType)
->addFieldSetter($classType)
;
Expand Down Expand Up @@ -89,7 +90,7 @@ protected function getFieldDefaultValueDeclaration(): Literal|string|null
return null;
}

protected function addFieldAttributes(Property $property, bool $exclude = false): self
protected function addFieldAttributes(Property $property, PhpNamespace $namespace, bool $exclude = false): self
{
if ($exclude) {
$property->addAttribute('JMS\Serializer\Annotation\Exclude');
Expand Down Expand Up @@ -139,56 +140,56 @@ protected function addFieldAttributes(Property $property, bool $exclude = false)
switch (true) {
case $this->field->isString():
$property->addAttribute('ApiPlatform\Metadata\ApiFilter', [
0 => new Literal('\ApiPlatform\Doctrine\Orm\Filter\SearchFilter::class'),
0 => new Literal($namespace->simplifyName('\ApiPlatform\Doctrine\Orm\Filter\SearchFilter') . '::class'),
'strategy' => 'partial'
]);
$property->addAttribute('ApiPlatform\Metadata\ApiFilter', [
new Literal('\RZ\Roadiz\CoreBundle\Api\Filter\NotFilter::class')
new Literal($namespace->simplifyName('\RZ\Roadiz\CoreBundle\Api\Filter\NotFilter') . '::class')
]);
break;
case $this->field->isMultiple():
case $this->field->isEnum():
$property->addAttribute('ApiPlatform\Metadata\ApiFilter', [
0 => new Literal('\ApiPlatform\Doctrine\Orm\Filter\SearchFilter::class'),
0 => new Literal($namespace->simplifyName('\ApiPlatform\Doctrine\Orm\Filter\SearchFilter') . '::class'),
'strategy' => 'exact'
]);
$property->addAttribute('ApiPlatform\Metadata\ApiFilter', [
new Literal('\RZ\Roadiz\CoreBundle\Api\Filter\NotFilter::class')
new Literal($namespace->simplifyName('\RZ\Roadiz\CoreBundle\Api\Filter\NotFilter') . '::class')
]);
break;
case $this->field->isBool():
$property->addAttribute('ApiPlatform\Metadata\ApiFilter', [
new Literal('\ApiPlatform\Doctrine\Orm\Filter\OrderFilter::class'),
new Literal($namespace->simplifyName('\ApiPlatform\Doctrine\Orm\Filter\OrderFilter') . '::class'),
]);
$property->addAttribute('ApiPlatform\Metadata\ApiFilter', [
new Literal('\ApiPlatform\Doctrine\Orm\Filter\BooleanFilter::class'),
new Literal($namespace->simplifyName('\ApiPlatform\Doctrine\Orm\Filter\BooleanFilter') . '::class'),
]);
break;
case $this->field->isManyToOne():
case $this->field->isManyToMany():
$property->addAttribute('ApiPlatform\Metadata\ApiFilter', [
new Literal('\ApiPlatform\Doctrine\Orm\Filter\ExistsFilter::class'),
new Literal($namespace->simplifyName('\ApiPlatform\Doctrine\Orm\Filter\ExistsFilter') . '::class'),
]);
break;
case $this->field->isInteger():
case $this->field->isDecimal():
$property->addAttribute('ApiPlatform\Metadata\ApiFilter', [
new Literal('\ApiPlatform\Doctrine\Orm\Filter\OrderFilter::class'),
new Literal($namespace->simplifyName('\ApiPlatform\Doctrine\Orm\Filter\OrderFilter') . '::class'),
]);
$property->addAttribute('ApiPlatform\Metadata\ApiFilter', [
new Literal('\ApiPlatform\Doctrine\Orm\Filter\NumericFilter::class'),
new Literal($namespace->simplifyName('\ApiPlatform\Doctrine\Orm\Filter\NumericFilter') . '::class'),
]);
$property->addAttribute('ApiPlatform\Metadata\ApiFilter', [
new Literal('\ApiPlatform\Doctrine\Orm\Filter\RangeFilter::class'),
new Literal($namespace->simplifyName('\ApiPlatform\Doctrine\Orm\Filter\RangeFilter') . '::class'),
]);
break;
case $this->field->isDate():
case $this->field->isDateTime():
$property->addAttribute('ApiPlatform\Metadata\ApiFilter', [
new Literal('\ApiPlatform\Doctrine\Orm\Filter\OrderFilter::class'),
new Literal($namespace->simplifyName('\ApiPlatform\Doctrine\Orm\Filter\OrderFilter') . '::class'),
]);
$property->addAttribute('ApiPlatform\Metadata\ApiFilter', [
new Literal('\ApiPlatform\Doctrine\Orm\Filter\DateFilter::class'),
new Literal($namespace->simplifyName('\ApiPlatform\Doctrine\Orm\Filter\DateFilter') . '::class'),
]);
break;
}
Expand All @@ -200,7 +201,7 @@ protected function addFieldAttributes(Property $property, bool $exclude = false)
/**
* Generate PHP alternative getter method block.
*/
abstract protected function addFieldGetter(ClassType $classType): self;
abstract protected function addFieldGetter(ClassType $classType, PhpNamespace $namespace): self;

/**
* Generate PHP alternative getter method block.
Expand Down
5 changes: 3 additions & 2 deletions src/Field/CustomFormsFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\Literal;
use Nette\PhpGenerator\Method;
use Nette\PhpGenerator\PhpNamespace;
use Nette\PhpGenerator\Property;

final class CustomFormsFieldGenerator extends AbstractFieldGenerator
Expand Down Expand Up @@ -39,7 +40,7 @@ protected function getFieldDefaultValueDeclaration(): Literal|string|null
return new Literal('null');
}

public function addFieldGetter(ClassType $classType): self
public function addFieldGetter(ClassType $classType, PhpNamespace $namespace): self
{
$method = $classType
->addMethod($this->field->getGetterName())
Expand All @@ -53,7 +54,7 @@ public function addFieldGetter(ClassType $classType): self
if (null === \$this->{$this->field->getVarName()}) {
if (null !== \$this->objectManager) {
\$this->{$this->field->getVarName()} = \$this->objectManager
->getRepository({$this->options['custom_form_class']}::class)
->getRepository({$namespace->simplifyName($this->options['custom_form_class'])}::class)
->findByNodeAndFieldName(
\$this->getNode(),
'{$this->field->getName()}'
Expand Down
5 changes: 3 additions & 2 deletions src/Field/DocumentsFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\Literal;
use Nette\PhpGenerator\Method;
use Nette\PhpGenerator\PhpNamespace;
use Nette\PhpGenerator\Property;
use Symfony\Component\String\UnicodeString;

Expand Down Expand Up @@ -45,7 +46,7 @@ protected function getFieldDefaultValueDeclaration(): Literal|string|null
return new Literal('null');
}

public function addFieldGetter(ClassType $classType): self
public function addFieldGetter(ClassType $classType, PhpNamespace $namespace): self
{
$getter = $classType->addMethod($this->field->getGetterName())
->setReturnType('array')
Expand All @@ -55,7 +56,7 @@ public function addFieldGetter(ClassType $classType): self
if (null === \$this->{$this->field->getVarName()}) {
if (null !== \$this->objectManager) {
\$this->{$this->field->getVarName()} = \$this->objectManager
->getRepository({$this->options['document_class']}::class)
->getRepository({$namespace->simplifyName($this->options['document_class'])}::class)
->findByNodeSourceAndFieldName(
\$this,
'{$this->field->getName()}'
Expand Down
16 changes: 11 additions & 5 deletions src/Field/ManyToManyFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\Literal;
use Nette\PhpGenerator\PhpNamespace;
use Nette\PhpGenerator\Property;
use Symfony\Component\String\UnicodeString;

Expand All @@ -20,9 +21,9 @@ protected function getFieldProperty(ClassType $classType): Property
}


protected function addFieldAttributes(Property $property, bool $exclude = false): self
protected function addFieldAttributes(Property $property, PhpNamespace $namespace, bool $exclude = false): self
{
parent::addFieldAttributes($property, $exclude);
parent::addFieldAttributes($property, $namespace, $exclude);

/*
* Many Users have Many Groups.
Expand Down Expand Up @@ -73,7 +74,7 @@ protected function addFieldAttributes(Property $property, bool $exclude = false)

if ($this->options['use_api_platform_filters'] === true) {
$property->addAttribute('ApiPlatform\Metadata\ApiFilter', [
0 => new Literal('\ApiPlatform\Doctrine\Orm\Filter\SearchFilter::class'),
0 => new Literal($namespace->simplifyName('\ApiPlatform\Doctrine\Orm\Filter\SearchFilter') . '::class'),
'strategy' => 'exact'
]);
}
Expand All @@ -97,13 +98,18 @@ protected function getFieldTypeDeclaration(): string
return '\Doctrine\Common\Collections\Collection';
}

public function addFieldGetter(ClassType $classType): self
public function addFieldGetter(ClassType $classType, PhpNamespace $namespace): self
{
$classType->addMethod($this->field->getGetterName())
->setReturnType('\Doctrine\Common\Collections\Collection')
->setPublic()
->setBody('return $this->' . $this->field->getVarName() . ';')
->addComment('@return \Doctrine\Common\Collections\Collection<int, ' . $this->getFullyQualifiedClassName() . '>');
->addComment(
'@return ' .
$namespace->simplifyName('\Doctrine\Common\Collections\Collection') .
'<int, ' . $this->getFullyQualifiedClassName() .
'>'
);
return $this;
}

Expand Down
9 changes: 5 additions & 4 deletions src/Field/ManyToOneFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\Literal;
use Nette\PhpGenerator\PhpNamespace;
use Nette\PhpGenerator\Property;

final class ManyToOneFieldGenerator extends AbstractConfigurableFieldGenerator
{
protected function addFieldAttributes(Property $property, bool $exclude = false): self
protected function addFieldAttributes(Property $property, PhpNamespace $namespace, bool $exclude = false): self
{
parent::addFieldAttributes($property, $exclude);
parent::addFieldAttributes($property, $namespace, $exclude);

/*
* Many Users have One Address.
Expand All @@ -31,7 +32,7 @@ protected function addFieldAttributes(Property $property, bool $exclude = false)

if ($this->options['use_api_platform_filters'] === true) {
$property->addAttribute('ApiPlatform\Metadata\ApiFilter', [
0 => new Literal('\ApiPlatform\Doctrine\Orm\Filter\SearchFilter::class'),
0 => new Literal($namespace->simplifyName('\ApiPlatform\Doctrine\Orm\Filter\SearchFilter') . '::class'),
'strategy' => 'exact'
]);
}
Expand Down Expand Up @@ -61,7 +62,7 @@ protected function getFieldDefaultValueDeclaration(): Literal|string|null
return new Literal('null');
}

public function addFieldGetter(ClassType $classType): self
public function addFieldGetter(ClassType $classType, PhpNamespace $namespace): self
{
$classType->addMethod($this->field->getGetterName())
->setReturnType($this->getFieldTypeDeclaration())
Expand Down
10 changes: 5 additions & 5 deletions src/Field/NodesFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace RZ\Roadiz\EntityGenerator\Field;

use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\Literal;
use Nette\PhpGenerator\Method;
use Nette\PhpGenerator\PhpNamespace;
use Nette\PhpGenerator\Property;
use RZ\Roadiz\Contracts\NodeType\NodeTypeFieldInterface;
use RZ\Roadiz\Contracts\NodeType\NodeTypeResolverInterface;
Expand All @@ -23,9 +23,9 @@ public function __construct(
parent::__construct($field, $defaultValuesResolver, $options);
}

public function addField(ClassType $classType): void
public function addField(ClassType $classType, PhpNamespace $namespace): void
{
$this->addFieldGetter($classType);
$this->addFieldGetter($classType, $namespace);
$this->addFieldSetter($classType);
}

Expand Down Expand Up @@ -89,7 +89,7 @@ protected function getRepositoryClass(): string
return $this->options['parent_class'];
}

public function addFieldGetter(ClassType $classType): self
public function addFieldGetter(ClassType $classType, PhpNamespace $namespace): self
{
$property = $classType->addProperty($this->getFieldSourcesName())
->setType('?array')
Expand All @@ -99,7 +99,7 @@ public function addFieldGetter(ClassType $classType): self
->addComment('@var ' . $this->getRepositoryClass() . '[]|null');

$this->addFieldAutodoc($property);
$this->addFieldAttributes($property, $this->isExcludingFieldFromJmsSerialization());
$this->addFieldAttributes($property, $namespace, $this->isExcludingFieldFromJmsSerialization());


$getter = $classType->addMethod($this->field->getGetterName() . 'Sources')
Expand Down
Loading

0 comments on commit ca4c092

Please sign in to comment.