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

api-platform / core / 6067528200

04 Sep 2023 12:12AM UTC coverage: 36.875% (-21.9%) from 58.794%
6067528200

Pull #5791

github

web-flow
Merge 64157e578 into d09cfc9d2
Pull Request #5791: fix: strip down any sql function name

3096 of 3096 new or added lines in 205 files covered. (100.0%)

9926 of 26918 relevant lines covered (36.87%)

6.5 hits per line

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

78.38
/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;
17
use ApiPlatform\Exception\InvalidIdentifierException;
18
use ApiPlatform\Exception\InvalidUriVariableException;
19
use ApiPlatform\Metadata\Put;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Serializer\SerializerContextBuilderInterface;
22
use ApiPlatform\State\Exception\ProviderNotFoundException;
23
use ApiPlatform\State\ProviderInterface;
24
use ApiPlatform\State\UriVariablesResolverTrait;
25
use ApiPlatform\Util\CloneTrait;
26
use ApiPlatform\Util\OperationRequestInitiatorTrait;
27
use ApiPlatform\Util\RequestAttributesExtractor;
28
use ApiPlatform\Util\RequestParser;
29
use Symfony\Component\HttpKernel\Event\RequestEvent;
30
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
31

32
/**
33
 * Retrieves data from the applicable data provider and sets it as a request parameter called data.
34
 *
35
 * @author Kévin Dunglas <dunglas@gmail.com>
36
 */
37
final class ReadListener
38
{
39
    use CloneTrait;
40
    use OperationRequestInitiatorTrait;
41
    use UriVariablesResolverTrait;
42

43
    public function __construct(
44
        private readonly ProviderInterface $provider,
45
        ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null,
46
        private readonly ?SerializerContextBuilderInterface $serializerContextBuilder = null,
47
        UriVariablesConverterInterface $uriVariablesConverter = null,
48
    ) {
49
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
81✔
50
        $this->uriVariablesConverter = $uriVariablesConverter;
81✔
51
    }
52

53
    /**
54
     * Calls the data provider and sets the data attribute.
55
     *
56
     * @throws NotFoundHttpException
57
     */
58
    public function onKernelRequest(RequestEvent $event): void
59
    {
60
        $request = $event->getRequest();
81✔
61
        $operation = $this->initializeOperation($request);
81✔
62

63
        if ('api_platform.symfony.main_controller' === $operation?->getController()) {
81✔
64
            return;
28✔
65
        }
66

67
        if (!($attributes = RequestAttributesExtractor::extractAttributes($request))) {
57✔
68
            return;
39✔
69
        }
70

71
        if (!$attributes['receive'] || !$operation || !($operation->canRead() ?? true) || (!$operation->getUriVariables() && !$request->isMethodSafe())) {
20✔
72
            return;
9✔
73
        }
74

75
        $context = ['operation' => $operation];
11✔
76

77
        if (null === $filters = $request->attributes->get('_api_filters')) {
11✔
78
            $queryString = RequestParser::getQueryString($request);
11✔
79
            $filters = $queryString ? RequestParser::parseRequestParams($queryString) : null;
11✔
80
        }
81

82
        if ($filters) {
11✔
83
            $context['filters'] = $filters;
×
84
        }
85

86
        if ($this->serializerContextBuilder) {
11✔
87
            // Builtin data providers are able to use the serialization context to automatically add join clauses
88
            $context += $normalizationContext = $this->serializerContextBuilder->createFromRequest($request, true, $attributes);
11✔
89
            $request->attributes->set('_api_normalization_context', $normalizationContext);
11✔
90
        }
91

92
        $parameters = $request->attributes->all();
11✔
93
        $resourceClass = $operation->getClass() ?? $attributes['resource_class'];
11✔
94
        try {
95
            $uriVariables = $this->getOperationUriVariables($operation, $parameters, $resourceClass);
11✔
96
            if ($request->attributes->get('_api_error', false)) {
11✔
97
                $exception = $request->attributes->get('data');
×
98
                $data = $operation->getProvider() ? $this->provider->provide($operation, $uriVariables, $context + ['previous_data' => $exception]) : $exception;
×
99
            } else {
100
                $data = $this->provider->provide($operation, $uriVariables, $context);
11✔
101
            }
102
        } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
×
103
            throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
×
104
        } catch (ProviderNotFoundException $e) {
×
105
            $data = null;
×
106
        }
107

108
        if (
109
            null === $data
11✔
110
            && 'POST' !== $operation->getMethod()
11✔
111
            && (
112
                'PUT' !== $operation->getMethod()
11✔
113
                || ($operation instanceof Put && !($operation->getAllowCreate() ?? false))
11✔
114
            )
115
        ) {
116
            throw new NotFoundHttpException('Not Found');
×
117
        }
118

119
        $request->attributes->set('data', $data);
11✔
120
        $request->attributes->set('previous_data', $this->clone($data));
11✔
121
    }
122
}
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

© 2026 Coveralls, Inc