1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Definition\Loader\Annotation\FieldDecorator; |
12
|
|
|
|
13
|
|
|
use Ynlo\GraphQLBundle\Annotation; |
14
|
|
|
use Ynlo\GraphQLBundle\Definition\FieldDefinition; |
15
|
|
|
use Ynlo\GraphQLBundle\Definition\Loader\Annotation\AnnotationReaderAwareTrait; |
16
|
|
|
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface; |
17
|
|
|
use Ynlo\GraphQLBundle\Util\TypeUtil; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Decorate a field definition using common GraphQL annotations |
21
|
|
|
*/ |
22
|
|
|
class GraphQLFieldDefinitionDecorator implements FieldDefinitionDecoratorInterface |
23
|
|
|
{ |
24
|
|
|
use AnnotationReaderAwareTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
22 |
|
public function decorateFieldDefinition($field, FieldDefinition $definition, ObjectDefinitionInterface $objectDefinition) |
30
|
|
|
{ |
31
|
22 |
|
if (!$field instanceof \ReflectionProperty && !$field instanceof \ReflectionMethod) { |
32
|
|
|
throw new \InvalidArgumentException('Invalid argument, expected reflection of property or method'); |
33
|
|
|
} |
34
|
|
|
|
35
|
22 |
|
if (null !== $name = $this->resolveFieldName($field)) { |
36
|
22 |
|
$definition->setName($name); |
37
|
|
|
} |
38
|
|
|
|
39
|
22 |
|
if (null !== $type = $this->resolveFieldType($field)) { |
|
|
|
|
40
|
22 |
|
$definition->setType($this->resolveFieldType($field)); |
41
|
22 |
|
$definition->setList($this->resolveFieldIsList($field)); |
42
|
22 |
|
$definition->setNonNull($this->resolveFieldNonNull($field)); |
43
|
22 |
|
$definition->setNonNullList($this->resolveFieldNonNullList($field)); |
44
|
|
|
} |
45
|
|
|
|
46
|
22 |
|
if (null !== $description = $this->resolveFieldDescription($field)) { |
47
|
22 |
|
$definition->setDescription($description); |
48
|
|
|
} |
49
|
|
|
|
50
|
22 |
|
if (null !== $deprecationReason = $this->resolveFieldDeprecationReason($field)) { |
51
|
|
|
$definition->setDeprecationReason($deprecationReason); |
52
|
|
|
} |
53
|
|
|
|
54
|
22 |
|
if (null !== $complexity = $this->resolveFieldComplexity($field)) { |
55
|
|
|
$definition->setComplexity($complexity); |
56
|
|
|
} |
57
|
|
|
|
58
|
22 |
|
if ($maxConcurrentUsage = $this->resolveFieldMaxConcurrentUsage($field)) { |
59
|
|
|
$definition->setMaxConcurrentUsage($maxConcurrentUsage); |
|
|
|
|
60
|
|
|
} |
61
|
22 |
|
|
62
|
|
|
if ($roles = $this->resolveFieldRoles($field)) { |
63
|
|
|
$definition->setRoles($roles); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get field specific annotation matching given objectDefinition |
69
|
|
|
* |
70
|
|
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
71
|
22 |
|
* @param string $annotationClass |
72
|
|
|
* |
73
|
22 |
|
* @return mixed |
74
|
22 |
|
*/ |
75
|
|
|
protected function getFieldAnnotation($prop, string $annotationClass) |
76
|
|
|
{ |
77
|
22 |
|
if ($prop instanceof \ReflectionProperty) { |
78
|
|
|
return $this->reader->getPropertyAnnotation($prop, $annotationClass); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->reader->getMethodAnnotation($prop, $annotationClass); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
22 |
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
86
|
|
|
* |
87
|
|
|
* @return string|null |
88
|
22 |
|
*/ |
89
|
22 |
|
protected function resolveFieldDescription($prop): ?string |
90
|
|
|
{ |
91
|
|
|
/** @var Annotation\Field $annotation */ |
92
|
22 |
|
if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) { |
93
|
|
|
return $annotation->description; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
22 |
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
101
|
|
|
* |
102
|
|
|
* @return string|null |
103
|
22 |
|
*/ |
104
|
22 |
|
protected function resolveFieldDeprecationReason($prop): ?string |
105
|
|
|
{ |
106
|
|
|
/** @var Annotation\Field $annotation */ |
107
|
22 |
|
if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) { |
108
|
|
|
return $annotation->deprecationReason; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return null; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
22 |
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
116
|
|
|
* |
117
|
|
|
* @return string|null |
118
|
22 |
|
*/ |
119
|
22 |
|
protected function resolveFieldComplexity($prop): ?string |
120
|
|
|
{ |
121
|
|
|
/** @var Annotation\Field $annotation */ |
122
|
22 |
|
if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) { |
123
|
|
|
return $annotation->complexity; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
22 |
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
131
|
|
|
* |
132
|
|
|
* @return string|null |
133
|
22 |
|
*/ |
134
|
22 |
|
protected function resolveFieldMaxConcurrentUsage($prop): ?string |
135
|
|
|
{ |
136
|
|
|
/** @var Annotation\Field $annotation */ |
137
|
22 |
|
if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) { |
138
|
|
|
return $annotation->maxConcurrentUsage; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return 0; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
22 |
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
22 |
|
*/ |
149
|
22 |
|
private function resolveFieldRoles($prop): array |
150
|
22 |
|
{ |
151
|
|
|
/** @var Annotation\Field $annotation */ |
152
|
|
|
if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) { |
153
|
|
|
return (array) $annotation->roles; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return []; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
161
|
22 |
|
* |
162
|
|
|
* @return bool|null |
163
|
|
|
*/ |
164
|
22 |
|
protected function resolveFieldNonNull($prop): ?bool |
165
|
22 |
|
{ |
166
|
22 |
|
/** @var Annotation\Field $annotationField */ |
167
|
|
|
$annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class); |
168
|
|
|
if ($annotationField && $annotationField->type) { |
169
|
|
|
return TypeUtil::isTypeNonNull($annotationField->type); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return null; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
177
|
22 |
|
* |
178
|
|
|
* @return bool|null |
179
|
|
|
*/ |
180
|
22 |
|
protected function resolveFieldNonNullList($prop): ?bool |
181
|
22 |
|
{ |
182
|
22 |
|
/** @var Annotation\Field $annotationField */ |
183
|
|
|
$annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class); |
184
|
|
|
if ($annotationField && $annotationField->type) { |
185
|
|
|
return TypeUtil::isTypeNonNullList($annotationField->type); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return null; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
193
|
22 |
|
* |
194
|
|
|
* @return bool|null |
195
|
22 |
|
*/ |
196
|
|
|
protected function resolveFieldIsList($prop): ?bool |
197
|
|
|
{ |
198
|
22 |
|
/** @var Annotation\Field $annotationField */ |
199
|
22 |
|
$annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class); |
200
|
22 |
|
if ($annotationField && $annotationField->type) { |
201
|
|
|
return TypeUtil::isTypeList($annotationField->type); |
202
|
|
|
} |
203
|
22 |
|
|
204
|
|
|
return null; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
209
|
|
|
* |
210
|
|
|
* @return string|null |
211
|
22 |
|
*/ |
212
|
|
|
protected function resolveFieldType($prop): ?string |
213
|
|
|
{ |
214
|
22 |
|
$type = null; |
215
|
22 |
|
|
216
|
|
|
/** @var Annotation\Field $annotationField */ |
217
|
|
|
$annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class); |
218
|
22 |
|
if ($annotationField && $annotationField->type) { |
219
|
22 |
|
$type = TypeUtil::normalize($annotationField->type); |
220
|
|
|
} |
221
|
|
|
|
222
|
22 |
|
return $type; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param \ReflectionMethod|\ReflectionProperty $prop |
227
|
|
|
* |
228
|
|
|
* @return string|null |
229
|
|
|
*/ |
230
|
|
|
protected function resolveFieldName($prop): ?string |
231
|
|
|
{ |
232
|
|
|
/** @var Annotation\Field $annotation */ |
233
|
|
|
if (($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) && $annotation->name) { |
234
|
|
|
return $annotation->name; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if ($prop instanceof \ReflectionMethod) { |
238
|
|
|
return lcfirst(preg_replace('/^(get|set)/', null, $prop->name)); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $prop->name; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|