Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
nawel-les-tilleuls committed Jul 22, 2022
1 parent a68a6d4 commit dc835c9
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 39 deletions.
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ parameters:
- src/Core/Upgrade/SubresourceTransformer.php
- src/Core/Upgrade/UpgradeApiResourceVisitor.php
- src/Core/Upgrade/UpgradeApiSubresourceVisitor.php
- src/Core/Upgrade/UpgradeApiFilterVisitor.php
- src/Core/Util/AttributesExtractor.php
- src/Core/Util/ErrorFormatGuesser.php
- src/Core/Util/Inflector.php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactoryInterface;
use ApiPlatform\Core\Upgrade\ColorConsoleDiffFormatter;
use ApiPlatform\Core\Upgrade\SubresourceTransformer;
use ApiPlatform\Core\Upgrade\UpgradeApiResourceVisitor;
use ApiPlatform\Core\Upgrade\UpgradeApiFilterVisitor;
use ApiPlatform\Core\Upgrade\UpgradeApiResourceVisitor;
use ApiPlatform\Core\Upgrade\UpgradeApiSubresourceVisitor;
use ApiPlatform\Exception\ResourceClassNotFoundException;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
Expand Down Expand Up @@ -107,8 +107,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$traverser = new NodeTraverser();
[$attribute, $isAnnotation] = $this->readApiResource($resourceClass);
$traverser->addVisitor(new UpgradeApiFilterVisitor($this->reader, $resourceClass, $attribute));

$traverser->addVisitor(new UpgradeApiFilterVisitor($this->reader, $resourceClass));

if (!$attribute) {
continue;
Expand Down
71 changes: 41 additions & 30 deletions src/Core/Upgrade/UpgradeApiFilterVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@
namespace ApiPlatform\Core\Upgrade;

use ApiPlatform\Core\Annotation\ApiFilter as LegacyApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter as LegacySearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter as LegacyExistsFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter as LegacyBooleanFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter as LegacyDateFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Doctrine\Orm\Filter\ExistsFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter as LegacyExistsFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\NumericFilter as LegacyNumericFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter as LegacyOrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\RangeFilter as LegacyRangeFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter as LegacySearchFilter;
use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
use ApiPlatform\Doctrine\Orm\Filter\ExistsFilter;
use ApiPlatform\Doctrine\Orm\Filter\NumericFilter;
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Doctrine\Orm\Filter\RangeFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\Resource\DeprecationMetadataTrait;
use Doctrine\Common\Annotations\AnnotationReader;
Expand Down Expand Up @@ -51,6 +59,10 @@ public function enterNode(Node $node)
SearchFilter::class,
ExistsFilter::class,
DateFilter::class,
BooleanFilter::class,
NumericFilter::class,
OrderFilter::class,
RangeFilter::class,
];

foreach ($node->stmts as $k => $stmt) {
Expand All @@ -76,9 +88,22 @@ public function enterNode(Node $node)
unset($node->stmts[$k]);
continue;
}



if (LegacyBooleanFilter::class === $useStatement) {
unset($node->stmts[$k]);
continue;
}
if (LegacyNumericFilter::class === $useStatement) {
unset($node->stmts[$k]);
continue;
}
if (LegacyOrderFilter::class === $useStatement) {
unset($node->stmts[$k]);
continue;
}
if (LegacyRangeFilter::class === $useStatement) {
unset($node->stmts[$k]);
continue;
}

if (false !== ($key = array_search($useStatement, $namespaces, true))) {
unset($namespaces[$key]);
Expand All @@ -96,16 +121,13 @@ public function enterNode(Node $node)
}
}

