forked from thecodingmachine/graphqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnnotationReader.php
More file actions
413 lines (359 loc) · 14.3 KB
/
AnnotationReader.php
File metadata and controls
413 lines (359 loc) · 14.3 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php
declare(strict_types=1);
namespace TheCodingMachine\GraphQLite;
use ReflectionClass;
use ReflectionEnumUnitCase;
use ReflectionMethod;
use ReflectionParameter;
use ReflectionProperty;
use TheCodingMachine\GraphQLite\Annotations\AbstractGraphQLElement;
use TheCodingMachine\GraphQLite\Annotations\Decorate;
use TheCodingMachine\GraphQLite\Annotations\EnumValue;
use TheCodingMachine\GraphQLite\Annotations\Exceptions\ClassNotFoundException;
use TheCodingMachine\GraphQLite\Annotations\Exceptions\InvalidParameterException;
use TheCodingMachine\GraphQLite\Annotations\ExtendType;
use TheCodingMachine\GraphQLite\Annotations\Factory;
use TheCodingMachine\GraphQLite\Annotations\Input;
use TheCodingMachine\GraphQLite\Annotations\MiddlewareAnnotationInterface;
use TheCodingMachine\GraphQLite\Annotations\MiddlewareAnnotations;
use TheCodingMachine\GraphQLite\Annotations\ParameterAnnotationInterface;
use TheCodingMachine\GraphQLite\Annotations\ParameterAnnotations;
use TheCodingMachine\GraphQLite\Annotations\SourceFieldInterface;
use TheCodingMachine\GraphQLite\Annotations\Type;
use TheCodingMachine\GraphQLite\Annotations\TypeInterface;
use function array_diff_key;
use function array_filter;
use function array_key_exists;
use function array_map;
use function array_merge;
use function assert;
use function count;
use function get_class;
use function is_a;
use function reset;
use function trigger_error;
use const E_USER_DEPRECATED;
class AnnotationReader
{
/** @var array<string, (object|null)> */
private array $methodAnnotationCache = [];
/** @var array<string, array<object>> */
private array $methodAnnotationsCache = [];
/** @var array<string, array<object>> */
private array $propertyAnnotationsCache = [];
/**
* Returns a class annotation. Does not look in the parent class.
*
* @param ReflectionClass<object> $refClass
* @param class-string<T> $annotationClass
*
* @return T|null
*
* @throws ClassNotFoundException
*
* @template T of object
*/
private function getClassAnnotation(ReflectionClass $refClass, string $annotationClass): object|null
{
$attribute = $refClass->getAttributes($annotationClass)[0] ?? null;
if (! $attribute) {
return null;
}
$instance = $attribute->newInstance();
assert($instance instanceof $annotationClass);
return $instance;
}
/**
* Returns a method annotation and handles correctly errors.
*
* @param class-string<object> $annotationClass
*/
private function getMethodAnnotation(ReflectionMethod $refMethod, string $annotationClass): object|null
{
$cacheKey = $refMethod->getDeclaringClass()->getName() . '::' . $refMethod->getName() . '_' . $annotationClass;
if (array_key_exists($cacheKey, $this->methodAnnotationCache)) {
return $this->methodAnnotationCache[$cacheKey];
}
$attribute = $refMethod->getAttributes($annotationClass)[0] ?? null;
if (! $attribute) {
$this->methodAnnotationCache[$cacheKey] = null;
return null;
}
$this->methodAnnotationCache[$cacheKey] = $attribute->newInstance();
return $this->methodAnnotationCache[$cacheKey];
}
/**
* Returns the class annotations. Finds in the parents too.
*
* @param ReflectionClass<T> $refClass
* @param class-string<A> $annotationClass
*
* @return A[]
*
* @template T of object
* @template A of object
*/
public function getClassAnnotations(ReflectionClass $refClass, string $annotationClass, bool $inherited = true): array
{
$toAddAnnotations = [];
do {
/** @var A[] $attributes */
$attributes = array_map(
static function ($attribute) {
return $attribute->newInstance();
},
array_filter($refClass->getAttributes(), static function ($annotation) use ($annotationClass): bool {
return is_a($annotation->getName(), $annotationClass, true);
}),
);
if (count($attributes) > 0) {
$toAddAnnotations[] = $attributes;
}
$refClass = $refClass->getParentClass();
} while ($inherited && $refClass);
if (count($toAddAnnotations) > 0) {
return array_merge(...$toAddAnnotations);
}
return [];
}
/**
* @param ReflectionClass<T> $refClass
*
* @throws ClassNotFoundException
*
* @template T of object
*/
public function getTypeAnnotation(ReflectionClass $refClass): TypeInterface|null
{
try {
$type = $this->getClassAnnotation($refClass, Type::class);
if ($type !== null && $type->isSelfType()) {
$type->setClass($refClass->getName());
}
} catch (ClassNotFoundException $e) {
throw ClassNotFoundException::wrapException($e, $refClass->getName());
}
return $type;
}
/**
* @param ReflectionClass<T> $refClass
*
* @return array<int,Input>
*
* @throws ClassNotFoundException
*
* @template T of object
*/
public function getInputAnnotations(ReflectionClass $refClass): array
{
try {
/** @var array<int,Input> $inputs */
$inputs = $this->getClassAnnotations($refClass, Input::class, false);
foreach ($inputs as $input) {
$input->setClass($refClass->getName());
}
} catch (ClassNotFoundException $e) {
throw ClassNotFoundException::wrapException($e, $refClass->getName());
}
return $inputs;
}
/**
* @param ReflectionClass<T> $refClass
*
* @throws ClassNotFoundException
*
* @template T of object
*/
public function getExtendTypeAnnotation(ReflectionClass $refClass): ExtendType|null
{
try {
$extendType = $this->getClassAnnotation($refClass, ExtendType::class);
} catch (ClassNotFoundException $e) {
throw ClassNotFoundException::wrapExceptionForExtendTag($e, $refClass->getName());
}
return $extendType;
}
/**
* Returns the {@see EnumValue} attribute declared on a PHP enum case, or null when no
* attribute is present. Callers use this to resolve the explicit description and deprecation
* reason before falling back to docblock parsing.
*/
public function getEnumValueAnnotation(ReflectionEnumUnitCase $refCase): EnumValue|null
{
$attribute = $refCase->getAttributes(EnumValue::class)[0] ?? null;
if ($attribute === null) {
return null;
}
$instance = $attribute->newInstance();
assert($instance instanceof EnumValue);
return $instance;
}
/** @param class-string<AbstractGraphQLElement> $annotationClass */
public function getGraphQLElementAnnotation(ReflectionMethod $refMethod, string $annotationClass): AbstractGraphQLElement|null
{
$queryAnnotation = $this->getMethodAnnotation($refMethod, $annotationClass);
assert($queryAnnotation instanceof AbstractGraphQLElement || $queryAnnotation === null);
return $queryAnnotation;
}
/**
* @param ReflectionClass<T> $refClass
*
* @return SourceFieldInterface[]
*
* @template T of object
*/
public function getSourceFields(ReflectionClass $refClass): array
{
/** @var SourceFieldInterface[] $sourceFields */
$sourceFields = $this->getClassAnnotations($refClass, SourceFieldInterface::class);
return $sourceFields;
}
public function getFactoryAnnotation(ReflectionMethod $refMethod): Factory|null
{
$factoryAnnotation = $this->getMethodAnnotation($refMethod, Factory::class);
assert($factoryAnnotation instanceof Factory || $factoryAnnotation === null);
return $factoryAnnotation;
}
public function getDecorateAnnotation(ReflectionMethod $refMethod): Decorate|null
{
$decorateAnnotation = $this->getMethodAnnotation($refMethod, Decorate::class);
assert($decorateAnnotation instanceof Decorate || $decorateAnnotation === null);
return $decorateAnnotation;
}
/**
* @param ReflectionParameter[] $refParameters
*
* @return array<string, ParameterAnnotations>
*
* @throws InvalidParameterException
*/
public function getParameterAnnotationsPerParameter(array $refParameters): array
{
if (empty($refParameters)) {
return [];
}
/** @var array<string, array<int,ParameterAnnotationInterface>> $parameterAnnotationsPerParameter */
$parameterAnnotationsPerParameter = [];
// resolve parameter annotations targeted to method
$firstParam = reset($refParameters);
$method = $firstParam->getDeclaringFunction();
assert($method instanceof ReflectionMethod);
$parameterAnnotations = $this->getMethodAnnotations($method, ParameterAnnotationInterface::class);
foreach ($parameterAnnotations as $parameterAnnotation) {
trigger_error(
"Using '" . ParameterAnnotationInterface::class . "' over methods is deprecated. " .
"Found attribute '" . $parameterAnnotation::class .
"' over '" . $method->getDeclaringClass()->getName() . ':' . $method->getName() . "'. " .
"Please target annotation to the parameter '$" . $parameterAnnotation->getTarget() . "' instead",
E_USER_DEPRECATED,
);
$parameterAnnotationsPerParameter[$parameterAnnotation->getTarget()][] = $parameterAnnotation;
}
// Let's check that the referenced parameters actually do exist:
$parametersByKey = [];
foreach ($refParameters as $refParameter) {
$parametersByKey[$refParameter->getName()] = true;
}
$diff = array_diff_key($parameterAnnotationsPerParameter, $parametersByKey);
if (count($diff) > 0) {
foreach ($diff as $parameterName => $parameterAnnotations) {
throw InvalidParameterException::parameterNotFound($parameterName, get_class($parameterAnnotations[0]), $method);
}
}
// resolve parameter annotations targeted to parameter
foreach ($refParameters as $refParameter) {
$attributes = $refParameter->getAttributes();
$parameterAnnotationsPerParameter[$refParameter->getName()] = [...$parameterAnnotationsPerParameter[$refParameter->getName()] ??
[],
...array_map(
static function ($attribute) {
return $attribute->newInstance();
},
array_filter($attributes, static function ($annotation): bool {
return is_a($annotation->getName(), ParameterAnnotationInterface::class, true);
}),
),
];
}
return array_map(
static function (array $parameterAnnotations): ParameterAnnotations {
/** @var ParameterAnnotationInterface[] $parameterAnnotations */
return new ParameterAnnotations($parameterAnnotations);
},
$parameterAnnotationsPerParameter,
);
}
public function getMiddlewareAnnotations(ReflectionMethod|ReflectionProperty $reflection): MiddlewareAnnotations
{
if ($reflection instanceof ReflectionMethod) {
$middlewareAnnotations = $this->getMethodAnnotations($reflection, MiddlewareAnnotationInterface::class);
} else {
$middlewareAnnotations = $this->getPropertyAnnotations($reflection, MiddlewareAnnotationInterface::class);
}
return new MiddlewareAnnotations($middlewareAnnotations);
}
/**
* Returns the method's annotations.
*
* @param class-string<T> $annotationClass
*
* @return array<int, T>
*
* @template T of object
*/
public function getMethodAnnotations(ReflectionMethod $refMethod, string $annotationClass): array
{
$cacheKey = $refMethod->getDeclaringClass()->getName() . '::' . $refMethod->getName() . '_s_' . $annotationClass;
if (isset($this->methodAnnotationsCache[$cacheKey])) {
/** @var array<int, T> $annotations */
$annotations = $this->methodAnnotationsCache[$cacheKey];
return $annotations;
}
$attributes = $refMethod->getAttributes();
/** @var array<int, T> $toAddAnnotations */
$toAddAnnotations = [
...array_map(
static function ($attribute) {
return $attribute->newInstance();
},
array_filter($attributes, static function ($annotation) use ($annotationClass): bool {
return is_a($annotation->getName(), $annotationClass, true);
}),
),
];
$this->methodAnnotationsCache[$cacheKey] = $toAddAnnotations;
return $toAddAnnotations;
}
/**
* Returns the property's annotations.
*
* @param class-string<T> $annotationClass
*
* @return array<int, T>
*
* @template T of object
*/
public function getPropertyAnnotations(ReflectionProperty $refProperty, string $annotationClass): array
{
$cacheKey = $refProperty->getDeclaringClass()->getName() . '::' . $refProperty->getName() . '_s_' . $annotationClass;
if (isset($this->propertyAnnotationsCache[$cacheKey])) {
/** @var array<int, T> $annotations */
$annotations = $this->propertyAnnotationsCache[$cacheKey];
return $annotations;
}
$attributes = $refProperty->getAttributes();
/** @var array<int, T> $toAddAnnotations */
$toAddAnnotations = [
...array_map(
static function ($attribute) {
return $attribute->newInstance();
},
array_filter($attributes, static function ($annotation) use ($annotationClass): bool {
return is_a($annotation->getName(), $annotationClass, true);
}),
),
];
$this->propertyAnnotationsCache[$cacheKey] = $toAddAnnotations;
return $toAddAnnotations;
}
}