-
-
Notifications
You must be signed in to change notification settings - Fork 964
Expand file tree
/
Copy pathPaginationExtension.php
More file actions
212 lines (174 loc) · 7.73 KB
/
PaginationExtension.php
File metadata and controls
212 lines (174 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Doctrine\Orm\Extension;
use ApiPlatform\Doctrine\Orm\AbstractPaginator;
use ApiPlatform\Doctrine\Orm\Paginator;
use ApiPlatform\Doctrine\Orm\Util\QueryChecker;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\Pagination\Pagination;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Tools\Pagination\CountWalker;
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrineOrmPaginator;
use Doctrine\Persistence\ManagerRegistry;
// Help opcache.preload discover always-needed symbols
class_exists(AbstractPaginator::class);
/**
* Applies pagination on the Doctrine query for resource collection when enabled.
*
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Samuel ROZE <samuel.roze@gmail.com>
*/
final class PaginationExtension implements QueryResultCollectionExtensionInterface
{
public function __construct(private readonly ManagerRegistry $managerRegistry, private readonly ?Pagination $pagination)
{
}
/**
* {@inheritdoc}
*/
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
{
if (null === $pagination = $this->getPagination($queryBuilder, $operation, $context)) {
return;
}
[$offset, $limit] = $pagination;
$queryBuilder
->setFirstResult($offset)
->setMaxResults($limit);
}
/**
* {@inheritdoc}
*/
public function supportsResult(string $resourceClass, ?Operation $operation = null, array $context = []): bool
{
if ($context['graphql_operation_name'] ?? false) {
return $this->pagination->isGraphQlEnabled($operation);
}
return $this->pagination->isEnabled($operation, $context);
}
/**
* {@inheritdoc}
*/
public function getResult(QueryBuilder $queryBuilder, ?string $resourceClass = null, ?Operation $operation = null, array $context = []): iterable
{
$query = $queryBuilder->getQuery();
// Only one alias, without joins, disable the DISTINCT on the COUNT
if (1 === \count($queryBuilder->getAllAliases())) {
$query->setHint(CountWalker::HINT_DISTINCT, false);
}
$doctrineOrmPaginator = new DoctrineOrmPaginator($query, $this->shouldDoctrinePaginatorFetchJoinCollection($queryBuilder, $operation, $context));
$doctrineOrmPaginator->setUseOutputWalkers($this->shouldDoctrinePaginatorUseOutputWalkers($queryBuilder, $operation, $context));
$isPartialEnabled = $this->pagination->isPartialEnabled($operation, $context);
if ($isPartialEnabled) {
return new class($doctrineOrmPaginator) extends AbstractPaginator {
};
}
return new Paginator($doctrineOrmPaginator);
}
/**
* @throws InvalidArgumentException
*/
private function getPagination(QueryBuilder $queryBuilder, ?Operation $operation, array $context): ?array
{
$enabled = isset($context['graphql_operation_name']) ? $this->pagination->isGraphQlEnabled($operation) : $this->pagination->isEnabled($operation, $context);
if (!$enabled) {
return null;
}
$context = $this->addCountToContext($queryBuilder, $context);
return \array_slice($this->pagination->getPagination($operation, $context), 1);
}
private function addCountToContext(QueryBuilder $queryBuilder, array $context): array
{
if (!($context['graphql_operation_name'] ?? false)) {
return $context;
}
if (isset($context['filters']['last']) && !isset($context['filters']['before'])) {
$context['count'] = (new DoctrineOrmPaginator($queryBuilder))->count();
}
return $context;
}
/**
* Determines the value of the $fetchJoinCollection argument passed to the Doctrine ORM Paginator.
*/
private function shouldDoctrinePaginatorFetchJoinCollection(QueryBuilder $queryBuilder, ?Operation $operation = null, array $context = []): bool
{
$fetchJoinCollection = $operation?->getPaginationFetchJoinCollection();
if (isset($context['operation_name']) && isset($fetchJoinCollection)) {
return $fetchJoinCollection;
}
if (isset($context['graphql_operation_name']) && isset($fetchJoinCollection)) {
return $fetchJoinCollection;
}
/*
* "Cannot count query which selects two FROM components, cannot make distinction"
*
* @see https://github.com/doctrine/orm/blob/v2.6.3/lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php#L81
* @see https://github.com/doctrine/doctrine2/issues/2910
*/
if (QueryChecker::hasRootEntityWithCompositeIdentifier($queryBuilder, $this->managerRegistry)) {
return false;
}
if (QueryChecker::hasJoinedToManyAssociation($queryBuilder, $this->managerRegistry)) {
return true;
}
// disable $fetchJoinCollection by default (performance)
return false;
}
/**
* Determines whether the Doctrine ORM Paginator should use output walkers.
*/
private function shouldDoctrinePaginatorUseOutputWalkers(QueryBuilder $queryBuilder, ?Operation $operation = null, array $context = []): bool
{
$useOutputWalkers = $operation?->getPaginationUseOutputWalkers();
if (isset($context['operation_name']) && isset($useOutputWalkers)) {
return $useOutputWalkers;
}
if (isset($context['graphql_operation_name']) && isset($useOutputWalkers)) {
return $useOutputWalkers;
}
/*
* "Cannot count query that uses a HAVING clause. Use the output walkers for pagination"
*
* @see https://github.com/doctrine/orm/blob/v2.6.3/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php#L56
*/
if (QueryChecker::hasHavingClause($queryBuilder)) {
return true;
}
/*
* "Cannot count query which selects two FROM components, cannot make distinction"
*
* @see https://github.com/doctrine/orm/blob/v2.6.3/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php#L64
*/
if (QueryChecker::hasRootEntityWithCompositeIdentifier($queryBuilder, $this->managerRegistry)) {
return true;
}
/*
* "Paginating an entity with foreign key as identifier only works when using the Output Walkers. Call Paginator#setUseOutputWalkers(true) before iterating the paginator."
*
* @see https://github.com/doctrine/orm/blob/v2.6.3/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php#L77
*/
if (QueryChecker::hasRootEntityWithForeignKeyIdentifier($queryBuilder, $this->managerRegistry)) {
return true;
}
/*
* "Cannot select distinct identifiers from query with LIMIT and ORDER BY on a column from a fetch joined to-many association. Use output walkers."
*
* @see https://github.com/doctrine/orm/blob/v2.6.3/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php#L150
*/
if (QueryChecker::hasMaxResults($queryBuilder) && QueryChecker::hasOrderByOnFetchJoinedToManyAssociation($queryBuilder, $this->managerRegistry)) {
return true;
}
// Disable output walkers by default (performance)
return false;
}
}