if ($node instanceof Node\Stmt\Property || $node instanceof Node\Stmt\Class_) {
if ($node instanceof Node\Stmt\Property || $node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Interface_) {
if ($node instanceof Node\Stmt\Property) {
$reflection = $this->reflectionClass->getProperty($node->props[0]->name->__toString());
} else {
$reflection = $this->reflectionClass;
}

// filter annotation : array
$filterAnnotations = $this->readApiFilters($reflection);

foreach ($this->readApiFilters($reflection) as $annotation) {
[$filterAnnotation, $isAnnotation] = $annotation;
if ($isAnnotation) {
Expand All @@ -122,19 +144,11 @@ public function enterNode(Node $node)
'properties',
'arguments',
] as $key) {
$value = $filterAnnotation->{$key};
if (null === $value || [] === $value) {
continue;
}
$arguments[$key] = $this->valueToNode($value);
$value = $filterAnnotation->{$key};

}
foreach ($filterAnnotation->attributes ?? [] as $key => $value) {
if (null === $value || [] === $value) {
if (!$value) {
continue;
}

[$key, $value] = $this->getKeyValue($key, $value);
$arguments[$key] = $this->valueToNode($value);
}

Expand All @@ -146,15 +160,14 @@ public function enterNode(Node $node)
]));
}
}

}

private function readApiFilters(\ReflectionProperty|\ReflectionClass $reflection): ?\Generator
private function readApiFilters(\ReflectionProperty|\ReflectionClass|\ReflectionInterface $reflection): ?\Generator
{
if (\PHP_VERSION_ID >= 80000 && $attributes = $reflection->getAttributes(LegacyApiFilter::class)) {
yield from array_map(function($attribute) {
return $attribute->newInstance();
} , $attributes);
yield from array_map(function ($attribute) {
return [$attribute->newInstance(), false];
}, $attributes);
}

if (null === $this->reader) {
Expand All @@ -165,7 +178,6 @@ private function readApiFilters(\ReflectionProperty|\ReflectionClass $reflection
$annotations = $this->reader->getPropertyAnnotations($reflection);
} else {
$annotations = $this->reader->getClassAnnotations($reflection);

}

foreach ($annotations as $annotation) {
Expand Down Expand Up @@ -236,14 +248,14 @@ private function arrayToArguments(array $arguments)
{
$args = [];
foreach ($arguments as $key => $value) {
if ($value)
if ($value) {
$args[] = new Node\Arg($value, false, false, [], new Node\Identifier($key));
}
}

return $args;
}


private function getShortName(string $class): string
{
if (false !== $pos = strrpos($class, '\\')) {
Expand All @@ -252,5 +264,4 @@ private function getShortName(string $class): string

return $class;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ public function testDebugResource()
"+#[ApiResource(graphQlOperations: [new Query(name: 'item_query'), new Mutation(name: 'update', normalizationContext: ['groups' => ['chicago', 'fakemanytomany']], denormalizationContext: ['groups' => ['friends']])], types: ['https://schema.org/Product'], normalizationContext: ['groups' => ['friends']], filters: ['related_dummy.friends', 'related_dummy.complex_sub_query'])]",
"#[ApiResource(uriTemplate: '/related_dummies/{id}/id.{_format}', uriVariables: ['id' => new Link(fromClass: self::class, identifiers: ['id'])], status: 200, types: ['https://schema.org/Product'], filters: ['related_dummy.friends', 'related_dummy.complex_sub_query'], normalizationContext: ['groups' => ['friends']], operations: [new Get()])]",
"+#[ApiFilter(filterClass: SearchFilter::class, properties: ['id', 'name'])]",
"+ #[ApiFilter(filterClass: SearchFilter::class)]",
"+ #[ApiFilter(filterClass: ExistsFilter::class)]",
"+ #[ApiFilter(filterClass: DateFilter::class)]",
'+ #[ApiFilter(filterClass: SearchFilter::class)]',
'+ #[ApiFilter(filterClass: ExistsFilter::class)]',
'+ #[ApiFilter(filterClass: DateFilter::class)]',
];

$display = $commandTester->getDisplay();
Expand Down
6 changes: 3 additions & 3 deletions tests/Fixtures/TestBundle/Entity/RelatedDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity;

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiSubresource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
Expand Down

0 comments on commit dc835c9

Please sign in to comment.