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

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