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

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