• 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

68.57
/src/Symfony/EventListener/WriteListener.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\Api\IriConverterInterface as LegacyIriConverterInterface;
17
use ApiPlatform\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
18
use ApiPlatform\Api\UriVariablesConverterInterface as LegacyUriVariablesConverterInterface;
19
use ApiPlatform\Exception\InvalidIdentifierException;
20
use ApiPlatform\Metadata\Error;
21
use ApiPlatform\Metadata\Exception\InvalidUriVariableException;
22
use ApiPlatform\Metadata\HttpOperation;
23
use ApiPlatform\Metadata\IriConverterInterface;
24
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
25
use ApiPlatform\Metadata\ResourceClassResolverInterface;
26
use ApiPlatform\Metadata\UriVariablesConverterInterface;
27
use ApiPlatform\Metadata\Util\ClassInfoTrait;
28
use ApiPlatform\Metadata\Util\CloneTrait;
29
use ApiPlatform\State\CallableProcessor;
30
use ApiPlatform\State\Processor\WriteProcessor;
31
use ApiPlatform\State\ProcessorInterface;
32
use ApiPlatform\State\UriVariablesResolverTrait;
33
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
34
use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
35
use Symfony\Component\HttpFoundation\Response;
36
use Symfony\Component\HttpKernel\Event\ViewEvent;
37
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
38

39
/**
40
 * Bridges persistence and the API system.
41
 *
42
 * @author Kévin Dunglas <dunglas@gmail.com>
43
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
44
 */
