|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the API Platform project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Extension; |
|
15
|
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Paginator; |
|
17
|
|
|
use ApiPlatform\Core\DataProvider\Pagination; |
|
18
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
19
|
|
|
use Doctrine\ODM\MongoDB\Aggregation\Builder; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Applies pagination on the Doctrine aggregation for resource collection when enabled. |
|
23
|
|
|
* |
|
24
|
|
|
* @experimental |
|
25
|
|
|
* |
|
26
|
|
|
* @author Kévin Dunglas <[email protected]> |
|
27
|
|
|
* @author Samuel ROZE <[email protected]> |
|
28
|
|
|
* @author Alan Poulain <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
final class PaginationExtension implements AggregationResultCollectionExtensionInterface |
|
31
|
|
|
{ |
|
32
|
|
|
private $managerRegistry; |
|
33
|
|
|
private $pagination; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(ManagerRegistry $managerRegistry, Pagination $pagination) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->managerRegistry = $managerRegistry; |
|
38
|
|
|
$this->pagination = $pagination; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function applyToCollection(Builder $aggregationBuilder, string $resourceClass, string $operationName = null, array &$context = []) |
|
45
|
|
|
{ |
|
46
|
|
|
if (!$this->pagination->isEnabled($resourceClass, $operationName)) { |
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
[, $offset, $limit] = $this->pagination->getPagination($resourceClass, $operationName); |
|
51
|
|
|
|
|
52
|
|
|
$repository = $this->managerRegistry->getManagerForClass($resourceClass)->getRepository($resourceClass); |
|
53
|
|
|
$aggregationBuilder |
|
54
|
|
|
->facet() |
|
55
|
|
|
->field('results')->pipeline( |
|
56
|
|
|
$repository->createAggregationBuilder() |
|
|
|
|
|
|
57
|
|
|
->skip($offset) |
|
58
|
|
|
->limit($limit) |
|
59
|
|
|
) |
|
60
|
|
|
->field('count')->pipeline( |
|
61
|
|
|
$repository->createAggregationBuilder() |
|
62
|
|
|
->count('count') |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function supportsResult(string $resourceClass, string $operationName = null, array $context = []): bool |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->pagination->isEnabled($resourceClass, $operationName); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getResult(Builder $aggregationBuilder, string $resourceClass, string $operationName = null, array $context = []) |
|
78
|
|
|
{ |
|
79
|
|
|
return new Paginator($aggregationBuilder->execute(), $this->managerRegistry->getManagerForClass($resourceClass)->getUnitOfWork(), $resourceClass, $aggregationBuilder->getPipeline()); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|