vendor/doctrine/doctrine-bundle/src/Repository/LazyServiceEntityRepository.php line 69

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\LazyObjectInterface;
  7. use function debug_backtrace;
  8. use function sprintf;
  9. use const DEBUG_BACKTRACE_IGNORE_ARGS;
  10. /**
  11. * @internal Extend {@see ServiceEntityRepository} instead.
  12. *
  13. * @template T of object
  14. * @template-extends EntityRepository<T>
  15. */
  16. class LazyServiceEntityRepository extends EntityRepository implements ServiceEntityRepositoryInterface
  17. {
  18. private ManagerRegistry $registry;
  19. private string $entityClass;
  20. /**
  21. * @param string $entityClass The class name of the entity this repository manages
  22. * @phpstan-param class-string<T> $entityClass
  23. */
  24. public function __construct(ManagerRegistry $registry, string $entityClass)
  25. {
  26. $this->registry = $registry;
  27. $this->entityClass = $entityClass;
  28. if ($this instanceof LazyObjectInterface) {
  29. $this->initialize();
  30. return;
  31. }
  32. unset($this->_em);
  33. unset($this->_class);
  34. unset($this->_entityName);
  35. }
  36. /** @return mixed */
  37. public function __get(string $name)
  38. {
  39. $this->initialize();
  40. $scope = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['class'] ?? null;
  41. return (function () use ($name) {
  42. return $this->$name;
  43. })->bindTo($this, $scope)();
  44. }
  45. public function __isset(string $name): bool
  46. {
  47. $this->initialize();
  48. $scope = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['class'] ?? null;
  49. return (function () use ($name) {
  50. return isset($this->$name);
  51. })->bindTo($this, $scope)();
  52. }
  53. private function initialize(): void
  54. {
  55. $manager = $this->registry->getManagerForClass($this->entityClass);
  56. if ($manager === null) {
  57. throw new LogicException(sprintf(
  58. '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.',
  59. $this->entityClass,
  60. ));
  61. }
  62. parent::__construct($manager, $manager->getClassMetadata($this->entityClass));
  63. }
  64. }