45
final class WriteListener
46
{
47
    use ClassInfoTrait;
48
    use CloneTrait;
49
    use OperationRequestInitiatorTrait;
50
    use UriVariablesResolverTrait;
51

52
    private LegacyIriConverterInterface|IriConverterInterface|null $iriConverter = null;
53

54
    /**
55
     * @param ProcessorInterface<mixed, mixed> $processor
56
     */
57
    public function __construct(
58
        private readonly ProcessorInterface $processor,
59
        LegacyIriConverterInterface|IriConverterInterface|ResourceMetadataCollectionFactoryInterface $iriConverter = null,
60
        private readonly null|ResourceClassResolverInterface|LegacyResourceClassResolverInterface $resourceClassResolver = null,
61
        ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null,
62
        LegacyUriVariablesConverterInterface|UriVariablesConverterInterface $uriVariablesConverter = null,
63
    ) {
64
        $this->uriVariablesConverter = $uriVariablesConverter;
32✔
65

66
        if ($processor instanceof CallableProcessor) {
32✔
NEW
67
            trigger_deprecation('api-platform/core', '3.3', 'Use a "%s" as first argument in "%s" instead of "%s".', WriteProcessor::class, self::class, $processor::class);
×
68
        }
69

70
        if ($iriConverter instanceof ResourceMetadataCollectionFactoryInterface) {
32✔
NEW
71
            $resourceMetadataCollectionFactory = $iriConverter;
×
72
        } else {
73
            $this->iriConverter = $iriConverter;
32✔
74
            trigger_deprecation('api-platform/core', '3.3', 'Use a "%s" as second argument in "%s" instead of "%s".', ResourceMetadataCollectionFactoryInterface::class, self::class, IriConverterInterface::class);
32✔
75
        }
76

77
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
32✔
78
    }
79

80
    /**
81
     * Persists, updates or delete data return by the controller if applicable.
82
     */
83
    public function onKernelView(ViewEvent $event): void
84
    {
85
        $controllerResult = $event->getControllerResult();
32✔
86
        $request = $event->getRequest();
32✔
87
        $operation = $this->initializeOperation($request);
32✔
88

89
        if (!($attributes = RequestAttributesExtractor::extractAttributes($request)) || !$attributes['persist']) {
32✔
90
            return;
8✔
91
        }
92

93
        if ($operation && (!$this->processor instanceof CallableProcessor && !$this->iriConverter)) {
24✔
NEW
94
            if (null === $operation->canWrite()) {
×
NEW
95
                $operation = $operation->withWrite(!$request->isMethodSafe());
×
96
            }
97

NEW
98
            $uriVariables = $request->attributes->get('_api_uri_variables') ?? [];
×
NEW
99
            if (!$uriVariables && !$operation instanceof Error && $operation instanceof HttpOperation) {
×
100
                try {
NEW
101
                    $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
×
NEW
102
                } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
×
NEW
103
                    throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
×
104
                }
105
            }
106

107
            // $request->attributes->set('original_data', $this->clone($controllerResult));
NEW
108
            $data = $this->processor->process($controllerResult, $operation, $uriVariables, [
×
NEW
109
                'request' => $request,
×
NEW
110
                'uri_variables' => $uriVariables,
×
NEW
111
                'resource_class' => $operation->getClass(),
×
NEW
112
                'previous_data' => false === $operation->canRead() ? null : $request->attributes->get('previous_data'),
×
NEW
113
            ]);
×
114

NEW
115
            if ($data) {
×
NEW
116
                $request->attributes->set('original_data', $this->clone($data));
×
117
            }
118

NEW
119
            $event->setControllerResult($data);
×
120

NEW
121
            return;
×
122
        }
123

124
        // API Platform 3.2 has a MainController where everything is handled by processors/providers
125
        if ('api_platform.symfony.main_controller' === $operation?->getController() || $request->attributes->get('_api_platform_disable_listeners')) {
24✔
126
            return;
×
127
        }
128

129
        if (
130
            $controllerResult instanceof Response
24✔
131
            || $request->isMethodSafe()
24✔
132
            || !($attributes = RequestAttributesExtractor::extractAttributes($request))
24✔
133
        ) {
134
            return;
4✔
135
        }
136

137
        if (!$attributes['persist'] || !($operation?->canWrite() ?? true)) {
20✔
138
            return;
4✔
139
        }
140

141
        if (!$operation?->getProcessor()) {
16✔
142
            return;
×
143
        }
144

145
        $context = [
16✔
146
            'operation' => $operation,
16✔
147
            'resource_class' => $attributes['resource_class'],
16✔
148
            'previous_data' => $attributes['previous_data'] ?? null,
16✔
149
        ];
16✔
150

151
        try {
152
            $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $attributes['resource_class']);
16✔
153
        } catch (InvalidIdentifierException $e) {
4✔
154
            throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
4✔
155
        }
156

157
        switch ($request->getMethod()) {
12✔
158
            case 'PUT':
12✔
159
            case 'PATCH':
12✔
160
            case 'POST':
12✔
161
                $persistResult = $this->processor->process($controllerResult, $operation, $uriVariables, $context);
8✔
162

163
                if ($persistResult) {
8✔
164
                    $controllerResult = $persistResult;
8✔
165
                    $event->setControllerResult($controllerResult);
8✔
166
                }
167

168
                if ($controllerResult instanceof Response) {
8✔
169
                    break;
×
170
                }
171

172
                $outputMetadata = $operation->getOutput() ?? ['class' => $attributes['resource_class']];
8✔
173
                $hasOutput = \is_array($outputMetadata) && \array_key_exists('class', $outputMetadata) && null !== $outputMetadata['class'];
8✔
174
                if (!$hasOutput) {
8✔
175
                    break;
4✔
176
                }
177

178
                if ($this->resourceClassResolver->isResourceClass($this->getObjectClass($controllerResult))) {
4✔
179
                    $request->attributes->set('_api_write_item_iri', $this->iriConverter->getIriFromResource($controllerResult));
4✔
180
                }
181

182
                break;
4✔
183
            case 'DELETE':
4✔
184
                $this->processor->process($controllerResult, $operation, $uriVariables, $context);
4✔
185
                $event->setControllerResult(null);
4✔
186
                break;
4✔
187
        }
188
    }
189
}
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