• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 17581213192

09 Sep 2025 11:31AM UTC coverage: 0.0% (-22.6%) from 22.604%
17581213192

Pull #7374

github

web-flow
Merge 0f1360e1a into 6db55be8c
Pull Request #7374: fix(jsonld): various json streamer fixes

0 of 34 new or added lines in 2 files covered. (0.0%)

12081 existing lines in 401 files now uncovered.

0 of 52792 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Hydra/State/JsonStreamerProcessor.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\State\Util\PaginationHelperTrait;
18
use ApiPlatform\Hydra\State\Util\SearchHelperTrait;
19
use ApiPlatform\Metadata\CollectionOperationInterface;
20
use ApiPlatform\Metadata\Error;
21
use ApiPlatform\Metadata\HttpOperation;
22
use ApiPlatform\Metadata\IriConverterInterface;
23
use ApiPlatform\Metadata\Operation;
24
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
25
use ApiPlatform\Metadata\ResourceClassResolverInterface;
26
use ApiPlatform\Metadata\UrlGeneratorInterface;
27
use ApiPlatform\State\Pagination\PaginatorInterface;
28
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
29
use ApiPlatform\State\ProcessorInterface;
30
use ApiPlatform\State\Util\HttpResponseHeadersTrait;
31
use ApiPlatform\State\Util\HttpResponseStatusTrait;
32
use Symfony\Component\HttpFoundation\Response;
33
use Symfony\Component\HttpFoundation\StreamedResponse;
34
use Symfony\Component\JsonStreamer\StreamWriterInterface;
35
use Symfony\Component\TypeInfo\Type;
36

37
/**
38
 * @implements ProcessorInterface<mixed,mixed>
39
 */
40
final class JsonStreamerProcessor implements ProcessorInterface
41
{
42
    use HttpResponseHeadersTrait;
43
    use HttpResponseStatusTrait;
44
    use PaginationHelperTrait;
45
    use SearchHelperTrait;
46

47
    /**
48
     * @param ProcessorInterface<mixed,mixed>|null       $processor
49
     * @param StreamWriterInterface<array<string,mixed>> $jsonStreamer
50
     */
51
    public function __construct(
52
        private readonly ?ProcessorInterface $processor,
53
        private readonly StreamWriterInterface $jsonStreamer,
54
        ?IriConverterInterface $iriConverter = null,
55
        ?ResourceClassResolverInterface $resourceClassResolver = null,
56
        ?OperationMetadataFactoryInterface $operationMetadataFactory = null,
57
        private readonly string $pageParameterName = 'page',
58
        private readonly string $enabledParameterName = 'pagination',
59
        private readonly int $urlGenerationStrategy = UrlGeneratorInterface::ABS_PATH,
60
    ) {
UNCOV
61
        $this->resourceClassResolver = $resourceClassResolver;
×
UNCOV
62
        $this->iriConverter = $iriConverter;
×
UNCOV
63
        $this->operationMetadataFactory = $operationMetadataFactory;
×
64
    }
65

66
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
67
    {
68
        if (
UNCOV
69
            $operation instanceof Error
×
UNCOV
70
            || $data instanceof Response
×
UNCOV
71
            || !$operation instanceof HttpOperation
×
UNCOV
72
            || !($request = $context['request'] ?? null)
×
UNCOV
73
            || !$operation->getJsonStream()
×
UNCOV
74
            || 'jsonld' !== $request->getRequestFormat()
×
75
        ) {
UNCOV
76
            return $this->processor?->process($data, $operation, $uriVariables, $context);
×
77
        }
78

UNCOV
79
        if ($operation instanceof CollectionOperationInterface) {
×
UNCOV
80
            $requestUri = $request->getRequestUri() ?? '';
×
UNCOV
81
            $collection = new Collection();
×
UNCOV
82
            $collection->member = $data;
×
UNCOV
83
            $collection->view = $this->getPartialCollectionView($data, $requestUri, $this->pageParameterName, $this->enabledParameterName, $operation->getUrlGenerationStrategy() ?? $this->urlGenerationStrategy);
×
84

UNCOV
85
            if ($operation->getParameters()) {
×
86
                $parts = parse_url($requestUri);
×
87
                $collection->search = $this->getSearch($parts['path'] ?? '', $operation);
×
88
            }
89

UNCOV
90
            if ($data instanceof PaginatorInterface) {
×
91
                $collection->totalItems = $data->getTotalItems();
×
92
            }
93

UNCOV
94
            if (\is_array($data) || ($data instanceof \Countable && !$data instanceof PartialPaginatorInterface)) {
×
UNCOV
95
                $collection->totalItems = \count($data);
×
96
            }
97

UNCOV
98
            $data = $this->jsonStreamer->write(
×
UNCOV
99
                $collection,
×
UNCOV
100
                Type::generic(Type::object($collection::class), Type::object($operation->getClass())),
×
UNCOV
101
                ['data' => $data, 'operation' => $operation],
×
UNCOV
102
            );
×
103
        } else {
UNCOV
104
            $data = $this->jsonStreamer->write(
×
UNCOV
105
                $data,
×
UNCOV
106
                Type::object($operation->getClass()),
×
UNCOV
107
                ['data' => $data, 'operation' => $operation],
×
UNCOV
108
            );
×
109
        }
110

111
        /** @var iterable<string> $data */
UNCOV
112
        $response = new StreamedResponse(
×
UNCOV
113
            $data,
×
UNCOV
114
            $this->getStatus($request, $operation, $context),
×
UNCOV
115
            $this->getHeaders($request, $operation, $context)
×
UNCOV
116
        );
×
117

UNCOV
118
        return $this->processor ? $this->processor->process($response, $operation, $uriVariables, $context) : $response;
×
119
    }
120
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc