|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | namespace Doctrine\Bundle\DoctrineBundle\Repository; |
4 | 6 |
|
| 7 | +use Doctrine\Common\Collections\AbstractLazyCollection; |
| 8 | +use Doctrine\Common\Collections\Criteria; |
| 9 | +use Doctrine\Common\Collections\Selectable; |
| 10 | +use Doctrine\DBAL\LockMode; |
| 11 | +use Doctrine\ORM\EntityManagerInterface; |
5 | 12 | use Doctrine\ORM\EntityRepository; |
| 13 | +use Doctrine\ORM\Mapping\ClassMetadata; |
| 14 | +use Doctrine\ORM\Query\ResultSetMappingBuilder; |
| 15 | +use Doctrine\ORM\QueryBuilder; |
| 16 | +use Doctrine\Persistence\ManagerRegistry; |
| 17 | +use LogicException; |
| 18 | +use Symfony\Component\VarExporter\LazyObjectInterface; |
| 19 | + |
| 20 | +use function sprintf; |
| 21 | + |
| 22 | +/** |
| 23 | + * Optional EntityRepository base class with a simplified constructor (for autowiring). |
| 24 | + * |
| 25 | + * To use in your class, inject the "registry" service and call |
| 26 | + * the parent constructor. For example: |
| 27 | + * |
| 28 | + * class YourEntityRepository extends ServiceEntityRepository |
| 29 | + * { |
| 30 | + * public function __construct(ManagerRegistry $registry) |
| 31 | + * { |
| 32 | + * parent::__construct($registry, YourEntity::class); |
| 33 | + * } |
| 34 | + * } |
| 35 | + * |
| 36 | + * @template T of object |
| 37 | + * @template-extends EntityRepository<T> |
| 38 | + */ |
| 39 | +class ServiceEntityRepository extends EntityRepository implements ServiceEntityRepositoryInterface |
| 40 | +{ |
| 41 | + /** @var EntityRepository<T> */ |
| 42 | + private EntityRepository|null $repository = null; |
| 43 | + |
| 44 | + /** @param class-string<T> $entityClass The class name of the entity this repository manages */ |
| 45 | + public function __construct( |
| 46 | + private readonly ManagerRegistry $registry, |
| 47 | + private readonly string $entityClass, |
| 48 | + ) { |
| 49 | + if (! $this instanceof LazyObjectInterface) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + $this->repository = $this->resolveRepository(); |
| 54 | + } |
| 55 | + |
| 56 | + public function createQueryBuilder(string $alias, string|null $indexBy = null): QueryBuilder |
| 57 | + { |
| 58 | + return ($this->repository ??= $this->resolveRepository()) |
| 59 | + ->createQueryBuilder($alias, $indexBy); |
| 60 | + } |
| 61 | + |
| 62 | + public function createResultSetMappingBuilder(string $alias): ResultSetMappingBuilder |
| 63 | + { |
| 64 | + return ($this->repository ??= $this->resolveRepository()) |
| 65 | + ->createResultSetMappingBuilder($alias); |
| 66 | + } |
6 | 67 |
|
7 | | -use function property_exists; |
| 68 | + public function find(mixed $id, LockMode|int|null $lockMode = null, int|null $lockVersion = null): object|null |
| 69 | + { |
| 70 | + /** @psalm-suppress InvalidReturnStatement This proxy is used only in combination with newer parent class */ |
| 71 | + return ($this->repository ??= $this->resolveRepository()) |
| 72 | + ->find($id, $lockMode, $lockVersion); |
| 73 | + } |
8 | 74 |
|
9 | | -if (property_exists(EntityRepository::class, '_entityName')) { |
10 | | - // ORM 2 |
11 | 75 | /** |
12 | | - * Optional EntityRepository base class with a simplified constructor (for autowiring). |
| 76 | + * {@inheritDoc} |
13 | 77 | * |
14 | | - * To use in your class, inject the "registry" service and call |
15 | | - * the parent constructor. For example: |
16 | | - * |
17 | | - * class YourEntityRepository extends ServiceEntityRepository |
18 | | - * { |
19 | | - * public function __construct(ManagerRegistry $registry) |
20 | | - * { |
21 | | - * parent::__construct($registry, YourEntity::class); |
22 | | - * } |
23 | | - * } |
24 | | - * |
25 | | - * @template T of object |
26 | | - * @template-extends LazyServiceEntityRepository<T> |
| 78 | + * @psalm-suppress InvalidReturnStatement This proxy is used only in combination with newer parent class |
| 79 | + * @psalm-suppress InvalidReturnType This proxy is used only in combination with newer parent class |
27 | 80 | */ |
28 | | - class ServiceEntityRepository extends LazyServiceEntityRepository |
| 81 | + public function findBy(array $criteria, array|null $orderBy = null, int|null $limit = null, int|null $offset = null): array |
| 82 | + { |
| 83 | + return ($this->repository ??= $this->resolveRepository()) |
| 84 | + ->findBy($criteria, $orderBy, $limit, $offset); |
| 85 | + } |
| 86 | + |
| 87 | + /** {@inheritDoc} */ |
| 88 | + public function findOneBy(array $criteria, array|null $orderBy = null): object|null |
| 89 | + { |
| 90 | + /** @psalm-suppress InvalidReturnStatement This proxy is used only in combination with newer parent class */ |
| 91 | + return ($this->repository ??= $this->resolveRepository()) |
| 92 | + ->findOneBy($criteria, $orderBy); |
| 93 | + } |
| 94 | + |
| 95 | + /** {@inheritDoc} */ |
| 96 | + public function count(array $criteria = []): int |
29 | 97 | { |
| 98 | + return ($this->repository ??= $this->resolveRepository())->count($criteria); |
30 | 99 | } |
31 | | -} else { |
32 | | - // ORM 3 |
| 100 | + |
33 | 101 | /** |
34 | | - * Optional EntityRepository base class with a simplified constructor (for autowiring). |
35 | | - * |
36 | | - * To use in your class, inject the "registry" service and call |
37 | | - * the parent constructor. For example: |
38 | | - * |
39 | | - * class YourEntityRepository extends ServiceEntityRepository |
40 | | - * { |
41 | | - * public function __construct(ManagerRegistry $registry) |
42 | | - * { |
43 | | - * parent::__construct($registry, YourEntity::class); |
44 | | - * } |
45 | | - * } |
46 | | - * |
47 | | - * @template T of object |
48 | | - * @template-extends ServiceEntityRepositoryProxy<T> |
| 102 | + * {@inheritDoc} |
49 | 103 | */ |
50 | | - class ServiceEntityRepository extends ServiceEntityRepositoryProxy |
| 104 | + public function __call(string $method, array $arguments): mixed |
| 105 | + { |
| 106 | + return ($this->repository ??= $this->resolveRepository())->$method(...$arguments); |
| 107 | + } |
| 108 | + |
| 109 | + protected function getEntityName(): string |
| 110 | + { |
| 111 | + return ($this->repository ??= $this->resolveRepository())->getEntityName(); |
| 112 | + } |
| 113 | + |
| 114 | + protected function getEntityManager(): EntityManagerInterface |
| 115 | + { |
| 116 | + return ($this->repository ??= $this->resolveRepository())->getEntityManager(); |
| 117 | + } |
| 118 | + |
| 119 | + /** @psalm-suppress InvalidReturnType This proxy is used only in combination with newer parent class */ |
| 120 | + protected function getClassMetadata(): ClassMetadata |
| 121 | + { |
| 122 | + /** @psalm-suppress InvalidReturnStatement This proxy is used only in combination with newer parent class */ |
| 123 | + return ($this->repository ??= $this->resolveRepository())->getClassMetadata(); |
| 124 | + } |
| 125 | + |
| 126 | + /** @phpstan-return AbstractLazyCollection<int, T>&Selectable<int, T> */ |
| 127 | + public function matching(Criteria $criteria): AbstractLazyCollection&Selectable |
51 | 128 | { |
| 129 | + return ($this->repository ??= $this->resolveRepository())->matching($criteria); |
| 130 | + } |
| 131 | + |
| 132 | + /** @return EntityRepository<T> */ |
| 133 | + private function resolveRepository(): EntityRepository |
| 134 | + { |
| 135 | + $manager = $this->registry->getManagerForClass($this->entityClass); |
| 136 | + |
| 137 | + if (! $manager instanceof EntityManagerInterface) { |
| 138 | + throw new LogicException(sprintf( |
| 139 | + '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.', |
| 140 | + $this->entityClass, |
| 141 | + )); |
| 142 | + } |
| 143 | + |
| 144 | + /** @var ClassMetadata<T> $classMetadata */ |
| 145 | + $classMetadata = $manager->getClassMetadata($this->entityClass); |
| 146 | + |
| 147 | + return new EntityRepository($manager, $classMetadata); |
52 | 148 | } |
53 | 149 | } |
0 commit comments