vendor/doctrine/doctrine-bundle/src/Repository/ServiceEntityRepositoryProxy.php line 94

  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Bundle\DoctrineBundle\Repository;
  4. use Doctrine\Common\Collections\AbstractLazyCollection;
  5. use Doctrine\Common\Collections\Criteria;
  6. use Doctrine\Common\Collections\Selectable;
  7. use Doctrine\DBAL\LockMode;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Doctrine\ORM\EntityRepository;
  10. use Doctrine\ORM\Mapping\ClassMetadata;
  11. use Doctrine\ORM\Query\ResultSetMappingBuilder;
  12. use Doctrine\ORM\QueryBuilder;
  13. use Doctrine\Persistence\ManagerRegistry;
  14. use LogicException;
  15. use Symfony\Component\VarExporter\LazyObjectInterface;
  16. use function sprintf;
  17. /**
  18.  * @internal Extend {@see ServiceEntityRepository} instead.
  19.  *
  20.  * @template T of object
  21.  * @template-extends EntityRepository<T>
  22.  */
  23. class ServiceEntityRepositoryProxy extends EntityRepository implements ServiceEntityRepositoryInterface
  24. {
  25.     private ?EntityRepository $repository null;
  26.     /** @param class-string<T> $entityClass The class name of the entity this repository manages */
  27.     public function __construct(
  28.         private readonly ManagerRegistry $registry,
  29.         private readonly string $entityClass,
  30.     ) {
  31.         if (! $this instanceof LazyObjectInterface) {
  32.             return;
  33.         }
  34.         $this->repository $this->resolveRepository();
  35.     }
  36.     public function createQueryBuilder(string $alias, ?string $indexBy null): QueryBuilder
  37.     {
  38.         return ($this->repository ??= $this->resolveRepository())
  39.             ->createQueryBuilder($alias$indexBy);
  40.     }
  41.     public function createResultSetMappingBuilder(string $alias): ResultSetMappingBuilder
  42.     {
  43.         return ($this->repository ??= $this->resolveRepository())
  44.             ->createResultSetMappingBuilder($alias);
  45.     }
  46.     public function find(mixed $idLockMode|int|null $lockMode nullint|null $lockVersion null): object|null
  47.     {
  48.         /** @psalm-suppress InvalidReturnStatement This proxy is used only in combination with newer parent class */
  49.         return ($this->repository ??= $this->resolveRepository())
  50.             ->find($id$lockMode$lockVersion);
  51.     }
  52.     /**
  53.      * {@inheritDoc}
  54.      *
  55.      * @psalm-suppress InvalidReturnStatement This proxy is used only in combination with newer parent class
  56.      * @psalm-suppress InvalidReturnType This proxy is used only in combination with newer parent class
  57.      */
  58.     public function findBy(array $criteria, ?array $orderBy null, ?int $limit null, ?int $offset null): array
  59.     {
  60.         return ($this->repository ??= $this->resolveRepository())
  61.             ->findBy($criteria$orderBy$limit$offset);
  62.     }
  63.     /** {@inheritDoc} */
  64.     public function findOneBy(array $criteria, ?array $orderBy null): object|null
  65.     {
  66.         /** @psalm-suppress InvalidReturnStatement This proxy is used only in combination with newer parent class */
  67.         return ($this->repository ??= $this->resolveRepository())
  68.             ->findOneBy($criteria$orderBy);
  69.     }
  70.     /** {@inheritDoc} */
  71.     public function count(array $criteria = []): int
  72.     {
  73.         return ($this->repository ??= $this->resolveRepository())->count($criteria);
  74.     }
  75.     /**
  76.      * {@inheritDoc}
  77.      */
  78.     public function __call(string $method, array $arguments): mixed
  79.     {
  80.         return ($this->repository ??= $this->resolveRepository())->$method(...$arguments);
  81.     }
  82.     protected function getEntityName(): string
  83.     {
  84.         return ($this->repository ??= $this->resolveRepository())->getEntityName();
  85.     }
  86.     protected function getEntityManager(): EntityManagerInterface
  87.     {
  88.         return ($this->repository ??= $this->resolveRepository())->getEntityManager();
  89.     }
  90.     /** @psalm-suppress InvalidReturnType This proxy is used only in combination with newer parent class */
  91.     protected function getClassMetadata(): ClassMetadata
  92.     {
  93.         /** @psalm-suppress InvalidReturnStatement This proxy is used only in combination with newer parent class */
  94.         return ($this->repository ??= $this->resolveRepository())->getClassMetadata();
  95.     }
  96.     public function matching(Criteria $criteria): AbstractLazyCollection&Selectable
  97.     {
  98.         return ($this->repository ??= $this->resolveRepository())->matching($criteria);
  99.     }
  100.     private function resolveRepository(): EntityRepository
  101.     {
  102.         $manager $this->registry->getManagerForClass($this->entityClass);
  103.         if ($manager === null) {
  104.             throw new LogicException(sprintf(
  105.                 'Could not find the entity manager for class "%s". Check your Doctrine configuration to make sure it is configured to load this entity’s metadata.',
  106.                 $this->entityClass,
  107.             ));
  108.         }
  109.         return new EntityRepository($manager$manager->getClassMetadata($this->entityClass));
  110.     }
  111. }