|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BEAR\Resource\Interceptor; |
|
6
|
|
|
|
|
7
|
|
|
use BEAR\Resource\Annotation\JsonSchema; |
|
8
|
|
|
use BEAR\Resource\Code; |
|
9
|
|
|
use BEAR\Resource\Exception\JsonSchemaException; |
|
10
|
|
|
use BEAR\Resource\Exception\JsonSchemaKeytNotFoundException; |
|
11
|
|
|
use BEAR\Resource\Exception\JsonSchemaNotFoundException; |
|
12
|
|
|
use BEAR\Resource\JsonSchemaExceptionHandlerInterface; |
|
13
|
|
|
use BEAR\Resource\JsonSchemaRequestExceptionHandlerInterface; |
|
14
|
|
|
use BEAR\Resource\ResourceObject; |
|
15
|
|
|
use BEAR\Resource\Types; |
|
16
|
|
|
use JsonSchema\Constraints\Constraint; |
|
17
|
|
|
use JsonSchema\Validator; |
|
18
|
|
|
use Override; |
|
19
|
|
|
use Ray\Aop\MethodInvocation; |
|
20
|
|
|
use Ray\Di\Di\Named; |
|
21
|
|
|
use ReflectionClass; |
|
22
|
|
|
use stdClass; |
|
23
|
|
|
|
|
24
|
|
|
use function assert; |
|
25
|
|
|
use function file_exists; |
|
26
|
|
|
use function is_array; |
|
27
|
|
|
use function is_dir; |
|
28
|
|
|
use function is_object; |
|
29
|
|
|
use function is_string; |
|
30
|
|
|
use function json_decode; |
|
31
|
|
|
use function json_encode; |
|
32
|
|
|
use function property_exists; |
|
33
|
|
|
use function sprintf; |
|
34
|
|
|
use function str_replace; |
|
35
|
|
|
|
|
36
|
|
|
use const JSON_THROW_ON_ERROR; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @psalm-import-type Query from Types |
|
40
|
|
|
* @psalm-import-type Body from Types |
|
41
|
|
|
*/ |
|
42
|
|
|
final class JsonSchemaInterceptor implements JsonSchemaInterceptorInterface |
|
43
|
|
|
{ |
|
44
|
|
|
public function __construct( |
|
45
|
|
|
#[Named('json_schema_dir')] |
|
46
|
|
|
private readonly string $schemaDir, |
|
47
|
|
|
#[Named('json_validate_dir')] |
|
48
|
|
|
private readonly string $validateDir, |
|
49
|
|
|
private readonly JsonSchemaExceptionHandlerInterface $handler, |
|
50
|
|
|
private readonly JsonSchemaRequestExceptionHandlerInterface $requestHandler, |
|
51
|
|
|
#[Named('json_schema_host')] |
|
52
|
|
|
private readonly string|null $schemaHost = null, |
|
53
|
|
|
) { |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritDoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
#[Override] |
|
60
|
|
|
public function invoke(MethodInvocation $invocation): ResourceObject |
|
61
|
|
|
{ |
|
62
|
|
|
$method = $invocation->getMethod(); |
|
63
|
|
|
$jsonSchema = $method->getAnnotation(JsonSchema::class); |
|
64
|
|
|
assert($jsonSchema instanceof JsonSchema); |
|
65
|
|
|
if ($jsonSchema->params) { |
|
66
|
|
|
$arguments = $this->getNamedArguments($invocation); |
|
67
|
|
|
$this->validateRequest($invocation, $jsonSchema, $arguments); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$ro = $invocation->proceed(); |
|
71
|
|
|
assert($ro instanceof ResourceObject); |
|
72
|
|
|
if ($ro->code === 200 || $ro->code === 201) { |
|
73
|
|
|
$this->validateResponse($ro, $jsonSchema); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $ro; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param Query $arguments |
|
|
|
|
|
|
81
|
|
|
* |
|
82
|
|
|
* MethodInvocation<T> generic cannot be specified without breaking interface compatibility |
|
83
|
|
|
* |
|
84
|
|
|
* @phpstan-ignore-next-line missingType.generics |
|
85
|
|
|
*/ |
|
86
|
|
|
private function validateRequest(MethodInvocation $invocation, JsonSchema $jsonSchema, array $arguments): void |
|
87
|
|
|
{ |
|
88
|
|
|
try { |
|
89
|
|
|
$schemaFile = $this->validateDir . '/' . $jsonSchema->params; |
|
90
|
|
|
$this->validateFileExists($schemaFile); |
|
91
|
|
|
$this->validate($arguments, $schemaFile); |
|
92
|
|
|
} catch (JsonSchemaException $e) { |
|
93
|
|
|
$ro = $invocation->getThis(); |
|
94
|
|
|
assert($ro instanceof ResourceObject); |
|
95
|
|
|
/** @psalm-suppress PossiblyUndefinedVariable */ |
|
96
|
|
|
$this->requestHandler->handleRequestException($arguments, $ro, $e, $schemaFile); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private function validateResponse(ResourceObject $ro, JsonSchema $jsonSchema): void |
|
101
|
|
|
{ |
|
102
|
|
|
$schemaFile = $this->getSchemaFile($jsonSchema, $ro); |
|
103
|
|
|
try { |
|
104
|
|
|
$this->validateRo($ro, $schemaFile, $jsonSchema); |
|
105
|
|
|
if (is_string($this->schemaHost)) { |
|
106
|
|
|
$ro->headers['Link'] = sprintf('<%s%s>; rel="describedby"', $this->schemaHost, $jsonSchema->schema); |
|
107
|
|
|
} |
|
108
|
|
|
} catch (JsonSchemaException $e) { |
|
109
|
|
|
$this->handler->handle($ro, $e, $schemaFile); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
private function validateRo(ResourceObject $ro, string $schemaFile, JsonSchema $jsonSchema): void |
|
114
|
|
|
{ |
|
115
|
|
|
$viewJson = $jsonSchema->target === 'view' ? (string) $ro : json_encode($ro, JSON_THROW_ON_ERROR); |
|
116
|
|
|
/** @var array<stdClass>|false|stdClass $json */ |
|
117
|
|
|
$json = json_decode($viewJson, null, 512, JSON_THROW_ON_ERROR); |
|
118
|
|
|
/** @var array<stdClass>|stdClass $target */ |
|
119
|
|
|
$target = is_object($json) ? $this->getTarget($json, $jsonSchema) : $json; |
|
120
|
|
|
$this->validate($target, $schemaFile); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
private function getTarget(object $json, JsonSchema $jsonSchema): mixed |
|
124
|
|
|
{ |
|
125
|
|
|
if ($jsonSchema->key === '') { |
|
126
|
|
|
return $json; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if (! property_exists($json, $jsonSchema->key)) { |
|
130
|
|
|
throw new JsonSchemaKeytNotFoundException($jsonSchema->key); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $json->{$jsonSchema->key}; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** @param array<mixed>|stdClass $target */ |
|
137
|
|
|
private function validate(array|stdClass $target, string $schemaFile): void |
|
138
|
|
|
{ |
|
139
|
|
|
$validator = new Validator(); |
|
140
|
|
|
$schema = (object) ['$ref' => 'file://' . $schemaFile]; |
|
141
|
|
|
$scanArray = is_array($target) ? $target : $this->deepArray($target); |
|
|
|
|
|
|
142
|
|
|
$validator->validate($scanArray, $schema, Constraint::CHECK_MODE_TYPE_CAST); |
|
143
|
|
|
$isValid = $validator->isValid(); |
|
144
|
|
|
if ($isValid) { |
|
145
|
|
|
return; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
throw $this->throwJsonSchemaException($validator, $schemaFile); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** @return Body */ |
|
|
|
|
|
|
152
|
|
|
private function deepArray(object $values): array |
|
153
|
|
|
{ |
|
154
|
|
|
$result = []; |
|
155
|
|
|
// Iterating over object properties yields mixed key/value types |
|
156
|
|
|
/** @psalm-suppress MixedAssignment */ |
|
157
|
|
|
foreach ($values as $key => $value) { // @phpstan-ignore-line |
|
158
|
|
|
/** @psalm-suppress MixedArrayOffset */ |
|
159
|
|
|
$result[$key] = is_object($value) ? $this->deepArray($value) : $value; // @phpstan-ignore-line |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $result; |
|
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
private function throwJsonSchemaException(Validator $validator, string $schemaFile): JsonSchemaException |
|
166
|
|
|
{ |
|
167
|
|
|
/** @var array<array<string, string>> $errors */ |
|
168
|
|
|
$errors = $validator->getErrors(); |
|
169
|
|
|
$msg = ''; |
|
170
|
|
|
foreach ($errors as $error) { |
|
171
|
|
|
$msg .= sprintf('[%s] %s; ', $error['property'], $error['message']); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$msg .= "by {$schemaFile}"; |
|
175
|
|
|
|
|
176
|
|
|
return new JsonSchemaException($msg, Code::ERROR); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
private function getSchemaFile(JsonSchema $jsonSchema, ResourceObject $ro): string |
|
180
|
|
|
{ |
|
181
|
|
|
if (! $jsonSchema->schema) { |
|
182
|
|
|
// for BC only |
|
183
|
|
|
new ReflectionClass($ro); |
|
184
|
|
|
$roFileName = $this->getParentClassName($ro); |
|
185
|
|
|
$bcFile = str_replace('.php', '.json', $roFileName); |
|
186
|
|
|
if (file_exists($bcFile)) { |
|
187
|
|
|
return $bcFile; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$schemaFile = $this->schemaDir . '/' . $jsonSchema->schema; |
|
192
|
|
|
$this->validateFileExists($schemaFile); |
|
193
|
|
|
|
|
194
|
|
|
return $schemaFile; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
private function getParentClassName(ResourceObject $ro): string |
|
198
|
|
|
{ |
|
199
|
|
|
$parent = (new ReflectionClass($ro))->getParentClass(); |
|
200
|
|
|
|
|
201
|
|
|
return $parent instanceof ReflectionClass ? (string) $parent->getFileName() : ''; |
|
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
private function validateFileExists(string $schemaFile): void |
|
205
|
|
|
{ |
|
206
|
|
|
if (! file_exists($schemaFile) || is_dir($schemaFile)) { |
|
207
|
|
|
throw new JsonSchemaNotFoundException($schemaFile); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @param MethodInvocation<object> $invocation |
|
213
|
|
|
* |
|
214
|
|
|
* @return Query |
|
215
|
|
|
*/ |
|
216
|
|
|
private function getNamedArguments(MethodInvocation $invocation): array |
|
217
|
|
|
{ |
|
218
|
|
|
$parameters = $invocation->getMethod()->getParameters(); |
|
219
|
|
|
$values = $invocation->getArguments(); |
|
220
|
|
|
$arguments = []; |
|
221
|
|
|
foreach ($parameters as $index => $parameter) { |
|
222
|
|
|
/** @psalm-suppress MixedAssignment */ |
|
223
|
|
|
$arguments[$parameter->name] = $values[$index] ?? $parameter->getDefaultValue(); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
return $arguments; |
|
|
|
|
|
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths