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. * {@inheritdoc}
  48. *
  49. * @throws RuntimeException
  50. */
  51. public function getCollection(string $resourceClass, string $operationName = null, array $context = []): iterable
  52. {
  53. /** @var EntityManagerInterface $manager */
  54. $manager = $this->managerRegistry->getManagerForClass($resourceClass);
  55. $repository = $manager->getRepository($resourceClass);
  56. if (!method_exists($repository, 'createQueryBuilder')) {
  57. throw new RuntimeException('The repository class must have a "createQueryBuilder" method.');
  58. }
  59. $queryBuilder = $repository->createQueryBuilder('o');
  60. $queryNameGenerator = new QueryNameGenerator();
  61. foreach ($this->collectionExtensions as $extension) {
  62. if ($extension instanceof LegacyQueryCollectionExtensionInterface) {
  63. $extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context); // @phpstan-ignore-line because of context
  64. } elseif ($extension instanceof QueryCollectionExtensionInterface) {
  65. $extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $context['operation'] ?? null, $context);
  66. }
  67. if ($extension instanceof LegacyQueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) { // @phpstan-ignore-line because of context
  68. return $extension->getResult($queryBuilder, $resourceClass, $operationName, $context); // @phpstan-ignore-line because of context
  69. }
  70. if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $context['operation'] ?? null, $context)) {
  71. return $extension->getResult($queryBuilder, $resourceClass, $context['operation'] ?? null, $context);
  72. }
  73. }
  74. return $queryBuilder->getQuery()->getResult();
  75. }
  76. }