|
| 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\Hydra\State; |
| 15 | + |
| 16 | +use ApiPlatform\Hydra\Collection; |
| 17 | +use ApiPlatform\Hydra\IriTemplate; |
| 18 | +use ApiPlatform\Hydra\IriTemplateMapping; |
| 19 | +use ApiPlatform\Hydra\PartialCollectionView; |
| 20 | +use ApiPlatform\Metadata\CollectionOperationInterface; |
| 21 | +use ApiPlatform\Metadata\Error; |
| 22 | +use ApiPlatform\Metadata\Operation; |
| 23 | +use ApiPlatform\Metadata\QueryParameterInterface; |
| 24 | +use ApiPlatform\Metadata\UrlGeneratorInterface; |
| 25 | +use ApiPlatform\Metadata\Util\IriHelper; |
| 26 | +use ApiPlatform\State\Pagination\PaginatorInterface; |
| 27 | +use ApiPlatform\State\Pagination\PartialPaginatorInterface; |
| 28 | +use ApiPlatform\State\ProcessorInterface; |
| 29 | +use Symfony\Component\HttpFoundation\Response; |
| 30 | +use Symfony\Component\HttpFoundation\StreamedResponse; |
| 31 | +use Symfony\Component\JsonStreamer\StreamWriterInterface; |
| 32 | +use Symfony\Component\TypeInfo\Type; |
| 33 | + |
| 34 | +final class JsonStreamerProcessor implements ProcessorInterface |
| 35 | +{ |
| 36 | + public function __construct( |
| 37 | + private readonly ProcessorInterface $processor, |
| 38 | + private readonly StreamWriterInterface $jsonStreamer, |
| 39 | + private readonly string $pageParameterName = 'page', |
| 40 | + private readonly string $enabledParameterName = 'pagination', |
| 41 | + private readonly int $urlGenerationStrategy = UrlGeneratorInterface::ABS_PATH |
| 42 | + ) { |
| 43 | + } |
| 44 | + |
| 45 | + private function getSearch(Operation $operation, string $requestUri): IriTemplate |
| 46 | + { |
| 47 | + /** @var list<IriTemplateMapping> */ |
| 48 | + $mapping = []; |
| 49 | + $keys = []; |
| 50 | + |
| 51 | + foreach ($operation->getParameters() ?? [] as $key => $parameter) { |
| 52 | + if (!$parameter instanceof QueryParameterInterface || false === $parameter->getHydra()) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + if (!($property = $parameter->getProperty())) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + $keys[] = $key; |
| 61 | + $m = new IriTemplateMapping( |
| 62 | + variable: $key, |
| 63 | + property: $property, |
| 64 | + required: $parameter->getRequired() ?? false |
| 65 | + ); |
| 66 | + $mapping[] = $m; |
| 67 | + } |
| 68 | + |
| 69 | + $parts = parse_url($requestUri); |
| 70 | + return new IriTemplate( |
| 71 | + variableRepresentation: 'BasicRepresentation', |
| 72 | + mapping: $mapping, |
| 73 | + template: \sprintf('%s{?%s}', $parts['path'] ?? '', implode(',', $keys)), |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + private function getView(mixed $object, string $requestUri, Operation $operation): PartialCollectionView |
| 78 | + { |
| 79 | + $currentPage = $lastPage = $itemsPerPage = $pageTotalItems = null; |
| 80 | + if ($paginated = ($object instanceof PartialPaginatorInterface)) { |
| 81 | + if ($object instanceof PaginatorInterface) { |
| 82 | + $paginated = 1. !== $lastPage = $object->getLastPage(); |
| 83 | + } else { |
| 84 | + $itemsPerPage = $object->getItemsPerPage(); |
| 85 | + $pageTotalItems = (float) \count($object); |
| 86 | + } |
| 87 | + |
| 88 | + $currentPage = $object->getCurrentPage(); |
| 89 | + } |
| 90 | + |
| 91 | + // TODO: This needs to be changed as well as I wrote in the CollectionFiltersNormalizer |
| 92 | + // We should not rely on the request_uri but instead rely on the UriTemplate |
| 93 | + // This needs that we implement the RFC and that we do more parsing before calling the serialization (MainController) |
| 94 | + $parsed = IriHelper::parseIri($requestUri ?? '/', $this->pageParameterName); |
| 95 | + $appliedFilters = $parsed['parameters']; |
| 96 | + unset($appliedFilters[$this->enabledParameterName]); |
| 97 | + |
| 98 | + $urlGenerationStrategy = $operation?->getUrlGenerationStrategy() ?? $this->urlGenerationStrategy; |
| 99 | + $id = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $paginated ? $currentPage : null, $urlGenerationStrategy); |
| 100 | + if (!$appliedFilters && !$paginated) { |
| 101 | + return new PartialCollectionView($id); |
| 102 | + } |
| 103 | + |
| 104 | + $first = $last = $previous = $next = null; |
| 105 | + if (null !== $lastPage) { |
| 106 | + $first = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, 1., $urlGenerationStrategy); |
| 107 | + $last = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $lastPage, $urlGenerationStrategy); |
| 108 | + } |
| 109 | + |
| 110 | + if (1. !== $currentPage) { |
| 111 | + $previous = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage - 1., $urlGenerationStrategy); |
| 112 | + } |
| 113 | + |
| 114 | + if ((null !== $lastPage && $currentPage < $lastPage) || (null === $lastPage && $pageTotalItems >= $itemsPerPage)) { |
| 115 | + $next = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage + 1., $urlGenerationStrategy); |
| 116 | + } |
| 117 | + |
| 118 | + return new PartialCollectionView($id, $first, $last, $previous, $next); |
| 119 | + } |
| 120 | + |
| 121 | + public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []) |
| 122 | + { |
| 123 | + if ($context['request']->query->has('skip_json_stream')) { |
| 124 | + return $this->processor->process($data, $operation, $uriVariables, $context); |
| 125 | + } |
| 126 | + |
| 127 | + if ($operation instanceof Error || $data instanceof Response) { |
| 128 | + return $this->processor->process($data, $operation, $uriVariables, $context); |
| 129 | + } |
| 130 | + |
| 131 | + if ($operation instanceof CollectionOperationInterface) { |
| 132 | + $requestUri = $context['request']->getRequestUri() ?? ''; |
| 133 | + $collection = new Collection(); |
| 134 | + $collection->member = $data; |
| 135 | + $collection->view = $this->getView($data, $requestUri, $operation); |
| 136 | + |
| 137 | + if ($operation->getParameters()) { |
| 138 | + $collection->search = $this->getSearch($operation, $requestUri); |
| 139 | + } |
| 140 | + |
| 141 | + if ($data instanceof PaginatorInterface) { |
| 142 | + $collection->totalItems = $data->getTotalItems(); |
| 143 | + } |
| 144 | + |
| 145 | + if (\is_array($data) || ($data instanceof \Countable && !$data instanceof PartialPaginatorInterface)) { |
| 146 | + $collection->totalItems = \count($data); |
| 147 | + } |
| 148 | + |
| 149 | + $response = new StreamedResponse($this->jsonStreamer->write($collection, Type::generic(Type::object($collection::class), Type::object($operation->getClass())), [ |
| 150 | + 'data' => $data, |
| 151 | + 'operation' => $operation, |
| 152 | + ])); |
| 153 | + } else { |
| 154 | + $response = new StreamedResponse($this->jsonStreamer->write($data, Type::object($operation->getClass()), [ |
| 155 | + 'data' => $data, |
| 156 | + 'operation' => $operation, |
| 157 | + ])); |
| 158 | + } |
| 159 | + |
| 160 | + return $this->processor->process($response, $operation, $uriVariables, $context); |
| 161 | + } |
| 162 | +} |
0 commit comments