vendor/api-platform/core/src/Doctrine/Orm/State/CollectionProvider.php line 40

  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Doctrine\Orm\State;
  12. use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
  13. use ApiPlatform\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface;
  14. use ApiPlatform\Doctrine\Orm\Util\QueryNameGenerator;
  15. use ApiPlatform\Exception\RuntimeException;
  16. use ApiPlatform\Metadata\Operation;
  17. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  18. use ApiPlatform\State\ProviderInterface;
  19. use Doctrine\ORM\EntityManagerInterface;
  20. use Doctrine\Persistence\ManagerRegistry;
  21. use Psr\Container\ContainerInterface;
  22. /**
  23.  * Collection state provider using the Doctrine ORM.
  24.  *
  25.  * @author Kévin Dunglas <kevin@dunglas.fr>
  26.  * @author Samuel ROZE <samuel.roze@gmail.com>
  27.  */
  28. final class CollectionProvider implements ProviderInterface
  29. {
  30.     use LinksHandlerTrait;
  31.     /**
  32.      * @param QueryCollectionExtensionInterface[] $collectionExtensions
  33.      */
  34.     public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly ManagerRegistry $managerRegistry, private readonly iterable $collectionExtensions = [], ContainerInterface $handleLinksLocator null)
  35.     {
  36.         $this->resourceMetadataCollectionFactory $resourceMetadataCollectionFactory;
  37.         $this->handleLinksLocator $handleLinksLocator;
  38.     }
  39.     public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable
  40.     {
  41.         $entityClass $operation->getClass();
  42.         if (($options $operation->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
  43.             $entityClass $options->getEntityClass();
  44.         }
  45.         /** @var EntityManagerInterface $manager */
  46.         $manager $this->managerRegistry->getManagerForClass($entityClass);
  47.         $repository $manager->getRepository($entityClass);
  48.         if (!method_exists($repository'createQueryBuilder')) {
  49.             throw new RuntimeException('The repository class must have a "createQueryBuilder" method.');
  50.         }
  51.         $queryBuilder $repository->createQueryBuilder('o');
  52.         $queryNameGenerator = new QueryNameGenerator();
  53.         if ($handleLinks $this->getLinksHandler($operation)) {
  54.             $handleLinks($queryBuilder$uriVariables$queryNameGenerator, ['entityClass' => $entityClass'operation' => $operation] + $context);
  55.         } else {
  56.             $this->handleLinks($queryBuilder$uriVariables$queryNameGenerator$context$entityClass$operation);
  57.         }
  58.         foreach ($this->collectionExtensions as $extension) {
  59.             $extension->applyToCollection($queryBuilder$queryNameGenerator$entityClass$operation$context);
  60.             if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($entityClass$operation$context)) {
  61.                 return $extension->getResult($queryBuilder$entityClass$operation$context);
  62.             }
  63.         }
  64.         return $queryBuilder->getQuery()->getResult();
  65.     }
  66. }