vendor/api-platform/core/src/Doctrine/Odm/State/CollectionProvider.php line 41

  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\Odm\State;
  12. use ApiPlatform\Doctrine\Odm\Extension\AggregationCollectionExtensionInterface;
  13. use ApiPlatform\Doctrine\Odm\Extension\AggregationResultCollectionExtensionInterface;
  14. use ApiPlatform\Exception\RuntimeException;
  15. use ApiPlatform\Metadata\Operation;
  16. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  17. use ApiPlatform\State\ProviderInterface;
  18. use Doctrine\ODM\MongoDB\DocumentManager;
  19. use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
  20. use Doctrine\Persistence\ManagerRegistry;
  21. /**
  22.  * Collection state provider using the Doctrine ODM.
  23.  */
  24. final class CollectionProvider implements ProviderInterface
  25. {
  26.     use LinksHandlerTrait;
  27.     /**
  28.      * @param AggregationCollectionExtensionInterface[] $collectionExtensions
  29.      */
  30.     public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly ManagerRegistry $managerRegistry, private readonly iterable $collectionExtensions = [])
  31.     {
  32.         $this->resourceMetadataCollectionFactory $resourceMetadataCollectionFactory;
  33.     }
  34.     public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable
  35.     {
  36.         $resourceClass $operation->getClass();
  37.         /** @var DocumentManager $manager */
  38.         $manager $this->managerRegistry->getManagerForClass($resourceClass);
  39.         $repository $manager->getRepository($resourceClass);
  40.         if (!$repository instanceof DocumentRepository) {
  41.             throw new RuntimeException(sprintf('The repository for "%s" must be an instance of "%s".'$resourceClassDocumentRepository::class));
  42.         }
  43.         $aggregationBuilder $repository->createAggregationBuilder();
  44.         $this->handleLinks($aggregationBuilder$uriVariables$context$resourceClass$operation);
  45.         foreach ($this->collectionExtensions as $extension) {
  46.             $extension->applyToCollection($aggregationBuilder$resourceClass$operation$context);
  47.             if ($extension instanceof AggregationResultCollectionExtensionInterface && $extension->supportsResult($resourceClass$operation$context)) {
  48.                 return $extension->getResult($aggregationBuilder$resourceClass$operation$context);
  49.             }
  50.         }
  51.         $attribute $operation->getExtraProperties()['doctrine_mongodb'] ?? [];
  52.         $executeOptions $attribute['execute_options'] ?? [];
  53.         return $aggregationBuilder->hydrate($resourceClass)->execute($executeOptions);
  54.     }
  55. }