vendor/doctrine/doctrine-bundle/Repository/LazyServiceEntityRepository.php line 42

  1. <?php
  2. namespace Doctrine\Bundle\DoctrineBundle\Repository;
  3. use Doctrine\ORM\EntityRepository;
  4. use Doctrine\Persistence\ManagerRegistry;
  5. use LogicException;
  6. use Symfony\Component\VarExporter\LazyGhostTrait;
  7. use function sprintf;
  8. /**
  9.  * Optional EntityRepository base class with a simplified constructor (for autowiring).
  10.  *
  11.  * To use in your class, inject the "registry" service and call
  12.  * the parent constructor. For example:
  13.  *
  14.  * class YourEntityRepository extends ServiceEntityRepository
  15.  * {
  16.  *     public function __construct(ManagerRegistry $registry)
  17.  *     {
  18.  *         parent::__construct($registry, YourEntity::class);
  19.  *     }
  20.  * }
  21.  *
  22.  * @internal to be renamed ServiceEntityRepository when PHP 8.1 / Symfony 6.2 becomes required
  23.  *
  24.  * @template T of object
  25.  * @template-extends EntityRepository<T>
  26.  */
  27. class LazyServiceEntityRepository extends EntityRepository implements ServiceEntityRepositoryInterface
  28. {
  29.     use LazyGhostTrait;
  30.     /**
  31.      * @param string $entityClass The class name of the entity this repository manages
  32.      * @psalm-param class-string<T> $entityClass
  33.      */
  34.     public function __construct(ManagerRegistry $registrystring $entityClass)
  35.     {
  36.         $initializer = function ($instance$property) use ($registry$entityClass) {
  37.             $manager $registry->getManagerForClass($entityClass);
  38.             if ($manager === null) {
  39.                 throw new LogicException(sprintf(
  40.                     '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.',
  41.                     $entityClass
  42.                 ));
  43.             }
  44.             parent::__construct($manager$manager->getClassMetadata($entityClass));
  45.             return $this->$property;
  46.         };
  47.         self::createLazyGhost([
  48.             "\0*\0_em" => $initializer,
  49.             "\0*\0_class" => $initializer,
  50.             "\0*\0_entityName" => $initializer,
  51.         ], null$this);
  52.     }
  53. }