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

api-platform / core / 10682869832

03 Sep 2024 12:24PM UTC coverage: 70.804%. Remained the same
10682869832

push

github

soyuka
docs: changelog v3.4.0-alpha.4

3075 of 4343 relevant lines covered (70.8%)

75.56 hits per line

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

25.0
/src/Symfony/EventListener/ReadListener.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\UriVariablesConverterInterface as LegacyUriVariablesConverterInterface;
17
use ApiPlatform\Exception\InvalidIdentifierException;
18
use ApiPlatform\Exception\InvalidUriVariableException;
19
use ApiPlatform\Metadata\Error;
20
use ApiPlatform\Metadata\HttpOperation;
21
use ApiPlatform\Metadata\Put;
22
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23
use ApiPlatform\Metadata\UriVariablesConverterInterface;
24
use ApiPlatform\Metadata\Util\CloneTrait;
25
use ApiPlatform\Serializer\SerializerContextBuilderInterface as LegacySerializerContextBuilderInterface;
26
use ApiPlatform\State\CallableProvider;
27
use ApiPlatform\State\Exception\ProviderNotFoundException;
28
use ApiPlatform\State\Provider\ReadProvider;
29
use ApiPlatform\State\ProviderInterface;
30
use ApiPlatform\State\SerializerContextBuilderInterface;
31
use ApiPlatform\State\UriVariablesResolverTrait;
32
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
33
use ApiPlatform\State\Util\RequestParser;
34
use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
35
use Symfony\Component\HttpKernel\Event\RequestEvent;
36
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
37

38
/**
39
 * Retrieves data from the applicable data provider and sets it as a request parameter called data.
40
 *
41
 * @author Kévin Dunglas <dunglas@gmail.com>
42
 */
43
final class ReadListener
44
{
45
    use CloneTrait;
46
    use OperationRequestInitiatorTrait;
47
    use UriVariablesResolverTrait;
48

49
    public function __construct(
50
        private readonly ProviderInterface $provider,
51
        ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null,
52
        private readonly LegacySerializerContextBuilderInterface|SerializerContextBuilderInterface|null $serializerContextBuilder = null,
53
        LegacyUriVariablesConverterInterface|UriVariablesConverterInterface|null $uriVariablesConverter = null,
54
    ) {
55
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
126✔
56
        $this->uriVariablesConverter = $uriVariablesConverter;
126✔
57

58
        if ($provider instanceof CallableProvider) {
126✔
59
            trigger_deprecation('api-platform/core', '3.3', 'Use a "%s" as first argument in "%s" instead of "%s".', ReadProvider::class, self::class, $provider::class);
118✔
60
        }
61
    }
62

63
    /**
64
     * Calls the data provider and sets the data attribute.
65
     *
66
     * @throws NotFoundHttpException
67
     */
68
    public function onKernelRequest(RequestEvent $event): void
69
    {
70
        $request = $event->getRequest();
126✔
71

72
        if (!($attributes = RequestAttributesExtractor::extractAttributes($request)) || !$attributes['receive']) {
126✔
73
            return;
29✔
74
        }
75

76
        $operation = $this->initializeOperation($request);
100✔
77

78
        if ($operation && !$this->provider instanceof CallableProvider) {
100✔
79
            if (null === $operation->canRead()) {
×
80
                $operation = $operation->withRead($operation->getUriVariables() || $request->isMethodSafe());
×
81
            }
82

83
            $uriVariables = [];
×
84
            if (!$operation instanceof Error && $operation instanceof HttpOperation) {
×
85
                try {
86
                    $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
×
87
                } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
×
88
                    if ($operation->canRead()) {
×
89
                        throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
×
90
                    }
91
                }
92
            }
93

94
            $request->attributes->set('_api_uri_variables', $uriVariables);
×
95
            $this->provider->provide($operation, $uriVariables, [
×
96
                'request' => $request,
×
97
                'uri_variables' => $uriVariables,
×
98
                'resource_class' => $operation->getClass(),
×
99
            ]);
×
100

101
            return;
×
102
        }
103

104
        if ('api_platform.symfony.main_controller' === $operation?->getController() || $request->attributes->get('_api_platform_disable_listeners')) {
100✔
105
            return;
96✔
106
        }
107

108
        if (!$operation || !($operation->canRead() ?? true) || (!$operation->getUriVariables() && !$request->isMethodSafe())) {
4✔
109
            return;
4✔
110
        }
111

112
        $context = ['operation' => $operation];
×
113

114
        if (null === $filters = $request->attributes->get('_api_filters')) {
×
115
            $queryString = RequestParser::getQueryString($request);
×
116
            $filters = $queryString ? RequestParser::parseRequestParams($queryString) : null;
×
117
        }
118

119
        if ($filters) {
×
120
            $context['filters'] = $filters;
×
121
        }
122

123
        if ($this->serializerContextBuilder) {
×
124
            // Builtin data providers are able to use the serialization context to automatically add join clauses
125
            $context += $normalizationContext = $this->serializerContextBuilder->createFromRequest($request, true, $attributes);
×
126
            $request->attributes->set('_api_normalization_context', $normalizationContext);
×
127
        }
128

129
        $parameters = $request->attributes->all();
×
130
        $resourceClass = $operation->getClass() ?? $attributes['resource_class'];
×
131
        try {
132
            $uriVariables = $this->getOperationUriVariables($operation, $parameters, $resourceClass);
×
133
            $data = $this->provider->provide($operation, $uriVariables, $context);
×
134
        } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
×
135
            throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
×
136
        } catch (ProviderNotFoundException $e) {
×
137
            $data = null;
×
138
        }
139

140
        if (
141
            null === $data
×
142
            && 'POST' !== $operation->getMethod()
×
143
            && (
144
                'PUT' !== $operation->getMethod()
×
145
                || ($operation instanceof Put && !($operation->getAllowCreate() ?? false))
×
146
            )
147
        ) {
148
            throw new NotFoundHttpException('Not Found');
×
149
        }
150

151
        $request->attributes->set('data', $data);
×
152
        $request->attributes->set('previous_data', $this->clone($data));
×
153
    }
154
}
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