-
-
Notifications
You must be signed in to change notification settings - Fork 963
Expand file tree
/
Copy pathJsonStreamerProcessor.php
More file actions
121 lines (107 loc) · 4.78 KB
/
JsonStreamerProcessor.php
File metadata and controls
121 lines (107 loc) · 4.78 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
<?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\Hydra\State;
use ApiPlatform\Hydra\Collection;
use ApiPlatform\Hydra\State\Util\PaginationHelperTrait;
use ApiPlatform\Hydra\State\Util\SearchHelperTrait;
use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Error;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\IriConverterInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\Metadata\UrlGeneratorInterface;
use ApiPlatform\State\Pagination\PaginatorInterface;
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\State\Util\HttpResponseHeadersTrait;
use ApiPlatform\State\Util\HttpResponseStatusTrait;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\JsonStreamer\StreamWriterInterface;
use Symfony\Component\TypeInfo\Type;
use Uri\Rfc3986\Uri;
/**
* @implements ProcessorInterface<mixed,mixed>
*/
final class JsonStreamerProcessor implements ProcessorInterface
{
use HttpResponseHeadersTrait;
use HttpResponseStatusTrait;
use PaginationHelperTrait;
use SearchHelperTrait;
/**
* @param ProcessorInterface<mixed,mixed>|null $processor
* @param StreamWriterInterface<array<string,mixed>> $jsonStreamer
*/
public function __construct(
private readonly ?ProcessorInterface $processor,
private readonly StreamWriterInterface $jsonStreamer,
?IriConverterInterface $iriConverter = null,
?ResourceClassResolverInterface $resourceClassResolver = null,
?OperationMetadataFactoryInterface $operationMetadataFactory = null,
private readonly string $pageParameterName = 'page',
private readonly string $enabledParameterName = 'pagination',
private readonly int $urlGenerationStrategy = UrlGeneratorInterface::ABS_PATH,
) {
$this->resourceClassResolver = $resourceClassResolver;
$this->iriConverter = $iriConverter;
$this->operationMetadataFactory = $operationMetadataFactory;
}
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
{
if (
$operation instanceof Error
|| $data instanceof Response
|| !$operation instanceof HttpOperation
|| !($request = $context['request'] ?? null)
|| !$operation->getJsonStream()
|| 'jsonld' !== $request->getRequestFormat()
) {
return $this->processor?->process($data, $operation, $uriVariables, $context);
}
if ($operation instanceof CollectionOperationInterface) {
$requestUri = $request->getRequestUri() ?? '';
$collection = new Collection();
$collection->member = $data;
$collection->view = $this->getPartialCollectionView($data, $requestUri, $this->pageParameterName, $this->enabledParameterName, $operation->getUrlGenerationStrategy() ?? $this->urlGenerationStrategy);
if ($operation->getParameters()) {
$path = PHP_VERSION_ID >= 80500 && \class_exists(Uri::class) ? Uri::parse($requestUri)?->getPath() : ($parts['path'] ?? '');
$collection->search = $this->getSearch($path, $operation);
}
if ($data instanceof PaginatorInterface) {
$collection->totalItems = $data->getTotalItems();
}
if (\is_array($data) || ($data instanceof \Countable && !$data instanceof PartialPaginatorInterface)) {
$collection->totalItems = \count($data);
}
$data = $this->jsonStreamer->write(
$collection,
Type::generic(Type::object($collection::class), Type::object($operation->getClass())),
['data' => $data, 'operation' => $operation],
);
} else {
$data = $this->jsonStreamer->write(
$data,
Type::object($operation->getClass()),
['data' => $data, 'operation' => $operation],
);
}
/** @var iterable<string> $data */
$response = new StreamedResponse(
$data,
$this->getStatus($request, $operation, $context),
$this->getHeaders($request, $operation, $context)
);
return $this->processor ? $this->processor->process($response, $operation, $uriVariables, $context) : $response;
}
}