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

api-platform / core / 7582727293

19 Jan 2024 10:45AM UTC coverage: 61.96% (+2.8%) from 59.207%
7582727293

push

github

web-flow
feat(symfony): request and view kernel listeners (#6102)

133 of 266 new or added lines in 19 files covered. (50.0%)

447 existing lines in 32 files now uncovered.

17435 of 28139 relevant lines covered (61.96%)

32.39 hits per line

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

59.74
/src/Symfony/EventListener/SerializeListener.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\Symfony\EventListener;
15

16
use ApiPlatform\Doctrine\Odm\State\Options as ODMOptions;
17
use ApiPlatform\Doctrine\Orm\State\Options;
18
use ApiPlatform\Exception\RuntimeException;
19
use ApiPlatform\Metadata\Error;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Serializer\ResourceList;
22
use ApiPlatform\Serializer\SerializerContextBuilderInterface;
23
use ApiPlatform\State\ProcessorInterface;
24
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
25
use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
26
use ApiPlatform\Util\ErrorFormatGuesser;
27
use ApiPlatform\Validator\Exception\ValidationException;
28
use Symfony\Component\HttpFoundation\Request;
29
use Symfony\Component\HttpFoundation\Response;
30
use Symfony\Component\HttpKernel\Event\ViewEvent;
31
use Symfony\Component\Serializer\Encoder\EncoderInterface;
32
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
33
use Symfony\Component\Serializer\SerializerInterface;
34
use Symfony\Component\WebLink\GenericLinkProvider;
35
use Symfony\Component\WebLink\Link;
36

37
/**
38
 * Serializes data.
39
 *
40
 * @author Kévin Dunglas <dunglas@gmail.com>
41
 */
42
final class SerializeListener
43
{
44
    use OperationRequestInitiatorTrait;
45

46
    public const OPERATION_ATTRIBUTE_KEY = 'serialize';
47
    private ?SerializerInterface $serializer = null;
48
    private ?ProcessorInterface $processor = null;
49
    private ?SerializerContextBuilderInterface $serializerContextBuilder = null;
50

51
    public function __construct(
52
        SerializerInterface|ProcessorInterface $serializer,
53
        SerializerContextBuilderInterface|ResourceMetadataCollectionFactoryInterface $serializerContextBuilder = null,
54
        ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null,
55
        private readonly array $errorFormats = [],
56
        // @phpstan-ignore-next-line we don't need this anymore
57
        private readonly bool $debug = false,
58
    ) {
59
        if ($serializer instanceof ProcessorInterface) {
28✔
NEW
60
            $this->processor = $serializer;
×
61
        } else {
62
            $this->serializer = $serializer;
28✔
63
            trigger_deprecation('api-platform/core', '3.3', 'Use a "%s" as first argument in "%s" instead of "%s".', ProcessorInterface::class, self::class, SerializerInterface::class);
28✔
64
        }
65

66
        if ($serializerContextBuilder instanceof ResourceMetadataCollectionFactoryInterface) {
28✔
NEW
67
            $resourceMetadataFactory = $serializerContextBuilder;
×
68
        } else {
69
            $this->serializerContextBuilder = $serializerContextBuilder;
28✔
70
            trigger_deprecation('api-platform/core', '3.3', 'Use a "%s" as second argument in "%s" instead of "%s".', ResourceMetadataCollectionFactoryInterface::class, self::class, SerializerContextBuilderInterface::class);
28✔
71
        }
72

73
        $this->resourceMetadataCollectionFactory = $resourceMetadataFactory;
28✔
74
    }
75

76
    /**
77
     * Serializes the data to the requested format.
78
     */
79
    public function onKernelView(ViewEvent $event): void
80
    {
81
        $controllerResult = $event->getControllerResult();
28✔
82
        $request = $event->getRequest();
28✔
83
        $operation = $this->initializeOperation($request);
28✔
84

85
        $attributes = RequestAttributesExtractor::extractAttributes($request);
28✔
86

87
        if (!($attributes['respond'] ?? $request->attributes->getBoolean('_api_respond', false))) {
28✔
88
            return;
8✔
89
        }
90

91
        if ($operation && $this->processor instanceof ProcessorInterface) {
20✔
NEW
92
            if (null === $operation->canSerialize()) {
×
NEW
93
                $operation = $operation->withSerialize(true);
×
94
            }
95

NEW
96
            if ($operation instanceof Error) {
×
97
                // we don't want the FlattenException
NEW
98
                $controllerResult = $request->attributes->get('data') ?? $controllerResult;
×
99
            }
100

NEW
101
            $uriVariables = $request->attributes->get('_api_uri_variables') ?? [];
×
NEW
102
            $serialized = $this->processor->process($controllerResult, $operation, $uriVariables, [
×
NEW
103
                'request' => $request,
×
NEW
104
                'uri_variables' => $uriVariables,
×
NEW
105
                'resource_class' => $operation->getClass(),
×
NEW
106
            ]);
×
107

NEW
108
            $event->setControllerResult($serialized);
×
109

NEW
110
            return;
×
111
        }
112

113
        // TODO: the code below needs to be removed in 4.x
114
        if ($controllerResult instanceof Response) {
20✔
115
            return;
×
116
        }
117

118
        $attributes = RequestAttributesExtractor::extractAttributes($request);
20✔
119

120
        if (!($attributes['respond'] ?? $request->attributes->getBoolean('_api_respond', false))) {
20✔
UNCOV
121
            return;
×
122
        }
123

124
        if ('api_platform.symfony.main_controller' === $operation?->getController() || $request->attributes->get('_api_platform_disable_listeners')) {
20✔
UNCOV
125
            return;
×
126
        }
127

128
        if (!($operation?->canSerialize() ?? true)) {
20✔
129
            return;
4✔
130
        }
131

132
        if (!$attributes) {
16✔
133
            $this->serializeRawData($event, $request, $controllerResult);
4✔
134

135
            return;
4✔
136
        }
137

138
        $context = $this->serializerContextBuilder->createFromRequest($request, true, $attributes);
12✔
139
        if (isset($context['output']) && \array_key_exists('class', $context['output']) && null === $context['output']['class']) {
12✔
140
            $event->setControllerResult(null);
4✔
141

142
            return;
4✔
143
        }
144

145
        if ($controllerResult instanceof ValidationException) {
8✔
146
            $format = ErrorFormatGuesser::guessErrorFormat($request, $this->errorFormats);
×
147
            $previousOperation = $request->attributes->get('_api_previous_operation');
×
148
            if (!($previousOperation?->getExtraProperties()['rfc_7807_compliant_errors'] ?? false)) {
×
149
                $context['groups'] = ['legacy_'.$format['key']];
×
150
                $context['force_iri_generation'] = false;
×
151
            }
152
        }
153

154
        if ($included = $request->attributes->get('_api_included')) {
8✔
155
            $context['api_included'] = $included;
×
156
        }
157
        $resources = new ResourceList();
8✔
158
        $context['resources'] = &$resources;
8✔
159
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'resources';
8✔
160

161
        $resourcesToPush = new ResourceList();
8✔
162
        $context['resources_to_push'] = &$resourcesToPush;
8✔
163
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'resources_to_push';
8✔
164
        if (($options = $operation?->getStateOptions()) && (
8✔
165
            ($options instanceof Options && $options->getEntityClass())
8✔
166
            || ($options instanceof ODMOptions && $options->getDocumentClass())
8✔
167
        )) {
168
            $context['force_resource_class'] = $operation->getClass();
×
169
        }
170

171
        $request->attributes->set('_api_normalization_context', $context);
8✔
172
        $event->setControllerResult($this->serializer->serialize($controllerResult, $request->getRequestFormat(), $context));
8✔
173

174
        $request->attributes->set('_resources', $request->attributes->get('_resources', []) + (array) $resources);
8✔
175
        if (!\count($resourcesToPush)) {
8✔
176
            return;
8✔
177
        }
178

179
        $linkProvider = $request->attributes->get('_api_platform_links', new GenericLinkProvider());
×
180
        foreach ($resourcesToPush as $resourceToPush) {
×
181
            $linkProvider = $linkProvider->withLink((new Link('preload', $resourceToPush))->withAttribute('as', 'fetch'));
×
182
        }
183
        $request->attributes->set('_api_platform_links', $linkProvider);
×
184
    }
185

186
    /**
187
     * Tries to serialize data that are not API resources (e.g. the entrypoint or data returned by a custom controller).
188
     *
189
     * @throws RuntimeException
190
     */
191
    private function serializeRawData(ViewEvent $event, Request $request, $controllerResult): void
192
    {
193
        if (\is_object($controllerResult)) {
4✔
194
            $event->setControllerResult($this->serializer->serialize($controllerResult, $request->getRequestFormat(), $request->attributes->get('_api_normalization_context', [])));
×
195

196
            return;
×
197
        }
198

199
        if (!$this->serializer instanceof EncoderInterface) {
4✔
200
            throw new RuntimeException(sprintf('The serializer must implement the "%s" interface.', EncoderInterface::class));
×
201
        }
202

203
        $event->setControllerResult($this->serializer->encode($controllerResult, $request->getRequestFormat()));
4✔
204
    }
205
}
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