-
-
Notifications
You must be signed in to change notification settings - Fork 962
Expand file tree
/
Copy pathParameterProvider.php
More file actions
180 lines (146 loc) · 6.85 KB
/
ParameterProvider.php
File metadata and controls
180 lines (146 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\State\Provider;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Parameter;
use ApiPlatform\Metadata\ResponseHeaderParameterInterface;
use ApiPlatform\State\Exception\ParameterNotSupportedException;
use ApiPlatform\State\Exception\ProviderNotFoundException;
use ApiPlatform\State\ParameterNotFound;
use ApiPlatform\State\ParameterProvider\ReadLinkParameterProvider;
use ApiPlatform\State\ProviderInterface;
use ApiPlatform\State\StopwatchAwareInterface;
use ApiPlatform\State\StopwatchAwareTrait;
use ApiPlatform\State\Util\ParameterParserTrait;
use ApiPlatform\State\Util\RequestParser;
use Psr\Container\ContainerInterface;
/**
* Loops over parameters to:
* - compute its values set as extra properties from the Parameter object (`_api_values`)
* - call the Parameter::provider if any and updates the Operation
*/
final class ParameterProvider implements ProviderInterface, StopwatchAwareInterface
{
use ParameterParserTrait;
use StopwatchAwareTrait;
public function __construct(private readonly ?ProviderInterface $decorated = null, private readonly ?ContainerInterface $locator = null)
{
}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$this->stopwatch?->start('api_platform.provider.parameter');
$request = $context['request'] ?? null;
if ($request && null === $request->attributes->get('_api_query_parameters')) {
$queryString = RequestParser::getQueryString($request);
$request->attributes->set('_api_query_parameters', $queryString ? RequestParser::parseRequestParams($queryString) : []);
}
if ($request && null === $request->attributes->get('_api_header_parameters')) {
$request->attributes->set('_api_header_parameters', $request->headers->all());
}
$parameters = $operation->getParameters();
if ($operation instanceof HttpOperation && true === $operation->getStrictQueryParameterValidation()) {
$keys = [];
foreach ($parameters as $parameter) {
$keys[] = $parameter->getKey();
}
foreach (array_keys($request->attributes->get('_api_query_parameters')) as $key) {
if (!\in_array($key, $keys, true)) {
throw new ParameterNotSupportedException($key);
}
}
}
$context = ['operation' => $operation] + $context;
foreach ($parameters ?? [] as $parameter) {
$values = $this->getParameterValues($parameter, $request, $context);
$value = $this->extractParameterValues($parameter, $values);
// we force API Platform's value extraction, use _api_query_parameters or _api_header_parameters if you need to set a value
if (isset($parameter->getExtraProperties()['_api_values'])) {
unset($parameter->getExtraProperties()['_api_values']);
}
if (null !== ($default = $parameter->getDefault() ?? $parameter->getSchema()['default'] ?? null) && $value instanceof ParameterNotFound) {
$value = $default;
}
$parameter->setValue($value);
$context['operation'] = $operation = $this->callParameterProvider($operation, $parameter, $values, $context);
}
if ($parameters) {
$operation = $operation->withParameters($parameters);
}
if ($operation instanceof HttpOperation) {
$operation = $this->handlePathParameters($operation, $uriVariables, $context);
}
$request?->attributes->set('_api_operation', $operation);
$context['operation'] = $operation;
$this->stopwatch?->stop('api_platform.provider.parameter');
return $this->decorated?->provide($operation, $uriVariables, $context);
}
/**
* TODO: uriVariables could be a Parameters instance, it'd make things easier.
*
* @param array<string, mixed> $uriVariables
* @param array<string, mixed> $context
*/
private function handlePathParameters(HttpOperation $operation, array $uriVariables, array $context): HttpOperation
{
foreach ($operation->getUriVariables() ?? [] as $key => $uriVariable) {
$uriVariable = $uriVariable->withKey($key);
if ($uriVariable->getSecurity() && !$uriVariable->getProvider()) {
$uriVariable = $uriVariable->withProvider(ReadLinkParameterProvider::class);
}
$values = $uriVariables;
if (!\array_key_exists($key, $uriVariables)) {
continue;
}
$value = $uriVariables[$key];
// we force API Platform's value extraction, use _api_query_parameters or _api_header_parameters if you need to set a value
if (isset($uriVariable->getExtraProperties()['_api_values'])) {
unset($uriVariable->getExtraProperties()['_api_values']);
}
if (($default = $uriVariable->getDefault() ?? $uriVariable->getSchema()['default'] ?? false) && ($value instanceof ParameterNotFound || !$value)) {
$value = $default;
}
$uriVariable->setValue($value);
if (($op = $this->callParameterProvider($operation, $uriVariable, $values, $context)) instanceof HttpOperation) {
$context['operation'] = $operation = $op;
}
}
return $operation;
}
/**
* @param array<string,mixed> $context
*/
private function callParameterProvider(Operation $operation, Parameter $parameter, mixed $values, array $context): Operation
{
if ($parameter->getValue() instanceof ParameterNotFound && !$parameter instanceof ResponseHeaderParameterInterface) {
return $operation;
}
if (null === ($provider = $parameter->getProvider())) {
return $operation;
}
if (\is_callable($provider)) {
if (($op = $provider($parameter, $values, $context)) instanceof Operation) {
$operation = $op;
}
return $operation;
}
if (\is_string($provider)) {
if (!$this->locator->has($provider)) {
throw new ProviderNotFoundException(\sprintf('Provider "%s" not found on operation "%s"', $provider, $operation->getName()));
}
$provider = $this->locator->get($provider);
}
if (($op = $provider->provide($parameter, $values, $context)) instanceof Operation) {
$operation = $op;
}
return $operation;
}
}