|
5 | 5 | use Doctrine\ORM\EntityRepository;
|
6 | 6 | use Doctrine\Persistence\ManagerRegistry;
|
7 | 7 | use LogicException;
|
8 |
| -use Symfony\Component\VarExporter\LazyGhostTrait; |
9 | 8 | use Symfony\Component\VarExporter\LazyObjectInterface;
|
10 | 9 |
|
| 10 | +use function debug_backtrace; |
11 | 11 | use function sprintf;
|
12 | 12 |
|
| 13 | +use const DEBUG_BACKTRACE_IGNORE_ARGS; |
| 14 | + |
13 | 15 | /**
|
14 | 16 | * @internal Extend {@see ServiceEntityRepository} instead.
|
15 | 17 | *
|
|
18 | 20 | */
|
19 | 21 | class LazyServiceEntityRepository extends EntityRepository implements ServiceEntityRepositoryInterface
|
20 | 22 | {
|
21 |
| - use LazyGhostTrait { |
22 |
| - createLazyGhost as private; |
23 |
| - } |
| 23 | + private ManagerRegistry $registry; |
| 24 | + private string $entityClass; |
24 | 25 |
|
25 | 26 | /**
|
26 | 27 | * @param string $entityClass The class name of the entity this repository manages
|
27 | 28 | * @psalm-param class-string<T> $entityClass
|
28 | 29 | */
|
29 | 30 | public function __construct(ManagerRegistry $registry, string $entityClass)
|
30 | 31 | {
|
31 |
| - $initializer = function ($instance, $property) use ($registry, $entityClass) { |
32 |
| - $manager = $registry->getManagerForClass($entityClass); |
| 32 | + $this->registry = $registry; |
| 33 | + $this->entityClass = $entityClass; |
33 | 34 |
|
34 |
| - if ($manager === null) { |
35 |
| - throw new LogicException(sprintf( |
36 |
| - '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.', |
37 |
| - $entityClass, |
38 |
| - )); |
39 |
| - } |
| 35 | + if ($this instanceof LazyObjectInterface) { |
| 36 | + $this->initialize(); |
| 37 | + |
| 38 | + return; |
| 39 | + } |
40 | 40 |
|
41 |
| - parent::__construct($manager, $manager->getClassMetadata($entityClass)); |
| 41 | + unset($this->_em); |
| 42 | + unset($this->_class); |
| 43 | + unset($this->_entityName); |
| 44 | + } |
42 | 45 |
|
43 |
| - return $this->$property; |
44 |
| - }; |
| 46 | + /** @return mixed */ |
| 47 | + public function __get(string $name) |
| 48 | + { |
| 49 | + $this->initialize(); |
45 | 50 |
|
46 |
| - if ($this instanceof LazyObjectInterface) { |
47 |
| - $initializer($this, '_entityName'); |
| 51 | + $scope = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['class'] ?? null; |
48 | 52 |
|
49 |
| - return; |
| 53 | + return (function () use ($name) { |
| 54 | + return $this->$name; |
| 55 | + })->bindTo($this, $scope)(); |
| 56 | + } |
| 57 | + |
| 58 | + public function __isset(string $name): bool |
| 59 | + { |
| 60 | + $this->initialize(); |
| 61 | + |
| 62 | + $scope = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['class'] ?? null; |
| 63 | + |
| 64 | + return (function () use ($name) { |
| 65 | + return isset($this->$name); |
| 66 | + })->bindTo($this, $scope)(); |
| 67 | + } |
| 68 | + |
| 69 | + private function initialize(): void |
| 70 | + { |
| 71 | + $manager = $this->registry->getManagerForClass($this->entityClass); |
| 72 | + |
| 73 | + if ($manager === null) { |
| 74 | + throw new LogicException(sprintf( |
| 75 | + '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.', |
| 76 | + $this->entityClass, |
| 77 | + )); |
50 | 78 | }
|
51 | 79 |
|
52 |
| - self::createLazyGhost([ |
53 |
| - "\0*\0_em" => $initializer, |
54 |
| - "\0*\0_class" => $initializer, |
55 |
| - "\0*\0_entityName" => $initializer, |
56 |
| - ], null, $this); |
| 80 | + parent::__construct($manager, $manager->getClassMetadata($this->entityClass)); |
57 | 81 | }
|
58 | 82 | }
|
0 commit comments