|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace TheCodingMachine\GraphQL\Controllers; |
|
5
|
|
|
|
|
6
|
|
|
use phpDocumentor\Reflection\Type; |
|
7
|
|
|
use phpDocumentor\Reflection\Types\Array_; |
|
8
|
|
|
use phpDocumentor\Reflection\Types\Boolean; |
|
9
|
|
|
use phpDocumentor\Reflection\Types\Float_; |
|
10
|
|
|
use phpDocumentor\Reflection\Types\Mixed; |
|
11
|
|
|
use phpDocumentor\Reflection\Types\Object_; |
|
12
|
|
|
use phpDocumentor\Reflection\Types\String_; |
|
13
|
|
|
use Roave\BetterReflection\Reflection\ReflectionClass; |
|
14
|
|
|
use Roave\BetterReflection\Reflection\ReflectionMethod; |
|
15
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
16
|
|
|
use phpDocumentor\Reflection\Types\Integer; |
|
17
|
|
|
use TheCodingMachine\GraphQL\Controllers\Annotations\Logged; |
|
18
|
|
|
use TheCodingMachine\GraphQL\Controllers\Annotations\Mutation; |
|
19
|
|
|
use TheCodingMachine\GraphQL\Controllers\Annotations\Query; |
|
20
|
|
|
use TheCodingMachine\GraphQL\Controllers\Annotations\Right; |
|
21
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\AuthenticationServiceInterface; |
|
22
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\AuthorizationServiceInterface; |
|
23
|
|
|
use Youshido\GraphQL\Field\Field; |
|
24
|
|
|
use Youshido\GraphQL\Type\ListType\ListType; |
|
25
|
|
|
use Youshido\GraphQL\Type\NonNullType; |
|
26
|
|
|
use Youshido\GraphQL\Type\Scalar\BooleanType; |
|
27
|
|
|
use Youshido\GraphQL\Type\Scalar\DateTimeType; |
|
28
|
|
|
use Youshido\GraphQL\Type\Scalar\FloatType; |
|
29
|
|
|
use Youshido\GraphQL\Type\Scalar\IntType; |
|
30
|
|
|
use Youshido\GraphQL\Type\Scalar\StringType; |
|
31
|
|
|
use Youshido\GraphQL\Type\TypeInterface; |
|
32
|
|
|
use Youshido\GraphQL\Type\Union\UnionType; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* A query provider that looks for queries in a "controller" |
|
36
|
|
|
*/ |
|
37
|
|
|
class ControllerQueryProvider implements QueryProviderInterface |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @var object |
|
41
|
|
|
*/ |
|
42
|
|
|
private $controller; |
|
43
|
|
|
/** |
|
44
|
|
|
* @var Reader |
|
45
|
|
|
*/ |
|
46
|
|
|
private $annotationReader; |
|
47
|
|
|
/** |
|
48
|
|
|
* @var TypeMapperInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
private $typeMapper; |
|
51
|
|
|
/** |
|
52
|
|
|
* @var HydratorInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
private $hydrator; |
|
55
|
|
|
/** |
|
56
|
|
|
* @var AuthenticationServiceInterface |
|
57
|
|
|
*/ |
|
58
|
|
|
private $authenticationService; |
|
59
|
|
|
/** |
|
60
|
|
|
* @var AuthorizationServiceInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
private $authorizationService; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param object $controller |
|
66
|
|
|
*/ |
|
67
|
|
|
public function __construct($controller, Reader $annotationReader, TypeMapperInterface $typeMapper, HydratorInterface $hydrator, AuthenticationServiceInterface $authenticationService, AuthorizationServiceInterface $authorizationService) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->controller = $controller; |
|
70
|
|
|
$this->annotationReader = $annotationReader; |
|
71
|
|
|
$this->typeMapper = $typeMapper; |
|
72
|
|
|
$this->hydrator = $hydrator; |
|
73
|
|
|
$this->authenticationService = $authenticationService; |
|
74
|
|
|
$this->authorizationService = $authorizationService; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return Field[] |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getQueries(): array |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->getFieldsByAnnotations(Query::class); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return Field[] |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getMutations(): array |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->getFieldsByAnnotations(Mutation::class); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return Field[] |
|
95
|
|
|
*/ |
|
96
|
|
|
private function getFieldsByAnnotations(string $annotationName): array |
|
97
|
|
|
{ |
|
98
|
|
|
$refClass = ReflectionClass::createFromInstance($this->controller); |
|
99
|
|
|
|
|
100
|
|
|
$queryList = []; |
|
101
|
|
|
|
|
102
|
|
|
$typeResolver = new \phpDocumentor\Reflection\TypeResolver(); |
|
103
|
|
|
|
|
104
|
|
|
foreach ($refClass->getMethods() as $refMethod) { |
|
105
|
|
|
$standardPhpMethod = new \ReflectionMethod(get_class($this->controller), $refMethod->getName()); |
|
106
|
|
|
// First, let's check the "Query" annotation |
|
107
|
|
|
$queryAnnotation = $this->annotationReader->getMethodAnnotation($standardPhpMethod, $annotationName); |
|
108
|
|
|
|
|
109
|
|
|
if ($queryAnnotation !== null) { |
|
110
|
|
|
if (!$this->isAuthorized($standardPhpMethod)) { |
|
111
|
|
|
continue; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$methodName = $refMethod->getName(); |
|
115
|
|
|
|
|
116
|
|
|
$args = $this->mapParameters($refMethod, $standardPhpMethod); |
|
117
|
|
|
|
|
118
|
|
|
$phpdocType = $typeResolver->resolve((string) $refMethod->getReturnType()); |
|
119
|
|
|
|
|
120
|
|
|
$type = $this->mapType($phpdocType, $refMethod->getDocBlockReturnTypes(), $standardPhpMethod->getReturnType()->allowsNull(), false); |
|
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
$queryList[] = new QueryField($methodName, $type, $args, [$this->controller, $methodName], $this->hydrator); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $queryList; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Checks the @Logged and @Right annotations. |
|
131
|
|
|
* |
|
132
|
|
|
* @param \ReflectionMethod $reflectionMethod |
|
133
|
|
|
* @return bool |
|
134
|
|
|
*/ |
|
135
|
|
|
private function isAuthorized(\ReflectionMethod $reflectionMethod) : bool |
|
136
|
|
|
{ |
|
137
|
|
|
$loggedAnnotation = $this->annotationReader->getMethodAnnotation($reflectionMethod, Logged::class); |
|
138
|
|
|
|
|
139
|
|
|
if ($loggedAnnotation !== null && !$this->authenticationService->isLogged()) { |
|
140
|
|
|
return false; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$rightAnnotation = $this->annotationReader->getMethodAnnotation($reflectionMethod, Right::class); |
|
144
|
|
|
/** @var $rightAnnotation Right */ |
|
145
|
|
|
|
|
146
|
|
|
if ($rightAnnotation !== null && !$this->authorizationService->isAllowed($rightAnnotation->getName())) { |
|
147
|
|
|
return false; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return true; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Note: there is a bug in $refMethod->allowsNull that forces us to use $standardRefMethod->allowsNull instead. |
|
155
|
|
|
* |
|
156
|
|
|
* @param ReflectionMethod $refMethod |
|
157
|
|
|
* @param \ReflectionMethod $standardRefMethod |
|
158
|
|
|
* @return array |
|
159
|
|
|
*/ |
|
160
|
|
|
private function mapParameters(ReflectionMethod $refMethod, \ReflectionMethod $standardRefMethod) |
|
161
|
|
|
{ |
|
162
|
|
|
$args = []; |
|
163
|
|
|
|
|
164
|
|
|
$typeResolver = new \phpDocumentor\Reflection\TypeResolver(); |
|
165
|
|
|
|
|
166
|
|
|
foreach ($standardRefMethod->getParameters() as $standardParameter) { |
|
167
|
|
|
$allowsNull = $standardParameter->allowsNull(); |
|
168
|
|
|
$parameter = $refMethod->getParameter($standardParameter->getName()); |
|
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
$phpdocType = $typeResolver->resolve((string) $parameter->getType()); |
|
171
|
|
|
|
|
172
|
|
|
$args[$parameter->getName()] = $this->mapType($phpdocType, $parameter->getDocBlockTypes(), $allowsNull, true); |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return $args; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param Type $type |
|
180
|
|
|
* @param Type[] $docBlockTypes |
|
181
|
|
|
* @return TypeInterface |
|
182
|
|
|
*/ |
|
183
|
|
|
private function mapType(Type $type, array $docBlockTypes, bool $isNullable, bool $mapToInputType): TypeInterface |
|
184
|
|
|
{ |
|
185
|
|
|
$graphQlType = null; |
|
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
if ($type instanceof Array_ || $type instanceof Mixed) { |
|
188
|
|
|
if (!$isNullable) { |
|
189
|
|
|
// Let's check a "null" value in the docblock |
|
190
|
|
|
$isNullable = $this->isNullable($docBlockTypes); |
|
191
|
|
|
} |
|
192
|
|
|
$filteredDocBlockTypes = $this->typesWithoutNullable($docBlockTypes); |
|
193
|
|
|
if (empty($filteredDocBlockTypes)) { |
|
194
|
|
|
// TODO: improve error message |
|
195
|
|
|
throw new GraphQLException("Don't know how to handle type ".((string) $type)); |
|
196
|
|
|
} elseif (count($filteredDocBlockTypes) === 1) { |
|
197
|
|
|
$graphQlType = $this->toGraphQlType($filteredDocBlockTypes[0], $mapToInputType); |
|
198
|
|
|
} else { |
|
199
|
|
|
throw new GraphQLException('Union types are not supported (yet)'); |
|
200
|
|
|
//$graphQlTypes = array_map([$this, 'toGraphQlType'], $filteredDocBlockTypes); |
|
|
|
|
|
|
201
|
|
|
//$$graphQlType = new UnionType($graphQlTypes); |
|
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
} else { |
|
204
|
|
|
$graphQlType = $this->toGraphQlType($type, $mapToInputType); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
if (!$isNullable) { |
|
208
|
|
|
$graphQlType = new NonNullType($graphQlType); |
|
|
|
|
|
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
return $graphQlType; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Casts a Type to a GraphQL type. |
|
216
|
|
|
* Does not deal with nullable. |
|
217
|
|
|
* |
|
218
|
|
|
* @param Type $type |
|
219
|
|
|
* @param bool $mapToInputType |
|
220
|
|
|
* @return TypeInterface |
|
221
|
|
|
*/ |
|
222
|
|
|
private function toGraphQlType(Type $type, bool $mapToInputType): TypeInterface |
|
223
|
|
|
{ |
|
224
|
|
|
if ($type instanceof Integer) { |
|
225
|
|
|
return new IntType(); |
|
226
|
|
|
} elseif ($type instanceof String_) { |
|
227
|
|
|
return new StringType(); |
|
228
|
|
|
} elseif ($type instanceof Boolean) { |
|
229
|
|
|
return new BooleanType(); |
|
230
|
|
|
} elseif ($type instanceof Float_) { |
|
231
|
|
|
return new FloatType(); |
|
232
|
|
|
} elseif ($type instanceof Object_) { |
|
233
|
|
|
$fqcn = (string) $type->getFqsen(); |
|
234
|
|
|
if ($fqcn === '\\DateTimeImmutable' || $fqcn === '\\DateTimeInterface') { |
|
235
|
|
|
return new DateTimeType(); |
|
236
|
|
|
} elseif ($fqcn === '\\DateTime') { |
|
237
|
|
|
throw new GraphQLException('Type-hinting a parameter against DateTime is not allowed. Please use the DateTimeImmutable type instead.'); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
$className = ltrim($type->getFqsen(), '\\'); |
|
241
|
|
|
if ($mapToInputType) { |
|
242
|
|
|
return $this->typeMapper->mapClassToInputType($className); |
|
243
|
|
|
} else { |
|
244
|
|
|
return $this->typeMapper->mapClassToType($className); |
|
245
|
|
|
} |
|
246
|
|
|
} elseif ($type instanceof Array_) { |
|
247
|
|
|
return new ListType(new NonNullType($this->toGraphQlType($type->getValueType(), $mapToInputType))); |
|
|
|
|
|
|
248
|
|
|
} else { |
|
249
|
|
|
throw new GraphQLException("Don't know how to handle type ".((string) $type)); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Removes "null" from the list of types. |
|
255
|
|
|
* |
|
256
|
|
|
* @param Type[] $docBlockTypeHints |
|
257
|
|
|
* @return array |
|
258
|
|
|
*/ |
|
259
|
|
|
private function typesWithoutNullable(array $docBlockTypeHints): array |
|
260
|
|
|
{ |
|
261
|
|
|
return array_filter($docBlockTypeHints, function ($item) { |
|
262
|
|
|
return !$item instanceof Null_; |
|
|
|
|
|
|
263
|
|
|
}); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* @param Type[] $docBlockTypeHints |
|
268
|
|
|
* @return bool |
|
269
|
|
|
*/ |
|
270
|
|
|
private function isNullable(array $docBlockTypeHints): bool |
|
271
|
|
|
{ |
|
272
|
|
|
foreach ($docBlockTypeHints as $docBlockTypeHint) { |
|
273
|
|
|
if ($docBlockTypeHint instanceof Null_) { |
|
|
|
|
|
|
274
|
|
|
return true; |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
return false; |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: