|
| 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\MongoDB\Extension; |
| 15 | + |
| 16 | +use ApiPlatform\Doctrine\Odm\Extension\AggregationResultCollectionExtensionInterface; |
| 17 | +use ApiPlatform\Doctrine\Odm\Paginator; |
| 18 | +use ApiPlatform\Metadata\Exception\RuntimeException; |
| 19 | +use ApiPlatform\Metadata\Operation; |
| 20 | +use ApiPlatform\State\Pagination\Pagination; |
| 21 | +use Doctrine\ODM\MongoDB\Aggregation\Builder; |
| 22 | +use Doctrine\ODM\MongoDB\DocumentManager; |
| 23 | +use Doctrine\ODM\MongoDB\Repository\DocumentRepository; |
| 24 | +use Doctrine\Persistence\ManagerRegistry; |
| 25 | + |
| 26 | +/** |
| 27 | + * Applies pagination on the Doctrine aggregation for resource collection when enabled. |
| 28 | + * |
| 29 | + * @author Kévin Dunglas <[email protected]> |
| 30 | + * @author Samuel ROZE <[email protected]> |
| 31 | + * @author Alan Poulain <[email protected]> |
| 32 | + */ |
| 33 | +final class PaginationExtension implements AggregationResultCollectionExtensionInterface |
| 34 | +{ |
| 35 | + public function __construct(private readonly ManagerRegistry $managerRegistry, private readonly Pagination $pagination) |
| 36 | + { |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * {@inheritdoc} |
| 41 | + * |
| 42 | + * @throws RuntimeException |
| 43 | + */ |
| 44 | + public function applyToCollection(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void |
| 45 | + { |
| 46 | + if (!$this->pagination->isEnabled($operation, $context)) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + if (($context['graphql_operation_name'] ?? false) && !$this->pagination->isGraphQlEnabled($operation, $context)) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + $context = $this->addCountToContext(clone $aggregationBuilder, $context); |
| 55 | + |
| 56 | + [, $offset, $limit] = $this->pagination->getPagination($operation, $context); |
| 57 | + |
| 58 | + $manager = $this->managerRegistry->getManagerForClass($resourceClass); |
| 59 | + if (!$manager instanceof DocumentManager) { |
| 60 | + throw new RuntimeException(\sprintf('The manager for "%s" must be an instance of "%s".', $resourceClass, DocumentManager::class)); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @var DocumentRepository |
| 65 | + */ |
| 66 | + $repository = $manager->getRepository($resourceClass); |
| 67 | + $resultsAggregationBuilder = $repository->createAggregationBuilder()->skip($offset); |
| 68 | + if ($limit > 0) { |
| 69 | + $resultsAggregationBuilder->limit($limit); |
| 70 | + } else { |
| 71 | + // Results have to be 0 but MongoDB does not support a limit equal to 0. |
| 72 | + $resultsAggregationBuilder->match()->field(Paginator::LIMIT_ZERO_MARKER_FIELD)->equals(Paginator::LIMIT_ZERO_MARKER); |
| 73 | + } |
| 74 | + |
| 75 | + $aggregationBuilder |
| 76 | + ->facet() |
| 77 | + ->field('results')->pipeline( |
| 78 | + $resultsAggregationBuilder |
| 79 | + ) |
| 80 | + ->field('count')->pipeline( |
| 81 | + $repository->createAggregationBuilder() |
| 82 | + ->count('count') |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * {@inheritdoc} |
| 88 | + */ |
| 89 | + public function supportsResult(string $resourceClass, ?Operation $operation = null, array $context = []): bool |
| 90 | + { |
| 91 | + if ($context['graphql_operation_name'] ?? false) { |
| 92 | + return $this->pagination->isGraphQlEnabled($operation, $context); |
| 93 | + } |
| 94 | + |
| 95 | + return $this->pagination->isEnabled($operation, $context); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * {@inheritdoc} |
| 100 | + * |
| 101 | + * @throws RuntimeException |
| 102 | + */ |
| 103 | + public function getResult(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array $context = []): iterable |
| 104 | + { |
| 105 | + $manager = $this->managerRegistry->getManagerForClass($resourceClass); |
| 106 | + if (!$manager instanceof DocumentManager) { |
| 107 | + throw new RuntimeException(\sprintf('The manager for "%s" must be an instance of "%s".', $resourceClass, DocumentManager::class)); |
| 108 | + } |
| 109 | + |
| 110 | + $attribute = $operation?->getExtraProperties()['doctrine_mongodb'] ?? []; |
| 111 | + $executeOptions = $attribute['execute_options'] ?? []; |
| 112 | + |
| 113 | + return new Paginator($aggregationBuilder->execute($executeOptions), $manager->getUnitOfWork(), $resourceClass, $aggregationBuilder->getPipeline()); |
| 114 | + } |
| 115 | + |
| 116 | + private function addCountToContext(Builder $aggregationBuilder, array $context): array |
| 117 | + { |
| 118 | + if (!($context['graphql_operation_name'] ?? false)) { |
| 119 | + return $context; |
| 120 | + } |
| 121 | + |
| 122 | + if (isset($context['filters']['last']) && !isset($context['filters']['before'])) { |
| 123 | + $context['count'] = $aggregationBuilder->count('count')->execute()->toArray()[0]['count']; |
| 124 | + } |
| 125 | + |
| 126 | + return $context; |
| 127 | + } |
| 128 | +} |
0 commit comments