Skip to content

Commit 6107050

Browse files
authored
Merge pull request #1972 from greg0ire/no-lser
Remove unused types
2 parents 5d60fb6 + dd2341d commit 6107050

File tree

7 files changed

+149
-294
lines changed

7 files changed

+149
-294
lines changed

UPGRADE-3.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ available only in `doctrine/orm` 2. It has been removed.
2424

2525
Support for the YML and annotation metadata drivers has been dropped.
2626

27+
`Doctrine\Bundle\DoctrineBundle\Repository\LazyServiceEntityRepository` has
28+
been removed without replacement.
29+
2730
Commands
2831
--------
2932

phpcs.xml.dist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727

2828
<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
2929
<exclude-pattern>tests/*</exclude-pattern>
30-
<exclude-pattern>src/Repository/RepositoryFactoryCompatibility.php</exclude-pattern>
31-
<exclude-pattern>src/Repository/ServiceEntityRepository.php</exclude-pattern>
3230
</rule>
3331
<rule ref="Squiz.Classes.ClassFileName.NoMatch">
3432
<exclude-pattern>tests/*</exclude-pattern>

src/Repository/ContainerRepositoryFactory.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
*/
2424
final class ContainerRepositoryFactory implements RepositoryFactory
2525
{
26-
use RepositoryFactoryCompatibility;
27-
2826
/** @var array<string, ObjectRepository> */
2927
private array $managedRepositories = [];
3028

@@ -34,6 +32,20 @@ public function __construct(
3432
) {
3533
}
3634

35+
/**
36+
* Gets the repository for an entity class.
37+
*
38+
* @param class-string<T> $entityName
39+
*
40+
* @return EntityRepository<T>
41+
*
42+
* @template T of object
43+
*/
44+
public function getRepository(EntityManagerInterface $entityManager, string $entityName): EntityRepository
45+
{
46+
return $this->doGetRepository($entityManager, $entityName, true);
47+
}
48+
3749
/**
3850
* @param class-string<T> $entityName
3951
*

src/Repository/LazyServiceEntityRepository.php

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/Repository/RepositoryFactoryCompatibility.php

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 132 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,149 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Doctrine\Bundle\DoctrineBundle\Repository;
46

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;
512
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+
}
667

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+
}
874

9-
if (property_exists(EntityRepository::class, '_entityName')) {
10-
// ORM 2
1175
/**
12-
* Optional EntityRepository base class with a simplified constructor (for autowiring).
76+
* {@inheritDoc}
1377
*
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
2780
*/
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
2997
{
98+
return ($this->repository ??= $this->resolveRepository())->count($criteria);
3099
}
31-
} else {
32-
// ORM 3
100+
33101
/**
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}
49103
*/
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
51128
{
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);
52148
}
53149
}

0 commit comments

Comments
 (0)