Skip to content

refactor(state): state options code duplication #7109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\State\Util\StateOptionsTrait;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\Persistence\ManagerRegistry;

final class DoctrineMongoDbOdmResourceCollectionMetadataFactory implements ResourceMetadataCollectionFactoryInterface
{
use StateOptionsTrait;

public function __construct(private readonly ManagerRegistry $managerRegistry, private readonly ResourceMetadataCollectionFactoryInterface $decorated)
{
}
Expand All @@ -45,11 +48,7 @@ public function create(string $resourceClass): ResourceMetadataCollection
if ($operations) {
/** @var Operation $operation */
foreach ($resourceMetadata->getOperations() as $operationName => $operation) {
$documentClass = $operation->getClass();
if (($options = $operation->getStateOptions()) && $options instanceof Options && $options->getDocumentClass()) {
$documentClass = $options->getDocumentClass();
}

$documentClass = $this->getStateOptionsClass($operation, $operation->getClass(), Options::class);
if (!$this->managerRegistry->getManagerForClass($documentClass) instanceof DocumentManager) {
continue;
}
Expand Down
7 changes: 3 additions & 4 deletions src/Doctrine/Odm/State/CollectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\State\ProviderInterface;
use ApiPlatform\State\Util\StateOptionsTrait;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Doctrine\Persistence\ManagerRegistry;
Expand All @@ -32,6 +33,7 @@ final class CollectionProvider implements ProviderInterface
{
use LinksHandlerLocatorTrait;
use LinksHandlerTrait;
use StateOptionsTrait;

/**
* @param AggregationCollectionExtensionInterface[] $collectionExtensions
Expand All @@ -45,10 +47,7 @@ public function __construct(ResourceMetadataCollectionFactoryInterface $resource

public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable
{
$documentClass = $operation->getClass();
if (($options = $operation->getStateOptions()) && $options instanceof Options && $options->getDocumentClass()) {
$documentClass = $options->getDocumentClass();
}
$documentClass = $this->getStateOptionsClass($operation, $operation->getClass(), Options::class);

/** @var DocumentManager $manager */
$manager = $this->managerRegistry->getManagerForClass($documentClass);
Expand Down
7 changes: 3 additions & 4 deletions src/Doctrine/Odm/State/ItemProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\State\ProviderInterface;
use ApiPlatform\State\Util\StateOptionsTrait;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Doctrine\Persistence\ManagerRegistry;
Expand All @@ -35,6 +36,7 @@ final class ItemProvider implements ProviderInterface
{
use LinksHandlerLocatorTrait;
use LinksHandlerTrait;
use StateOptionsTrait;

/**
* @param AggregationItemExtensionInterface[] $itemExtensions
Expand All @@ -48,10 +50,7 @@ public function __construct(ResourceMetadataCollectionFactoryInterface $resource

public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object
{
$documentClass = $operation->getClass();
if (($options = $operation->getStateOptions()) && $options instanceof Options && $options->getDocumentClass()) {
$documentClass = $options->getDocumentClass();
}
$documentClass = $this->getStateOptionsClass($operation, $operation->getClass(), Options::class);

/** @var DocumentManager $manager */
$manager = $this->managerRegistry->getManagerForClass($documentClass);
Expand Down
16 changes: 5 additions & 11 deletions src/Doctrine/Odm/State/LinksHandlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ApiPlatform\Metadata\Exception\RuntimeException;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\Util\StateOptionsTrait;
use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
Expand All @@ -28,6 +29,7 @@
trait LinksHandlerTrait
{
use CommonLinksHandlerTrait;
use StateOptionsTrait;

private ManagerRegistry $managerRegistry;

Expand Down Expand Up @@ -142,26 +144,18 @@ private function buildAggregation(string $toClass, array $links, array $identifi

private function getLinkFromClass(Link $link, Operation $operation): string
{
$documentClass = $this->getStateOptionsClass($operation, $operation->getClass(), Options::class);
$fromClass = $link->getFromClass();
if ($fromClass === $operation->getClass() && $documentClass = $this->getStateOptionsDocumentClass($operation)) {
if ($fromClass === $operation->getClass() && $documentClass) {
return $documentClass;
}

$operation = $this->resourceMetadataCollectionFactory->create($fromClass)->getOperation();

if ($documentClass = $this->getStateOptionsDocumentClass($operation)) {
if ($documentClass = $this->getStateOptionsClass($operation, null, Options::class)) {
return $documentClass;
}

throw new \Exception('Can not found a doctrine class for this link.');
}

private function getStateOptionsDocumentClass(Operation $operation): ?string
{
if (($options = $operation->getStateOptions()) && $options instanceof Options && $documentClass = $options->getDocumentClass()) {
return $documentClass;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\State\Util\StateOptionsTrait;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;

final class DoctrineOrmResourceCollectionMetadataFactory implements ResourceMetadataCollectionFactoryInterface
{
use StateOptionsTrait;

public function __construct(private readonly ManagerRegistry $managerRegistry, private readonly ResourceMetadataCollectionFactoryInterface $decorated)
{
}
Expand All @@ -45,10 +48,7 @@ public function create(string $resourceClass): ResourceMetadataCollection
if ($operations) {
/** @var Operation $operation */
foreach ($resourceMetadata->getOperations() as $operationName => $operation) {
$entityClass = $operation->getClass();
if (($options = $operation->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
$entityClass = $options->getEntityClass();
}
$entityClass = $this->getStateOptionsClass($operation, $operation->getClass(), Options::class);

if (!$this->managerRegistry->getManagerForClass($entityClass) instanceof EntityManagerInterface) {
continue;
Expand All @@ -64,10 +64,7 @@ public function create(string $resourceClass): ResourceMetadataCollection

if ($graphQlOperations) {
foreach ($graphQlOperations as $operationName => $graphQlOperation) {
$entityClass = $graphQlOperation->getClass();
if (($options = $graphQlOperation->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
$entityClass = $options->getEntityClass();
}
$entityClass = $this->getStateOptionsClass($graphQlOperation, $graphQlOperation->getClass(), Options::class);

if (!$this->managerRegistry->getManagerForClass($entityClass) instanceof EntityManagerInterface) {
continue;
Expand Down
7 changes: 3 additions & 4 deletions src/Doctrine/Orm/State/CollectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\State\ProviderInterface;
use ApiPlatform\State\Util\StateOptionsTrait;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Container\ContainerInterface;
Expand All @@ -35,6 +36,7 @@ final class CollectionProvider implements ProviderInterface
{
use LinksHandlerLocatorTrait;
use LinksHandlerTrait;
use StateOptionsTrait;

/**
* @param QueryCollectionExtensionInterface[] $collectionExtensions
Expand All @@ -48,10 +50,7 @@ public function __construct(ResourceMetadataCollectionFactoryInterface $resource

public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable
{
$entityClass = $operation->getClass();
if (($options = $operation->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
$entityClass = $options->getEntityClass();
}
$entityClass = $this->getStateOptionsClass($operation, $operation->getClass(), Options::class);

/** @var EntityManagerInterface $manager */
$manager = $this->managerRegistry->getManagerForClass($entityClass);
Expand Down
7 changes: 3 additions & 4 deletions src/Doctrine/Orm/State/ItemProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\State\ProviderInterface;
use ApiPlatform\State\Util\StateOptionsTrait;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Container\ContainerInterface;
Expand All @@ -35,6 +36,7 @@ final class ItemProvider implements ProviderInterface
{
use LinksHandlerLocatorTrait;
use LinksHandlerTrait;
use StateOptionsTrait;

/**
* @param QueryItemExtensionInterface[] $itemExtensions
Expand All @@ -48,10 +50,7 @@ public function __construct(ResourceMetadataCollectionFactoryInterface $resource

public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object
{
$entityClass = $operation->getClass();
if (($options = $operation->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
$entityClass = $options->getEntityClass();
}
$entityClass = $this->getStateOptionsClass($operation, $operation->getClass(), Options::class);

/** @var EntityManagerInterface|null $manager */
$manager = $this->managerRegistry->getManagerForClass($entityClass);
Expand Down
19 changes: 4 additions & 15 deletions src/Doctrine/Orm/State/LinksHandlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\Util\StateOptionsTrait;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
Expand All @@ -27,6 +28,7 @@
trait LinksHandlerTrait
{
use CommonLinksHandlerTrait;
use StateOptionsTrait;

private ManagerRegistry $managerRegistry;

Expand Down Expand Up @@ -159,25 +161,12 @@ private function handleLinks(QueryBuilder $queryBuilder, array $identifiers, Que
private function getLinkFromClass(Link $link, Operation $operation): string
{
$fromClass = $link->getFromClass();
if ($fromClass === $operation->getClass() && $entityClass = $this->getStateOptionsEntityClass($operation)) {
if ($fromClass === $operation->getClass() && $entityClass = $this->getStateOptionsClass($operation, $operation->getClass(), Options::class)) {
return $entityClass;
}

$operation = $this->resourceMetadataCollectionFactory->create($fromClass)->getOperation();

if ($entityClass = $this->getStateOptionsEntityClass($operation)) {
return $entityClass;
}

throw new \Exception('Can not found a doctrine class for this link.');
}

private function getStateOptionsEntityClass(Operation $operation): ?string
{
if (($options = $operation->getStateOptions()) && $options instanceof Options && $entityClass = $options->getEntityClass()) {
return $entityClass;
}

return null;
return $this->getStateOptionsClass($operation, $operation->getClass(), Options::class);
}
}
15 changes: 1 addition & 14 deletions src/GraphQl/Type/FieldsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace ApiPlatform\GraphQl\Type;

use ApiPlatform\Doctrine\Odm\State\Options as ODMOptions;
use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\GraphQl\Exception\InvalidTypeException;
use ApiPlatform\GraphQl\Resolver\Factory\ResolverFactoryInterface;
use ApiPlatform\GraphQl\Type\Definition\TypeInterface;
Expand Down Expand Up @@ -563,18 +561,7 @@ private function getFilterArgs(array $args, ?string $resourceClass, string $root
continue;
}

$entityClass = $resourceClass;
if ($options = $resourceOperation->getStateOptions()) {
if (class_exists(Options::class) && $options instanceof Options && $options->getEntityClass()) {
$entityClass = $options->getEntityClass();
}

if (class_exists(ODMOptions::class) && $options instanceof ODMOptions && $options->getDocumentClass()) {
$entityClass = $options->getDocumentClass();
}
}

foreach ($this->filterLocator->get($filterId)->getDescription($entityClass) as $key => $description) {
foreach ($this->filterLocator->get($filterId)->getDescription($resourceClass) as $key => $description) {
$nullable = isset($description['required']) ? !$description['required'] : true;
$filterType = \in_array($description['type'], Type::$builtinTypes, true) ? new Type($description['type'], $nullable) : new Type('object', $nullable, $description['type']);
$graphqlFilterType = $this->convertType($filterType, false, $resourceOperation, $rootOperation, $resourceClass, $rootResource, $property, $depth);
Expand Down
14 changes: 3 additions & 11 deletions src/Hydra/Serializer/CollectionFiltersNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@

namespace ApiPlatform\Hydra\Serializer;

use ApiPlatform\Doctrine\Odm\State\Options as ODMOptions;
use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\JsonLd\Serializer\HydraPrefixTrait;
use ApiPlatform\Metadata\FilterInterface;
use ApiPlatform\Metadata\Parameters;
use ApiPlatform\Metadata\QueryParameterInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\State\Util\StateOptionsTrait;
use Psr\Container\ContainerInterface;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
Expand All @@ -35,6 +34,7 @@
final class CollectionFiltersNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use HydraPrefixTrait;
use StateOptionsTrait;
private ?ContainerInterface $filterLocator = null;

/**
Expand Down Expand Up @@ -101,15 +101,7 @@ public function normalize(mixed $object, ?string $format = null, array $context
}
}

if ($options = $operation->getStateOptions()) {
if ($options instanceof Options && $options->getEntityClass()) {
$resourceClass = $options->getEntityClass();
}

if ($options instanceof ODMOptions && $options->getDocumentClass()) {
$resourceClass = $options->getDocumentClass();
}
}
$resourceClass = $this->getStateOptionsClass($operation, $resourceClass);

if ($currentFilters || ($parameters && \count($parameters))) {
$hydraPrefix = $this->getHydraPrefix($context + $this->defaultContext);
Expand Down
5 changes: 3 additions & 2 deletions src/Laravel/Eloquent/State/CollectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\Pagination\Pagination;
use ApiPlatform\State\ProviderInterface;
use ApiPlatform\State\Util\StateOptionsTrait;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Psr\Container\ContainerInterface;
Expand All @@ -30,7 +31,7 @@
final class CollectionProvider implements ProviderInterface
{
use LinksHandlerLocatorTrait;
use ModelClassTrait;
use StateOptionsTrait;

/**
* @param LinksHandlerInterface<Model> $linksHandler
Expand All @@ -47,7 +48,7 @@ public function __construct(

public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$resourceClass = $this->getModelClass($operation);
$resourceClass = $this->getStateOptionsClass($operation, $operation->getClass(), Options::class);
$model = new $resourceClass();

if (!$model instanceof Model) {
Expand Down
5 changes: 3 additions & 2 deletions src/Laravel/Eloquent/State/ItemProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ApiPlatform\Metadata\Exception\RuntimeException;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use ApiPlatform\State\Util\StateOptionsTrait;
use Illuminate\Database\Eloquent\Model;
use Psr\Container\ContainerInterface;

Expand All @@ -26,7 +27,7 @@
final class ItemProvider implements ProviderInterface
{
use LinksHandlerLocatorTrait;
use ModelClassTrait;
use StateOptionsTrait;

/**
* @param LinksHandlerInterface<Model> $linksHandler
Expand All @@ -42,7 +43,7 @@ public function __construct(

public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$resourceClass = $this->getModelClass($operation);
$resourceClass = $this->getStateOptionsClass($operation, $operation->getClass(), Options::class);
$model = new $resourceClass();

if (!$model instanceof Model) {
Expand Down
Loading
Loading