vendor/api-platform/core/src/Doctrine/Orm/State/ItemProvider.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\QueryItemExtensionInterface;
  13. use ApiPlatform\Doctrine\Orm\Extension\QueryResultItemExtensionInterface;
  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.  * Item 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 ItemProvider implements ProviderInterface
  29. {
  30.     use LinksHandlerTrait;
  31.     /**
  32.      * @param QueryItemExtensionInterface[] $itemExtensions
  33.      */
  34.     public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly ManagerRegistry $managerRegistry, private readonly iterable $itemExtensions = [], ContainerInterface $handleLinksLocator null)
  35.     {
  36.         $this->resourceMetadataCollectionFactory $resourceMetadataCollectionFactory;
  37.         $this->handleLinksLocator $handleLinksLocator;
  38.     }
  39.     public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object
  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.         $fetchData $context['fetch_data'] ?? true;
  48.         if (!$fetchData && \array_key_exists('id'$uriVariables)) {
  49.             // todo : if uriVariables don't contain the id, this fails. This should behave like it does in the following code
  50.             return $manager->getReference($entityClass$uriVariables);
  51.         }
  52.         $repository $manager->getRepository($entityClass);
  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.         if ($handleLinks $this->getLinksHandler($operation)) {
  59.             $handleLinks($queryBuilder$uriVariables$queryNameGenerator, ['entityClass' => $entityClass'operation' => $operation] + $context);
  60.         } else {
  61.             $this->handleLinks($queryBuilder$uriVariables$queryNameGenerator$context$entityClass$operation);
  62.         }
  63.         foreach ($this->itemExtensions as $extension) {
  64.             $extension->applyToItem($queryBuilder$queryNameGenerator$entityClass$uriVariables$operation$context);
  65.             if ($extension instanceof QueryResultItemExtensionInterface && $extension->supportsResult($entityClass$operation$context)) {
  66.                 return $extension->getResult($queryBuilder$entityClass$operation$context);
  67.             }
  68.         }
  69.         return $queryBuilder->getQuery()->getOneOrNullResult();
  70.     }
  71. }