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

api-platform / core / 17061771168

19 Aug 2025 06:44AM UTC coverage: 0.0%. First build
17061771168

Pull #7299

github

web-flow
Merge 560c166c3 into 6491bfc7a
Pull Request #7299: feat(symfony): stop watch system provider/processor

0 of 36 new or added lines in 10 files covered. (0.0%)

0 of 51906 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/State/Processor/RespondProcessor.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\State\Processor;
15

16
use ApiPlatform\Metadata\Exception\HttpExceptionInterface;
17
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
18
use ApiPlatform\Metadata\Exception\ItemNotFoundException;
19
use ApiPlatform\Metadata\Exception\RuntimeException;
20
use ApiPlatform\Metadata\HttpOperation;
21
use ApiPlatform\Metadata\IriConverterInterface;
22
use ApiPlatform\Metadata\Operation;
23
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
24
use ApiPlatform\Metadata\Put;
25
use ApiPlatform\Metadata\ResourceClassResolverInterface;
26
use ApiPlatform\Metadata\UrlGeneratorInterface;
27
use ApiPlatform\Metadata\Util\ClassInfoTrait;
28
use ApiPlatform\Metadata\Util\CloneTrait;
29
use ApiPlatform\State\ProcessorInterface;
30
use ApiPlatform\State\StopwatchAwareInterface;
31
use ApiPlatform\State\StopwatchAwareTrait;
32
use Symfony\Component\HttpFoundation\Response;
33
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface;
34

35
/**
36
 * Serializes data.
37
 *
38
 * @author Kévin Dunglas <dunglas@gmail.com>
39
 */
40
final class RespondProcessor implements ProcessorInterface, StopwatchAwareInterface
41
{
42
    use ClassInfoTrait;
43
    use CloneTrait;
44
    use StopwatchAwareTrait;
45

46
    public const METHOD_TO_CODE = [
47
        'POST' => Response::HTTP_CREATED,
48
        'DELETE' => Response::HTTP_NO_CONTENT,
49
    ];
50

51
    public function __construct(
52
        private ?IriConverterInterface $iriConverter = null,
53
        private readonly ?ResourceClassResolverInterface $resourceClassResolver = null,
54
        private readonly ?OperationMetadataFactoryInterface $operationMetadataFactory = null,
55
    ) {
56
    }
×
57

58
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
59
    {
60
        if ($data instanceof Response || !$operation instanceof HttpOperation) {
×
61
            return $data;
×
62
        }
63

64
        if (!($request = $context['request'] ?? null)) {
×
65
            return $data;
×
66
        }
67

NEW
68
        $this->stopwatch?->start('api_platform.processor.respond');
×
69

70
        $headers = [
×
71
            'Content-Type' => \sprintf('%s; charset=utf-8', $request->getMimeType($request->getRequestFormat())),
×
72
            'Vary' => 'Accept',
×
73
            'X-Content-Type-Options' => 'nosniff',
×
74
            'X-Frame-Options' => 'deny',
×
75
        ];
×
76

77
        $exception = $request->attributes->get('exception');
×
78
        if (($exception instanceof HttpExceptionInterface || $exception instanceof SymfonyHttpExceptionInterface) && $exceptionHeaders = $exception->getHeaders()) {
×
79
            $headers = array_merge($headers, $exceptionHeaders);
×
80
        }
81

82
        if ($operationHeaders = $operation->getHeaders()) {
×
83
            $headers = array_merge($headers, $operationHeaders);
×
84
        }
85

86
        $status = $operation->getStatus();
×
87

88
        if ($sunset = $operation->getSunset()) {
×
89
            $headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTimeInterface::RFC1123);
×
90
        }
91

92
        if ($acceptPatch = $operation->getAcceptPatch()) {
×
93
            $headers['Accept-Patch'] = $acceptPatch;
×
94
        }
95

96
        $method = $request->getMethod();
×
97
        $originalData = $context['original_data'] ?? null;
×
98

99
        $outputMetadata = $operation->getOutput() ?? ['class' => $operation->getClass()];
×
100
        $hasOutput = \is_array($outputMetadata) && \array_key_exists('class', $outputMetadata) && null !== $outputMetadata['class'];
×
101
        $hasData = !$hasOutput ? false : ($this->resourceClassResolver && $originalData && \is_object($originalData) && $this->resourceClassResolver->isResourceClass($this->getObjectClass($originalData)));
×
102

103
        if ($hasData) {
×
104
            $isAlternateResourceMetadata = $operation->getExtraProperties()['is_alternate_resource_metadata'] ?? false;
×
105
            $canonicalUriTemplate = $operation->getExtraProperties()['canonical_uri_template'] ?? null;
×
106

107
            if (
108
                !isset($headers['Location'])
×
109
                && 300 <= $status && $status < 400
×
110
                && ($isAlternateResourceMetadata || $canonicalUriTemplate)
×
111
            ) {
112
                $canonicalOperation = $operation;
×
113
                if ($this->operationMetadataFactory && null !== $canonicalUriTemplate) {
×
114
                    $canonicalOperation = $this->operationMetadataFactory->create($canonicalUriTemplate, $context);
×
115
                }
116

117
                if ($this->iriConverter) {
×
118
                    $headers['Location'] = $this->iriConverter->getIriFromResource($originalData, UrlGeneratorInterface::ABS_PATH, $canonicalOperation);
×
119
                }
120
            } elseif ('PUT' === $method && !$request->attributes->get('previous_data') && null === $status && ($operation instanceof Put && ($operation->getAllowCreate() ?? false))) {
×
121
                $status = 201;
×
122
            }
123
        }
124

125
        $status ??= self::METHOD_TO_CODE[$method] ?? 200;
×
126

127
        $requestParts = parse_url($request->getRequestUri());
×
128
        if ($this->iriConverter && !isset($headers['Content-Location'])) {
×
129
            try {
130
                $iri = null;
×
131
                if ($hasData) {
×
132
                    $iri = $this->iriConverter->getIriFromResource($originalData);
×
133
                } elseif ($operation->getClass()) {
×
134
                    $iri = $this->iriConverter->getIriFromResource($operation->getClass(), UrlGeneratorInterface::ABS_PATH, $operation);
×
135
                }
136

137
                if ($iri && 'GET' !== $method) {
×
138
                    $location = \sprintf('%s.%s', $iri, $request->getRequestFormat());
×
139
                    if (isset($requestParts['query'])) {
×
140
                        $location .= '?'.$requestParts['query'];
×
141
                    }
142

143
                    $headers['Content-Location'] = $location;
×
144
                    if ((201 === $status || (300 <= $status && $status < 400)) && 'POST' === $method && !isset($headers['Location'])) {
×
145
                        $headers['Location'] = $iri;
×
146
                    }
147
                }
148
            } catch (InvalidArgumentException|ItemNotFoundException|RuntimeException) {
×
149
            }
150
        }
151

NEW
152
        $this->stopwatch?->stop('api_platform.processor.respond');
×
153

154
        return new Response(
×
155
            $data,
×
156
            $status,
×
157
            $headers
×
158
        );
×
159
    }
160
}
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