1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Porpaginas\Doctrine\Repository; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Doctrine\ORM\EntityRepository; |
7
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
8
|
|
|
use Doctrine\ORM\Query; |
9
|
|
|
use Doctrine\ORM\QueryBuilder; |
10
|
|
|
use Doctrine\Persistence\ObjectRepository; |
11
|
|
|
use Zenstruck\Porpaginas\Doctrine\Batch\ORMCountableBatchProcessor; |
12
|
|
|
use Zenstruck\Porpaginas\Doctrine\ORMQueryResult; |
13
|
|
|
use Zenstruck\Porpaginas\Exception\NotFound; |
14
|
|
|
use Zenstruck\Porpaginas\Repository; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @mixin EntityRepository |
18
|
|
|
* |
19
|
|
|
* @author Kevin Bond <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class ORMRepository implements ObjectRepository, Repository |
22
|
|
|
{ |
23
|
|
|
private const DEFAULT_ALIAS = 'entity'; |
24
|
|
|
|
25
|
|
|
private ?EntityRepository $repo = null; |
26
|
|
|
|
27
|
1 |
|
final public function __call($name, $arguments) |
28
|
|
|
{ |
29
|
1 |
|
return $this->repo()->{$name}(...$arguments); |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
public function getIterator(): \Traversable |
33
|
|
|
{ |
34
|
1 |
|
return $this->batchIterator(); |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
public function batchIterator(int $chunkSize = 100): \Traversable |
38
|
|
|
{ |
39
|
1 |
|
return static::createResult($this->qb())->batchIterator($chunkSize); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function batchProcessor(int $chunkSize = 100): ORMCountableBatchProcessor |
43
|
|
|
{ |
44
|
1 |
|
return static::createResult($this->qb())->batchProcessor($chunkSize); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function count(): int |
48
|
|
|
{ |
49
|
1 |
|
return \count(static::createResult($this->qb())); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @see EntityRepository::find() |
54
|
|
|
*/ |
55
|
1 |
|
public function find($id, $lockMode = null, $lockVersion = null): ?object |
56
|
|
|
{ |
57
|
1 |
|
return $this->repo()->find($id, $lockMode, $lockVersion); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @see EntityRepository::findAll() |
62
|
|
|
*/ |
63
|
1 |
|
public function findAll(): array |
64
|
|
|
{ |
65
|
1 |
|
return $this->repo()->findAll(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @see EntityRepository::findBy() |
70
|
|
|
*/ |
71
|
1 |
|
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array |
72
|
|
|
{ |
73
|
1 |
|
return $this->repo()->findBy($criteria, $orderBy, $limit, $offset); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @see EntityRepository::findOneBy() |
78
|
|
|
*/ |
79
|
1 |
|
public function findOneBy(array $criteria): ?object |
80
|
|
|
{ |
81
|
1 |
|
return $this->repo()->findOneBy($criteria); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param callable(QueryBuilder $qb, string $alias) $specification |
86
|
|
|
*/ |
87
|
2 |
|
final public function get(callable $specification) |
88
|
|
|
{ |
89
|
2 |
|
if (null === $result = $this->queryForSpecification($specification)->getOneOrNullResult()) { |
90
|
1 |
|
throw new NotFound("{$this->getClassName()} not found for given specification."); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
return $result; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param callable(QueryBuilder $qb, string $alias) $specification |
98
|
|
|
*/ |
99
|
1 |
|
final public function filter(callable $specification): ORMQueryResult |
100
|
|
|
{ |
101
|
1 |
|
return self::createResult($this->queryForSpecification($specification)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Query|QueryBuilder $query |
106
|
|
|
*/ |
107
|
3 |
|
final protected static function createResult($query): ORMQueryResult |
108
|
|
|
{ |
109
|
3 |
|
return new ORMQueryResult($query); |
110
|
|
|
} |
111
|
|
|
|
112
|
5 |
|
final protected function qb(string $alias = self::DEFAULT_ALIAS, string $indexBy = null): QueryBuilder |
113
|
|
|
{ |
114
|
5 |
|
return $this->repo()->createQueryBuilder($alias, $indexBy); |
115
|
|
|
} |
116
|
|
|
|
117
|
10 |
|
final protected function repo(): EntityRepository |
118
|
|
|
{ |
119
|
10 |
|
return $this->repo ?: $this->repo = static::createEntityRepository($this->em(), $this->em()->getClassMetadata($this->getClassName())); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
abstract protected function em(): EntityManagerInterface; |
123
|
|
|
|
124
|
10 |
|
protected static function createEntityRepository(EntityManagerInterface $em, ClassMetadata $class): EntityRepository |
125
|
|
|
{ |
126
|
10 |
|
return new EntityRepository($em, $class); |
127
|
|
|
} |
128
|
|
|
|
129
|
3 |
|
private function queryForSpecification(callable $specification): Query |
130
|
|
|
{ |
131
|
3 |
|
$specification($qb = $this->qb(self::DEFAULT_ALIAS), self::DEFAULT_ALIAS); |
132
|
|
|
|
133
|
3 |
|
return $qb->getQuery(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|