vendor/api-platform/core/src/Bridge/Doctrine/Orm/CollectionDataProvider.php line 69

Open in your IDE?
  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\Core\Bridge\Doctrine\Orm;
  12. use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
  13. use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface;
  14. use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
  15. use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
  16. use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
  17. use ApiPlatform\Core\Exception\RuntimeException;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Doctrine\Persistence\ManagerRegistry;
  20. /**
  21.  * Collection data provider for the Doctrine ORM.
  22.  *
  23.  * @author Kévin Dunglas <dunglas@gmail.com>
  24.  * @author Samuel ROZE <samuel.roze@gmail.com>
  25.  * @final
  26.  */
  27. class CollectionDataProvider implements ContextAwareCollectionDataProviderInterfaceRestrictedDataProviderInterface
  28. {
  29.     private $managerRegistry;
  30.     private $collectionExtensions;
  31.     /**
  32.      * @param QueryCollectionExtensionInterface[] $collectionExtensions
  33.      */
  34.     public function __construct(ManagerRegistry $managerRegistryiterable $collectionExtensions = [])
  35.     {
  36.         $this->managerRegistry $managerRegistry;
  37.         $this->collectionExtensions $collectionExtensions;
  38.     }
  39.     public function supports(string $resourceClassstring $operationName null, array $context = []): bool
  40.     {
  41.         return $this->managerRegistry->getManagerForClass($resourceClass) instanceof EntityManagerInterface;
  42.     }
  43.     /**
  44.      * {@inheritdoc}
  45.      *
  46.      * @throws RuntimeException
  47.      */
  48.     public function getCollection(string $resourceClassstring $operationName null, array $context = [])
  49.     {
  50.         /** @var EntityManagerInterface $manager */
  51.         $manager $this->managerRegistry->getManagerForClass($resourceClass);
  52.         $repository $manager->getRepository($resourceClass);
  53.         if (!method_exists($repository'createQueryBuilder')) {
  54.             throw new RuntimeException('The repository class must have a "createQueryBuilder" method.');
  55.         }
  56.         $queryBuilder $repository->createQueryBuilder('o');
  57.         $queryNameGenerator = new QueryNameGenerator();
  58.         foreach ($this->collectionExtensions as $extension) {
  59.             $extension->applyToCollection($queryBuilder$queryNameGenerator$resourceClass$operationName$context);
  60.             if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass$operationName$context)) {
  61.                 return $extension->getResult($queryBuilder$resourceClass$operationName$context);
  62.             }
  63.         }
  64.         return $queryBuilder->getQuery()->getResult();
  65.     }
  66. }