We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 210 |
Total Lines | 1003 |
Duplicated Lines | 0 % |
Coverage | 96.16% |
Changes | 10 | ||
Bugs | 1 | Features | 0 |
Complex classes like AnnotationParser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AnnotationParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class AnnotationParser implements PreParserInterface |
||
49 | { |
||
50 | private static array $classesMap = []; |
||
51 | private static array $providers = []; |
||
52 | private static array $doctrineMapping = []; |
||
53 | private static array $graphClassCache = []; |
||
54 | |||
55 | private const GQL_SCALAR = 'scalar'; |
||
56 | private const GQL_ENUM = 'enum'; |
||
57 | private const GQL_TYPE = 'type'; |
||
58 | private const GQL_INPUT = 'input'; |
||
59 | private const GQL_UNION = 'union'; |
||
60 | private const GQL_INTERFACE = 'interface'; |
||
61 | |||
62 | /** |
||
63 | * @see https://facebook.github.io/graphql/draft/#sec-Input-and-Output-Types |
||
64 | */ |
||
65 | private const VALID_INPUT_TYPES = [self::GQL_SCALAR, self::GQL_ENUM, self::GQL_INPUT]; |
||
66 | private const VALID_OUTPUT_TYPES = [self::GQL_SCALAR, self::GQL_TYPE, self::GQL_INTERFACE, self::GQL_UNION, self::GQL_ENUM]; |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | * |
||
71 | * @throws InvalidArgumentException |
||
72 | * @throws ReflectionException |
||
73 | */ |
||
74 | 25 | public static function preParse(SplFileInfo $file, ContainerBuilder $container, array $configs = []): void |
|
75 | { |
||
76 | 25 | $container->setParameter('overblog_graphql_types.classes_map', self::processFile($file, $container, $configs, true)); |
|
77 | 25 | } |
|
78 | |||
79 | /** |
||
80 | * @throws InvalidArgumentException |
||
81 | * @throws ReflectionException |
||
82 | */ |
||
83 | 25 | public static function parse(SplFileInfo $file, ContainerBuilder $container, array $configs = []): array |
|
84 | { |
||
85 | 25 | return self::processFile($file, $container, $configs, false); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * @internal |
||
90 | */ |
||
91 | 63 | public static function reset(): void |
|
92 | { |
||
93 | 63 | self::$classesMap = []; |
|
94 | 63 | self::$providers = []; |
|
95 | 63 | self::$graphClassCache = []; |
|
96 | 63 | } |
|
97 | |||
98 | /** |
||
99 | * Process a file. |
||
100 | * |
||
101 | * @throws InvalidArgumentException|ReflectionException|AnnotationException |
||
102 | */ |
||
103 | 25 | private static function processFile(SplFileInfo $file, ContainerBuilder $container, array $configs, bool $preProcess): array |
|
104 | { |
||
105 | 25 | self::$doctrineMapping = $configs['doctrine']['types_mapping']; |
|
106 | 25 | $container->addResource(new FileResource($file->getRealPath())); |
|
107 | |||
108 | try { |
||
109 | 25 | $className = $file->getBasename('.php'); |
|
110 | 25 | if (preg_match('#namespace (.+);#', file_get_contents($file->getRealPath()), $matches)) { |
|
111 | 25 | $className = trim($matches[1]).'\\'.$className; |
|
112 | } |
||
113 | |||
114 | 25 | $gqlTypes = []; |
|
115 | 25 | $graphClass = self::getGraphClass($className); |
|
116 | |||
117 | 25 | foreach ($graphClass->getAnnotations() as $classAnnotation) { |
|
118 | 25 | $gqlTypes = self::classAnnotationsToGQLConfiguration( |
|
119 | 25 | $graphClass, |
|
120 | $classAnnotation, |
||
121 | $configs, |
||
122 | $gqlTypes, |
||
123 | $preProcess |
||
124 | ); |
||
125 | } |
||
126 | |||
127 | 25 | return $preProcess ? self::$classesMap : $gqlTypes; |
|
128 | 10 | } catch (\InvalidArgumentException $e) { |
|
129 | 10 | throw new InvalidArgumentException(sprintf('Failed to parse GraphQL annotations from file "%s".', $file), $e->getCode(), $e); |
|
130 | } |
||
131 | } |
||
132 | |||
133 | 25 | private static function classAnnotationsToGQLConfiguration( |
|
134 | GraphClass $graphClass, |
||
135 | object $classAnnotation, |
||
136 | array $configs, |
||
137 | array $gqlTypes, |
||
138 | bool $preProcess |
||
139 | ): array { |
||
140 | 25 | $gqlConfiguration = $gqlType = $gqlName = null; |
|
141 | |||
142 | switch (true) { |
||
143 | 25 | case $classAnnotation instanceof GQL\Type: |
|
144 | 25 | $gqlType = self::GQL_TYPE; |
|
145 | 25 | $gqlName = $classAnnotation->name ?? $graphClass->getShortName(); |
|
146 | 25 | if (!$preProcess) { |
|
147 | 25 | $gqlConfiguration = self::typeAnnotationToGQLConfiguration($graphClass, $classAnnotation, $gqlName, $configs); |
|
148 | |||
149 | 25 | if ($classAnnotation instanceof GQL\Relay\Connection) { |
|
150 | 24 | if (!$graphClass->implementsInterface(ConnectionInterface::class)) { |
|
151 | throw new InvalidArgumentException(sprintf('The annotation @Connection on class "%s" can only be used on class implementing the ConnectionInterface.', $graphClass->getName())); |
||
152 | } |
||
153 | |||
154 | 24 | if (!(isset($classAnnotation->edge) xor isset($classAnnotation->node))) { |
|
155 | throw new InvalidArgumentException(sprintf('The annotation @Connection on class "%s" is invalid. You must define either the "edge" OR the "node" attribute, but not both.', $graphClass->getName())); |
||
156 | } |
||
157 | |||
158 | 24 | $edgeType = $classAnnotation->edge ?? false; |
|
159 | 24 | if (!$edgeType) { |
|
160 | 24 | $edgeType = $gqlName.'Edge'; |
|
161 | 24 | $gqlTypes[$edgeType] = [ |
|
162 | 24 | 'type' => 'object', |
|
163 | 'config' => [ |
||
164 | 'builders' => [ |
||
165 | 24 | ['builder' => 'relay-edge', 'builderConfig' => ['nodeType' => $classAnnotation->node]], |
|
166 | ], |
||
167 | ], |
||
168 | ]; |
||
169 | } |
||
170 | |||
171 | 24 | if (!isset($gqlConfiguration['config']['builders'])) { |
|
172 | 24 | $gqlConfiguration['config']['builders'] = []; |
|
173 | } |
||
174 | |||
175 | 24 | array_unshift($gqlConfiguration['config']['builders'], ['builder' => 'relay-connection', 'builderConfig' => ['edgeType' => $edgeType]]); |
|
176 | } |
||
177 | } |
||
178 | 25 | break; |
|
179 | |||
180 | 24 | case $classAnnotation instanceof GQL\Input: |
|
181 | 24 | $gqlType = self::GQL_INPUT; |
|
182 | 24 | $gqlName = $classAnnotation->name ?? self::suffixName($graphClass->getShortName(), 'Input'); |
|
183 | 24 | if (!$preProcess) { |
|
184 | 24 | $gqlConfiguration = self::inputAnnotationToGQLConfiguration($graphClass, $classAnnotation); |
|
185 | } |
||
186 | 24 | break; |
|
187 | |||
188 | 24 | case $classAnnotation instanceof GQL\Scalar: |
|
189 | 24 | $gqlType = self::GQL_SCALAR; |
|
190 | 24 | if (!$preProcess) { |
|
191 | 24 | $gqlConfiguration = self::scalarAnnotationToGQLConfiguration($graphClass, $classAnnotation); |
|
192 | } |
||
193 | 24 | break; |
|
194 | |||
195 | 24 | case $classAnnotation instanceof GQL\Enum: |
|
196 | 24 | $gqlType = self::GQL_ENUM; |
|
197 | 24 | if (!$preProcess) { |
|
198 | 24 | $gqlConfiguration = self::enumAnnotationToGQLConfiguration($graphClass, $classAnnotation); |
|
199 | } |
||
200 | 24 | break; |
|
201 | |||
202 | 24 | case $classAnnotation instanceof GQL\Union: |
|
203 | 24 | $gqlType = self::GQL_UNION; |
|
204 | 24 | if (!$preProcess) { |
|
205 | 24 | $gqlConfiguration = self::unionAnnotationToGQLConfiguration($graphClass, $classAnnotation); |
|
206 | } |
||
207 | 24 | break; |
|
208 | |||
209 | 24 | case $classAnnotation instanceof GQL\TypeInterface: |
|
210 | 24 | $gqlType = self::GQL_INTERFACE; |
|
211 | 24 | if (!$preProcess) { |
|
212 | 24 | $gqlConfiguration = self::typeInterfaceAnnotationToGQLConfiguration($graphClass, $classAnnotation); |
|
213 | } |
||
214 | 24 | break; |
|
215 | |||
216 | 24 | case $classAnnotation instanceof GQL\Provider: |
|
217 | 24 | if ($preProcess) { |
|
218 | 24 | self::$providers[] = ['metadata' => $graphClass, 'annotation' => $classAnnotation]; |
|
219 | } |
||
220 | |||
221 | 24 | return []; |
|
222 | } |
||
223 | |||
224 | 25 | if (null !== $gqlType) { |
|
225 | 25 | if (!$gqlName) { |
|
226 | 24 | $gqlName = isset($classAnnotation->name) ? $classAnnotation->name : $graphClass->getShortName(); |
|
227 | } |
||
228 | |||
229 | 25 | if ($preProcess) { |
|
230 | 25 | if (isset(self::$classesMap[$gqlName])) { |
|
231 | 1 | throw new InvalidArgumentException(sprintf('The GraphQL type "%s" has already been registered in class "%s"', $gqlName, self::$classesMap[$gqlName]['class'])); |
|
232 | } |
||
233 | 25 | self::$classesMap[$gqlName] = ['type' => $gqlType, 'class' => $graphClass->getName()]; |
|
234 | } else { |
||
235 | 25 | $gqlTypes = [$gqlName => $gqlConfiguration] + $gqlTypes; |
|
236 | } |
||
237 | } |
||
238 | |||
239 | 25 | return $gqlTypes; |
|
240 | } |
||
241 | |||
242 | /** |
||
243 | * @throws ReflectionException |
||
244 | */ |
||
245 | 25 | private static function getGraphClass(string $className): GraphClass |
|
246 | { |
||
247 | 25 | self::$graphClassCache[$className] ??= new GraphClass($className); |
|
248 | |||
249 | 25 | return self::$graphClassCache[$className]; |
|
250 | } |
||
251 | |||
252 | 25 | private static function typeAnnotationToGQLConfiguration( |
|
253 | GraphClass $graphClass, |
||
254 | GQL\Type $classAnnotation, |
||
255 | string $gqlName, |
||
256 | array $configs |
||
257 | ): array { |
||
258 | 25 | $isMutation = $isDefault = $isRoot = false; |
|
259 | 25 | if (isset($configs['definitions']['schema'])) { |
|
260 | 24 | $defaultSchemaName = isset($configs['definitions']['schema']['default']) ? 'default' : array_key_first($configs['definitions']['schema']); |
|
261 | 24 | foreach ($configs['definitions']['schema'] as $schemaName => $schema) { |
|
262 | 24 | $schemaQuery = $schema['query'] ?? null; |
|
263 | 24 | $schemaMutation = $schema['mutation'] ?? null; |
|
264 | |||
265 | 24 | if ($gqlName === $schemaQuery) { |
|
266 | 24 | $isRoot = true; |
|
267 | 24 | if ($defaultSchemaName === $schemaName) { |
|
268 | 24 | $isDefault = true; |
|
269 | } |
||
270 | 24 | } elseif ($gqlName === $schemaMutation) { |
|
271 | 24 | $isMutation = true; |
|
272 | 24 | $isRoot = true; |
|
273 | 24 | if ($defaultSchemaName === $schemaName) { |
|
274 | 24 | $isDefault = true; |
|
275 | } |
||
276 | } |
||
277 | } |
||
278 | } |
||
279 | |||
280 | 25 | $currentValue = $isRoot ? sprintf("service('%s')", self::formatNamespaceForExpression($graphClass->getName())) : 'value'; |
|
281 | |||
282 | 25 | $gqlConfiguration = self::graphQLTypeConfigFromAnnotation($graphClass, $classAnnotation, $currentValue); |
|
283 | |||
284 | 25 | $providerFields = self::getGraphQLFieldsFromProviders($graphClass, $isMutation ? GQL\Mutation::class : GQL\Query::class, $gqlName, $isDefault); |
|
285 | 25 | $gqlConfiguration['config']['fields'] = array_merge($gqlConfiguration['config']['fields'], $providerFields); |
|
286 | |||
287 | 25 | if ($classAnnotation instanceof GQL\Relay\Edge) { |
|
288 | 24 | if (!$graphClass->implementsInterface(EdgeInterface::class)) { |
|
289 | throw new InvalidArgumentException(sprintf('The annotation @Edge on class "%s" can only be used on class implementing the EdgeInterface.', $graphClass->getName())); |
||
290 | } |
||
291 | 24 | if (!isset($gqlConfiguration['config']['builders'])) { |
|
292 | 24 | $gqlConfiguration['config']['builders'] = []; |
|
293 | } |
||
294 | 24 | array_unshift($gqlConfiguration['config']['builders'], ['builder' => 'relay-edge', 'builderConfig' => ['nodeType' => $classAnnotation->node]]); |
|
295 | } |
||
296 | |||
297 | 25 | return $gqlConfiguration; |
|
298 | } |
||
299 | |||
300 | 25 | private static function graphQLTypeConfigFromAnnotation(GraphClass $graphClass, GQL\Type $typeAnnotation, string $currentValue): array |
|
301 | { |
||
302 | 25 | $typeConfiguration = []; |
|
303 | 25 | $fieldsFromProperties = self::getGraphQLTypeFieldsFromAnnotations($graphClass, $graphClass->getPropertiesExtended(), GQL\Field::class, $currentValue); |
|
304 | 25 | $fieldsFromMethods = self::getGraphQLTypeFieldsFromAnnotations($graphClass, $graphClass->getMethods(), GQL\Field::class, $currentValue); |
|
305 | |||
306 | 25 | $typeConfiguration['fields'] = array_merge($fieldsFromProperties, $fieldsFromMethods); |
|
307 | 25 | $typeConfiguration = self::getDescriptionConfiguration($graphClass->getAnnotations()) + $typeConfiguration; |
|
308 | |||
309 | 25 | if (isset($typeAnnotation->interfaces)) { |
|
310 | 24 | $typeConfiguration['interfaces'] = $typeAnnotation->interfaces; |
|
311 | } else { |
||
312 | 25 | $interfaces = array_keys(self::searchClassesMapBy(function ($gqlType, $configuration) use ($graphClass) { |
|
313 | 24 | ['class' => $interfaceClassName] = $configuration; |
|
314 | |||
315 | 24 | $interfaceMetadata = self::getGraphClass($interfaceClassName); |
|
316 | 24 | if ($interfaceMetadata->isInterface() && $graphClass->implementsInterface($interfaceMetadata->getName())) { |
|
317 | 24 | return true; |
|
318 | } |
||
319 | |||
320 | 24 | return $graphClass->isSubclassOf($interfaceClassName); |
|
321 | 25 | }, self::GQL_INTERFACE)); |
|
322 | |||
323 | 25 | sort($interfaces); |
|
324 | 25 | $typeConfiguration['interfaces'] = $interfaces; |
|
325 | } |
||
326 | |||
327 | 25 | if (isset($typeAnnotation->resolveField)) { |
|
328 | 24 | $typeConfiguration['resolveField'] = self::formatExpression($typeAnnotation->resolveField); |
|
329 | } |
||
330 | |||
331 | 25 | if (isset($typeAnnotation->builders) && !empty($typeAnnotation->builders)) { |
|
332 | 24 | $typeConfiguration['builders'] = array_map(function ($fieldsBuilderAnnotation) { |
|
333 | 24 | return ['builder' => $fieldsBuilderAnnotation->builder, 'builderConfig' => $fieldsBuilderAnnotation->builderConfig]; |
|
334 | 24 | }, $typeAnnotation->builders); |
|
335 | } |
||
336 | |||
337 | 25 | if (isset($typeAnnotation->isTypeOf)) { |
|
338 | 24 | $typeConfiguration['isTypeOf'] = $typeAnnotation->isTypeOf; |
|
339 | } |
||
340 | |||
341 | 25 | $publicAnnotation = self::getFirstAnnotationMatching($graphClass->getAnnotations(), GQL\IsPublic::class); |
|
342 | 25 | if (null !== $publicAnnotation) { |
|
343 | 24 | $typeConfiguration['fieldsDefaultPublic'] = self::formatExpression($publicAnnotation->value); |
|
344 | } |
||
345 | |||
346 | 25 | $accessAnnotation = self::getFirstAnnotationMatching($graphClass->getAnnotations(), GQL\Access::class); |
|
347 | |||
348 | 25 | if (null !== $accessAnnotation) { |
|
349 | 24 | if (isset($accessAnnotation->value)) { |
|
350 | 24 | $typeConfiguration['fieldsDefaultAccess'] = self::formatExpression($accessAnnotation->value); |
|
351 | } |
||
352 | 24 | if (isset($accessAnnotation->nullOnDenied)) { |
|
353 | 24 | $typeConfiguration['fieldsDefaultAccessConfig'] = ['nullOnDenied' => $accessAnnotation->nullOnDenied]; |
|
354 | } |
||
355 | } |
||
356 | |||
357 | 25 | return ['type' => $typeAnnotation->isRelay ? 'relay-mutation-payload' : 'object', 'config' => $typeConfiguration]; |
|
358 | } |
||
359 | |||
360 | /** |
||
361 | * Create a GraphQL Interface type configuration from annotations on properties. |
||
362 | */ |
||
363 | 24 | private static function typeInterfaceAnnotationToGQLConfiguration(GraphClass $graphClass, GQL\TypeInterface $interfaceAnnotation): array |
|
364 | { |
||
365 | 24 | $interfaceConfiguration = []; |
|
366 | |||
367 | 24 | $fieldsFromProperties = self::getGraphQLTypeFieldsFromAnnotations($graphClass, $graphClass->getPropertiesExtended()); |
|
368 | 24 | $fieldsFromMethods = self::getGraphQLTypeFieldsFromAnnotations($graphClass, $graphClass->getMethods()); |
|
369 | |||
370 | 24 | $interfaceConfiguration['fields'] = array_merge($fieldsFromProperties, $fieldsFromMethods); |
|
371 | 24 | $interfaceConfiguration = self::getDescriptionConfiguration($graphClass->getAnnotations()) + $interfaceConfiguration; |
|
372 | |||
373 | 24 | $interfaceConfiguration['resolveType'] = self::formatExpression($interfaceAnnotation->resolveType); |
|
374 | |||
375 | 24 | return ['type' => 'interface', 'config' => $interfaceConfiguration]; |
|
376 | } |
||
377 | |||
378 | /** |
||
379 | * Create a GraphQL Input type configuration from annotations on properties. |
||
380 | */ |
||
381 | 24 | private static function inputAnnotationToGQLConfiguration(GraphClass $graphClass, GQL\Input $inputAnnotation): array |
|
382 | { |
||
383 | 24 | $inputConfiguration = array_merge([ |
|
384 | 24 | 'fields' => self::getGraphQLInputFieldsFromAnnotations($graphClass, $graphClass->getPropertiesExtended()), |
|
385 | 24 | ], self::getDescriptionConfiguration($graphClass->getAnnotations())); |
|
386 | |||
387 | 24 | return ['type' => $inputAnnotation->isRelay ? 'relay-mutation-input' : 'input-object', 'config' => $inputConfiguration]; |
|
388 | } |
||
389 | |||
390 | /** |
||
391 | * Get a GraphQL scalar configuration from given scalar annotation. |
||
392 | */ |
||
393 | 24 | private static function scalarAnnotationToGQLConfiguration(GraphClass $graphClass, GQL\Scalar $scalarAnnotation): array |
|
394 | { |
||
395 | 24 | $scalarConfiguration = []; |
|
396 | |||
397 | 24 | if (isset($scalarAnnotation->scalarType)) { |
|
398 | 24 | $scalarConfiguration['scalarType'] = self::formatExpression($scalarAnnotation->scalarType); |
|
399 | } else { |
||
400 | $scalarConfiguration = [ |
||
401 | 24 | 'serialize' => [$graphClass->getName(), 'serialize'], |
|
402 | 24 | 'parseValue' => [$graphClass->getName(), 'parseValue'], |
|
403 | 24 | 'parseLiteral' => [$graphClass->getName(), 'parseLiteral'], |
|
404 | ]; |
||
405 | } |
||
406 | |||
407 | 24 | $scalarConfiguration = self::getDescriptionConfiguration($graphClass->getAnnotations()) + $scalarConfiguration; |
|
408 | |||
409 | 24 | return ['type' => 'custom-scalar', 'config' => $scalarConfiguration]; |
|
410 | } |
||
411 | |||
412 | /** |
||
413 | * Get a GraphQL Enum configuration from given enum annotation. |
||
414 | */ |
||
415 | 24 | private static function enumAnnotationToGQLConfiguration(GraphClass $graphClass, GQL\Enum $enumAnnotation): array |
|
416 | { |
||
417 | 24 | $enumValues = $enumAnnotation->values ? $enumAnnotation->values : []; |
|
418 | |||
419 | 24 | $values = []; |
|
420 | |||
421 | 24 | foreach ($graphClass->getConstants() as $name => $value) { |
|
422 | 24 | $valueAnnotation = current(array_filter($enumValues, fn ($enumValueAnnotation) => $enumValueAnnotation->name == $name)); |
|
423 | 24 | $valueConfig = []; |
|
424 | 24 | $valueConfig['value'] = $value; |
|
425 | |||
426 | 24 | if ($valueAnnotation && isset($valueAnnotation->description)) { |
|
427 | 24 | $valueConfig['description'] = $valueAnnotation->description; |
|
428 | } |
||
429 | |||
430 | 24 | if ($valueAnnotation && isset($valueAnnotation->deprecationReason)) { |
|
431 | 24 | $valueConfig['deprecationReason'] = $valueAnnotation->deprecationReason; |
|
432 | } |
||
433 | |||
434 | 24 | $values[$name] = $valueConfig; |
|
435 | } |
||
436 | |||
437 | 24 | $enumConfiguration = ['values' => $values]; |
|
438 | 24 | $enumConfiguration = self::getDescriptionConfiguration($graphClass->getAnnotations()) + $enumConfiguration; |
|
439 | |||
440 | 24 | return ['type' => 'enum', 'config' => $enumConfiguration]; |
|
441 | } |
||
442 | |||
443 | /** |
||
444 | * Get a GraphQL Union configuration from given union annotation. |
||
445 | */ |
||
446 | 24 | private static function unionAnnotationToGQLConfiguration(GraphClass $graphClass, GQL\Union $unionAnnotation): array |
|
447 | { |
||
448 | 24 | $unionConfiguration = []; |
|
449 | 24 | if (isset($unionAnnotation->types)) { |
|
450 | 24 | $unionConfiguration['types'] = $unionAnnotation->types; |
|
451 | } else { |
||
452 | 24 | $types = array_keys(self::searchClassesMapBy(function ($gqlType, $configuration) use ($graphClass) { |
|
453 | 24 | $typeClassName = $configuration['class']; |
|
454 | 24 | $typeMetadata = self::getGraphClass($typeClassName); |
|
455 | |||
456 | 24 | if ($graphClass->isInterface() && $typeMetadata->implementsInterface($graphClass->getName())) { |
|
457 | 24 | return true; |
|
458 | } |
||
459 | |||
460 | 24 | return $typeMetadata->isSubclassOf($graphClass->getName()); |
|
461 | 24 | }, self::GQL_TYPE)); |
|
462 | 24 | sort($types); |
|
463 | 24 | $unionConfiguration['types'] = $types; |
|
464 | } |
||
465 | |||
466 | 24 | $unionConfiguration = self::getDescriptionConfiguration($graphClass->getAnnotations()) + $unionConfiguration; |
|
467 | |||
468 | 24 | if (isset($unionAnnotation->resolveType)) { |
|
469 | 24 | $unionConfiguration['resolveType'] = self::formatExpression($unionAnnotation->resolveType); |
|
470 | } else { |
||
471 | 24 | if ($graphClass->hasMethod('resolveType')) { |
|
472 | 24 | $method = $graphClass->getMethod('resolveType'); |
|
473 | 24 | if ($method->isStatic() && $method->isPublic()) { |
|
474 | 24 | $unionConfiguration['resolveType'] = self::formatExpression(sprintf("@=call('%s::%s', [service('overblog_graphql.type_resolver'), value], true)", self::formatNamespaceForExpression($graphClass->getName()), 'resolveType')); |
|
475 | } else { |
||
476 | 24 | throw new InvalidArgumentException('The "resolveType()" method on class must be static and public. Or you must define a "resolveType" attribute on the @Union annotation.'); |
|
477 | } |
||
478 | } else { |
||
479 | 1 | throw new InvalidArgumentException('The annotation @Union has no "resolveType" attribute and the related class has no "resolveType()" public static method. You need to define of them.'); |
|
480 | } |
||
481 | } |
||
482 | |||
483 | 24 | return ['type' => 'union', 'config' => $unionConfiguration]; |
|
484 | } |
||
485 | |||
486 | /** |
||
487 | * @phpstan-param ReflectionMethod|ReflectionProperty $reflector |
||
488 | * @phpstan-param class-string<GQL\Field> $fieldAnnotationName |
||
489 | * |
||
490 | * @throws AnnotationException |
||
491 | */ |
||
492 | 24 | private static function getTypeFieldConfigurationFromReflector(GraphClass $graphClass, Reflector $reflector, string $fieldAnnotationName, string $currentValue = 'value'): array |
|
493 | { |
||
494 | 24 | $annotations = $graphClass->getAnnotations($reflector); |
|
495 | |||
496 | 24 | $fieldAnnotation = self::getFirstAnnotationMatching($annotations, $fieldAnnotationName); |
|
497 | 24 | $accessAnnotation = self::getFirstAnnotationMatching($annotations, GQL\Access::class); |
|
498 | 24 | $publicAnnotation = self::getFirstAnnotationMatching($annotations, GQL\IsPublic::class); |
|
499 | |||
500 | 24 | if (null === $fieldAnnotation) { |
|
501 | 24 | if (null !== $accessAnnotation || null !== $publicAnnotation) { |
|
502 | 1 | throw new InvalidArgumentException(sprintf('The annotations "@Access" and/or "@Visible" defined on "%s" are only usable in addition of annotation "@Field"', $reflector->getName())); |
|
503 | } |
||
504 | |||
505 | 24 | return []; |
|
506 | } |
||
507 | |||
508 | 24 | if ($reflector instanceof ReflectionMethod && !$reflector->isPublic()) { |
|
509 | 1 | throw new InvalidArgumentException(sprintf('The Annotation "@Field" can only be applied to public method. The method "%s" is not public.', $reflector->getName())); |
|
510 | } |
||
511 | |||
512 | 24 | $fieldName = $reflector->getName(); |
|
513 | 24 | $fieldConfiguration = []; |
|
514 | |||
515 | 24 | if (isset($fieldAnnotation->type)) { |
|
516 | 24 | $fieldConfiguration['type'] = $fieldAnnotation->type; |
|
517 | } |
||
518 | |||
519 | 24 | $fieldConfiguration = self::getDescriptionConfiguration($annotations, true) + $fieldConfiguration; |
|
520 | |||
521 | 24 | $args = []; |
|
522 | |||
523 | 24 | foreach ($fieldAnnotation->args as $arg) { |
|
524 | 24 | $args[$arg->name] = ['type' => $arg->type]; |
|
525 | |||
526 | 24 | if (isset($arg->description)) { |
|
527 | 24 | $args[$arg->name]['description'] = $arg->description; |
|
528 | } |
||
529 | |||
530 | 24 | if (isset($arg->default)) { |
|
531 | 24 | $args[$arg->name]['defaultValue'] = $arg->default; |
|
532 | } |
||
533 | } |
||
534 | |||
535 | 24 | if (empty($fieldAnnotation->args) && $reflector instanceof ReflectionMethod) { |
|
536 | 24 | $args = self::guessArgs($reflector); |
|
537 | } |
||
538 | |||
539 | 24 | if (!empty($args)) { |
|
540 | 24 | $fieldConfiguration['args'] = $args; |
|
541 | } |
||
542 | |||
543 | 24 | $fieldName = $fieldAnnotation->name ?? $fieldName; |
|
544 | |||
545 | 24 | if (isset($fieldAnnotation->resolve)) { |
|
546 | 24 | $fieldConfiguration['resolve'] = self::formatExpression($fieldAnnotation->resolve); |
|
547 | } else { |
||
548 | 24 | if ($reflector instanceof ReflectionMethod) { |
|
549 | 24 | $fieldConfiguration['resolve'] = self::formatExpression(sprintf('call(%s.%s, %s)', $currentValue, $reflector->getName(), self::formatArgsForExpression($args))); |
|
550 | } else { |
||
551 | 24 | if ($fieldName !== $reflector->getName() || 'value' !== $currentValue) { |
|
552 | $fieldConfiguration['resolve'] = self::formatExpression(sprintf('%s.%s', $currentValue, $reflector->getName())); |
||
553 | } |
||
554 | } |
||
555 | } |
||
556 | |||
557 | 24 | if ($fieldAnnotation->argsBuilder) { |
|
558 | 24 | if (is_string($fieldAnnotation->argsBuilder)) { |
|
559 | $fieldConfiguration['argsBuilder'] = $fieldAnnotation->argsBuilder; |
||
560 | 24 | } elseif (is_array($fieldAnnotation->argsBuilder)) { |
|
561 | 24 | list($builder, $builderConfig) = $fieldAnnotation->argsBuilder; |
|
562 | 24 | $fieldConfiguration['argsBuilder'] = ['builder' => $builder, 'config' => $builderConfig]; |
|
563 | } else { |
||
564 | throw new InvalidArgumentException(sprintf('The attribute "argsBuilder" on GraphQL annotation "@%s" defined on "%s" must be a string or an array where first index is the builder name and the second is the config.', $fieldAnnotationName, $reflector->getName())); |
||
565 | } |
||
566 | } |
||
567 | |||
568 | 24 | if ($fieldAnnotation->fieldBuilder) { |
|
569 | 24 | if (is_string($fieldAnnotation->fieldBuilder)) { |
|
570 | $fieldConfiguration['builder'] = $fieldAnnotation->fieldBuilder; |
||
571 | 24 | } elseif (is_array($fieldAnnotation->fieldBuilder)) { |
|
572 | 24 | list($builder, $builderConfig) = $fieldAnnotation->fieldBuilder; |
|
573 | 24 | $fieldConfiguration['builder'] = $builder; |
|
574 | 24 | $fieldConfiguration['builderConfig'] = $builderConfig ?: []; |
|
575 | } else { |
||
576 | 24 | throw new InvalidArgumentException(sprintf('The attribute "fieldBuilder" on GraphQL annotation "@%s" defined on "%s" must be a string or an array where first index is the builder name and the second is the config.', $fieldAnnotationName, $reflector->getName())); |
|
577 | } |
||
578 | } else { |
||
579 | 24 | if (!isset($fieldAnnotation->type)) { |
|
580 | 24 | if ($reflector instanceof ReflectionMethod) { |
|
581 | /** @var ReflectionMethod $reflector */ |
||
582 | 24 | if ($reflector->hasReturnType()) { |
|
583 | try { |
||
584 | // @phpstan-ignore-next-line |
||
585 | 24 | $fieldConfiguration['type'] = self::resolveGraphQLTypeFromReflectionType($reflector->getReturnType(), self::VALID_OUTPUT_TYPES); |
|
586 | } catch (Exception $e) { |
||
587 | 24 | throw new InvalidArgumentException(sprintf('The attribute "type" on GraphQL annotation "@%s" is missing on method "%s" and cannot be auto-guessed from type hint "%s"', $fieldAnnotationName, $reflector->getName(), (string) $reflector->getReturnType())); |
|
588 | } |
||
589 | } else { |
||
590 | 24 | throw new InvalidArgumentException(sprintf('The attribute "type" on GraphQL annotation "@%s" is missing on method "%s" and cannot be auto-guessed as there is not return type hint.', $fieldAnnotationName, $reflector->getName())); |
|
591 | } |
||
592 | } else { |
||
593 | try { |
||
594 | 24 | $fieldConfiguration['type'] = self::guessType($graphClass, $annotations); |
|
595 | 2 | } catch (Exception $e) { |
|
596 | 2 | throw new InvalidArgumentException(sprintf('The attribute "type" on "@%s" defined on "%s" is required and cannot be auto-guessed : %s.', $fieldAnnotationName, $reflector->getName(), $e->getMessage())); |
|
597 | } |
||
598 | } |
||
599 | } |
||
600 | } |
||
601 | |||
602 | 24 | if (null !== $accessAnnotation) { |
|
603 | 24 | if (isset($accessAnnotation->value)) { |
|
604 | 24 | $fieldConfiguration['access'] = self::formatExpression($accessAnnotation->value); |
|
605 | } |
||
606 | 24 | if (isset($accessAnnotation->nullOnDenied)) { |
|
607 | 24 | $fieldConfiguration['accessConfig'] = ['nullOnDenied' => $accessAnnotation->nullOnDenied]; |
|
608 | } |
||
609 | } |
||
610 | |||
611 | 24 | if ($publicAnnotation) { |
|
612 | 24 | $fieldConfiguration['public'] = self::formatExpression($publicAnnotation->value); |
|
613 | } |
||
614 | |||
615 | 24 | if ($fieldAnnotation->complexity) { |
|
616 | 24 | $fieldConfiguration['complexity'] = self::formatExpression($fieldAnnotation->complexity); |
|
617 | } |
||
618 | |||
619 | 24 | return [$fieldName => $fieldConfiguration]; |
|
620 | } |
||
621 | |||
622 | /** |
||
623 | * Create GraphQL input fields configuration based on annotations. |
||
624 | * |
||
625 | * @param ReflectionProperty[] $reflectors |
||
626 | * |
||
627 | * @throws AnnotationException |
||
628 | */ |
||
629 | 24 | private static function getGraphQLInputFieldsFromAnnotations(GraphClass $graphClass, array $reflectors): array |
|
630 | { |
||
631 | 24 | $fields = []; |
|
632 | |||
633 | 24 | foreach ($reflectors as $reflector) { |
|
634 | 24 | $annotations = $graphClass->getAnnotations($reflector); |
|
635 | |||
636 | /** @var GQL\Field $fieldAnnotation */ |
||
637 | 24 | $fieldAnnotation = self::getFirstAnnotationMatching($annotations, GQL\Field::class); |
|
638 | |||
639 | // Ignore field with resolver when the type is an Input |
||
640 | 24 | if (isset($fieldAnnotation->resolve)) { |
|
641 | return []; |
||
642 | } |
||
643 | |||
644 | 24 | $fieldName = $reflector->getName(); |
|
645 | 24 | $fieldType = $fieldAnnotation->type; |
|
646 | 24 | $fieldConfiguration = []; |
|
647 | 24 | if ($fieldType) { |
|
648 | // Resolve a PHP class from a GraphQL type |
||
649 | 24 | $resolvedType = self::$classesMap[$fieldType] ?? null; |
|
650 | // We found a type but it is not allowed |
||
651 | 24 | if (null !== $resolvedType && !in_array($resolvedType['type'], self::VALID_INPUT_TYPES)) { |
|
652 | throw new InvalidArgumentException(sprintf('The type "%s" on "%s" is a "%s" not valid on an Input @Field. Only Input, Scalar and Enum are allowed.', $fieldType, $reflector->getName(), $resolvedType['type'])); |
||
653 | } |
||
654 | |||
655 | 24 | $fieldConfiguration['type'] = $fieldType; |
|
656 | } |
||
657 | |||
658 | 24 | $fieldConfiguration = array_merge(self::getDescriptionConfiguration($annotations, true), $fieldConfiguration); |
|
659 | 24 | $fields[$fieldName] = $fieldConfiguration; |
|
660 | } |
||
661 | |||
662 | 24 | return $fields; |
|
663 | } |
||
664 | |||
665 | /** |
||
666 | * Create GraphQL type fields configuration based on annotations. |
||
667 | * |
||
668 | * @phpstan-param class-string<GQL\Field> $fieldAnnotationName |
||
669 | * |
||
670 | * @param ReflectionProperty[]|ReflectionMethod[] $reflectors |
||
671 | * |
||
672 | * @throws AnnotationException |
||
673 | */ |
||
674 | 25 | private static function getGraphQLTypeFieldsFromAnnotations(GraphClass $graphClass, array $reflectors, string $fieldAnnotationName = GQL\Field::class, string $currentValue = 'value'): array |
|
675 | { |
||
676 | 25 | $fields = []; |
|
677 | |||
678 | 25 | foreach ($reflectors as $reflector) { |
|
679 | 24 | $fields = array_merge($fields, self::getTypeFieldConfigurationFromReflector($graphClass, $reflector, $fieldAnnotationName, $currentValue)); |
|
680 | } |
||
681 | |||
682 | 25 | return $fields; |
|
683 | } |
||
684 | |||
685 | /** |
||
686 | * @phpstan-param class-string<GQL\Query|GQL\Mutation> $expectedAnnotation |
||
687 | * |
||
688 | * Return fields config from Provider methods. |
||
689 | * Loop through configured provider and extract fields targeting the targetType. |
||
690 | */ |
||
691 | 25 | private static function getGraphQLFieldsFromProviders(GraphClass $graphClass, string $expectedAnnotation, string $targetType, bool $isDefaultTarget = false): array |
|
692 | { |
||
693 | 25 | $fields = []; |
|
694 | 25 | foreach (self::$providers as ['metadata' => $providerMetadata, 'annotation' => $providerAnnotation]) { |
|
695 | 24 | $defaultAccessAnnotation = self::getFirstAnnotationMatching($providerMetadata->getAnnotations(), GQL\Access::class); |
|
696 | 24 | $defaultIsPublicAnnotation = self::getFirstAnnotationMatching($providerMetadata->getAnnotations(), GQL\IsPublic::class); |
|
697 | |||
698 | 24 | $defaultAccess = isset($defaultAccessAnnotation->value) ? self::formatExpression($defaultAccessAnnotation->value) : false; |
|
699 | 24 | $defaultAccessConfig = isset($defaultAccessAnnotation->nullOnDenied) ? ['nullOnDenied' => $defaultAccessAnnotation->nullOnDenied] : false; |
|
700 | 24 | $defaultIsPublic = $defaultIsPublicAnnotation ? self::formatExpression($defaultIsPublicAnnotation->value) : false; |
|
701 | |||
702 | 24 | $methods = []; |
|
703 | // First found the methods matching the targeted type |
||
704 | 24 | foreach ($providerMetadata->getMethods() as $method) { |
|
705 | 24 | $annotations = $providerMetadata->getAnnotations($method); |
|
706 | |||
707 | 24 | $annotation = self::getFirstAnnotationMatching($annotations, [GQL\Mutation::class, GQL\Query::class]); |
|
708 | 24 | if (null === $annotation) { |
|
709 | continue; |
||
710 | } |
||
711 | |||
712 | 24 | $annotationTargets = $annotation->targetType ?? null; |
|
713 | |||
714 | 24 | if (null === $annotationTargets) { |
|
715 | 24 | if ($isDefaultTarget) { |
|
716 | 24 | $annotationTargets = [$targetType]; |
|
717 | 24 | if (!$annotation instanceof $expectedAnnotation) { |
|
718 | 24 | continue; |
|
719 | } |
||
720 | } else { |
||
721 | 24 | continue; |
|
722 | } |
||
723 | } |
||
724 | |||
725 | 24 | if (!in_array($targetType, $annotationTargets)) { |
|
726 | 24 | continue; |
|
727 | } |
||
728 | |||
729 | 24 | if (!$annotation instanceof $expectedAnnotation) { |
|
730 | 2 | if (GQL\Mutation::class == $expectedAnnotation) { |
|
731 | 1 | $message = sprintf('The provider "%s" try to add a query field on type "%s" (through @Query on method "%s") but "%s" is a mutation.', $providerMetadata->getName(), $targetType, $method->getName(), $targetType); |
|
732 | } else { |
||
733 | 1 | $message = sprintf('The provider "%s" try to add a mutation on type "%s" (through @Mutation on method "%s") but "%s" is not a mutation.', $providerMetadata->getName(), $targetType, $method->getName(), $targetType); |
|
734 | } |
||
735 | |||
736 | 2 | throw new InvalidArgumentException($message); |
|
737 | } |
||
738 | 24 | $methods[$method->getName()] = $method; |
|
739 | } |
||
740 | |||
741 | 24 | $currentValue = sprintf("service('%s')", self::formatNamespaceForExpression($providerMetadata->getName())); |
|
742 | 24 | $providerFields = self::getGraphQLTypeFieldsFromAnnotations($graphClass, $methods, $expectedAnnotation, $currentValue); |
|
743 | 24 | foreach ($providerFields as $fieldName => $fieldConfig) { |
|
744 | 24 | if (isset($providerAnnotation->prefix)) { |
|
745 | 24 | $fieldName = sprintf('%s%s', $providerAnnotation->prefix, $fieldName); |
|
746 | } |
||
747 | |||
748 | 24 | if ($defaultAccess && !isset($fieldConfig['access'])) { |
|
749 | 24 | $fieldConfig['access'] = $defaultAccess; |
|
750 | } |
||
751 | |||
752 | 24 | if ($defaultAccessConfig && !isset($fieldConfig['accessConfig'])) { |
|
753 | 24 | $fieldConfig['accessConfig'] = $defaultAccessConfig; |
|
754 | } |
||
755 | |||
756 | 24 | if ($defaultIsPublic && !isset($fieldConfig['public'])) { |
|
757 | 24 | $fieldConfig['public'] = $defaultIsPublic; |
|
758 | } |
||
759 | |||
760 | 24 | $fields[$fieldName] = $fieldConfig; |
|
761 | } |
||
762 | } |
||
763 | |||
764 | 25 | return $fields; |
|
765 | } |
||
766 | |||
767 | /** |
||
768 | * Get the config for description & deprecation reason. |
||
769 | */ |
||
770 | 25 | private static function getDescriptionConfiguration(array $annotations, bool $withDeprecation = false): array |
|
771 | { |
||
772 | 25 | $config = []; |
|
773 | 25 | $descriptionAnnotation = self::getFirstAnnotationMatching($annotations, GQL\Description::class); |
|
774 | 25 | if (null !== $descriptionAnnotation) { |
|
775 | 24 | $config['description'] = $descriptionAnnotation->value; |
|
776 | } |
||
777 | |||
778 | 25 | if ($withDeprecation) { |
|
779 | 24 | $deprecatedAnnotation = self::getFirstAnnotationMatching($annotations, GQL\Deprecated::class); |
|
780 | 24 | if (null !== $deprecatedAnnotation) { |
|
781 | 24 | $config['deprecationReason'] = $deprecatedAnnotation->value; |
|
782 | } |
||
783 | } |
||
784 | |||
785 | 25 | return $config; |
|
786 | } |
||
787 | |||
788 | /** |
||
789 | * Format an array of args to a list of arguments in an expression. |
||
790 | */ |
||
791 | 24 | private static function formatArgsForExpression(array $args): string |
|
792 | { |
||
793 | 24 | $mapping = []; |
|
794 | 24 | foreach ($args as $name => $config) { |
|
795 | 24 | $mapping[] = sprintf('%s: "%s"', $name, $config['type']); |
|
796 | } |
||
797 | |||
798 | 24 | return sprintf('arguments({%s}, args)', implode(', ', $mapping)); |
|
799 | } |
||
800 | |||
801 | /** |
||
802 | * Format a namespace to be used in an expression (double escape). |
||
803 | */ |
||
804 | 24 | private static function formatNamespaceForExpression(string $namespace): string |
|
805 | { |
||
806 | 24 | return str_replace('\\', '\\\\', $namespace); |
|
807 | } |
||
808 | |||
809 | /** |
||
810 | * Get the first annotation matching given class. |
||
811 | * |
||
812 | * @phpstan-template T of object |
||
813 | * @phpstan-param class-string<T>|class-string<T>[] $annotationClass |
||
814 | * @phpstan-return T|null |
||
815 | * |
||
816 | * @param string|array $annotationClass |
||
817 | * |
||
818 | * @return object|null |
||
819 | */ |
||
820 | 25 | private static function getFirstAnnotationMatching(array $annotations, $annotationClass) |
|
821 | { |
||
822 | 25 | if (is_string($annotationClass)) { |
|
823 | 25 | $annotationClass = [$annotationClass]; |
|
824 | } |
||
825 | |||
826 | 25 | foreach ($annotations as $annotation) { |
|
827 | 25 | foreach ($annotationClass as $class) { |
|
828 | 25 | if ($annotation instanceof $class) { |
|
829 | 24 | return $annotation; |
|
830 | } |
||
831 | } |
||
832 | } |
||
833 | |||
834 | 25 | return null; |
|
835 | } |
||
836 | |||
837 | /** |
||
838 | * Format an expression (ie. add "@=" if not set). |
||
839 | */ |
||
840 | 24 | private static function formatExpression(string $expression): string |
|
841 | { |
||
842 | 24 | return '@=' === substr($expression, 0, 2) ? $expression : sprintf('@=%s', $expression); |
|
843 | } |
||
844 | |||
845 | /** |
||
846 | * Suffix a name if it is not already. |
||
847 | */ |
||
848 | 24 | private static function suffixName(string $name, string $suffix): string |
|
851 | } |
||
852 | |||
853 | /** |
||
854 | * Try to guess a field type base on his annotations. |
||
855 | * |
||
856 | * @throws RuntimeException |
||
857 | */ |
||
858 | 24 | private static function guessType(GraphClass $graphClass, array $annotations): string |
|
859 | { |
||
860 | 24 | $columnAnnotation = self::getFirstAnnotationMatching($annotations, Column::class); |
|
861 | 24 | if (null !== $columnAnnotation) { |
|
862 | 24 | $type = self::resolveTypeFromDoctrineType($columnAnnotation->type); |
|
863 | 24 | $nullable = $columnAnnotation->nullable; |
|
864 | 24 | if ($type) { |
|
865 | 24 | return $nullable ? $type : sprintf('%s!', $type); |
|
866 | } else { |
||
867 | 1 | throw new RuntimeException(sprintf('Unable to auto-guess GraphQL type from Doctrine type "%s"', $columnAnnotation->type)); |
|
868 | } |
||
869 | } |
||
870 | |||
871 | $associationAnnotations = [ |
||
872 | 24 | OneToMany::class => true, |
|
873 | OneToOne::class => false, |
||
874 | ManyToMany::class => true, |
||
875 | ManyToOne::class => false, |
||
876 | ]; |
||
877 | |||
878 | 24 | $associationAnnotation = self::getFirstAnnotationMatching($annotations, array_keys($associationAnnotations)); |
|
879 | 24 | if (null !== $associationAnnotation) { |
|
880 | 24 | $target = self::fullyQualifiedClassName($associationAnnotation->targetEntity, $graphClass->getNamespaceName()); |
|
881 | 24 | $type = self::resolveTypeFromClass($target, ['type']); |
|
882 | |||
883 | 24 | if ($type) { |
|
884 | 24 | $isMultiple = $associationAnnotations[get_class($associationAnnotation)]; |
|
885 | 24 | if ($isMultiple) { |
|
886 | 24 | return sprintf('[%s]!', $type); |
|
887 | } else { |
||
888 | 24 | $isNullable = false; |
|
889 | 24 | $joinColumn = self::getFirstAnnotationMatching($annotations, JoinColumn::class); |
|
890 | 24 | if (null !== $joinColumn) { |
|
891 | 24 | $isNullable = $joinColumn->nullable; |
|
892 | } |
||
893 | |||
894 | 24 | return sprintf('%s%s', $type, $isNullable ? '' : '!'); |
|
895 | } |
||
896 | } else { |
||
897 | 1 | throw new RuntimeException(sprintf('Unable to auto-guess GraphQL type from Doctrine target class "%s" (check if the target class is a GraphQL type itself (with a @GQL\Type annotation).', $target)); |
|
898 | } |
||
899 | } |
||
900 | |||
901 | throw new InvalidArgumentException('No Doctrine ORM annotation found.'); |
||
902 | } |
||
903 | |||
904 | /** |
||
905 | * Resolve a FQN from classname and namespace. |
||
906 | * |
||
907 | * @internal |
||
908 | */ |
||
909 | 24 | public static function fullyQualifiedClassName(string $className, string $namespace): string |
|
910 | { |
||
911 | 24 | if (false === strpos($className, '\\') && $namespace) { |
|
912 | 24 | return $namespace.'\\'.$className; |
|
913 | } |
||
914 | |||
915 | 1 | return $className; |
|
916 | } |
||
917 | |||
918 | /** |
||
919 | * Resolve a GraphQLType from a doctrine type. |
||
920 | */ |
||
921 | 24 | private static function resolveTypeFromDoctrineType(string $doctrineType): ?string |
|
943 | } |
||
944 | } |
||
945 | |||
946 | /** |
||
947 | * Transform a method arguments from reflection to a list of GraphQL argument. |
||
948 | */ |
||
949 | 24 | private static function guessArgs(ReflectionMethod $method): array |
|
950 | { |
||
951 | 24 | $arguments = []; |
|
952 | 24 | foreach ($method->getParameters() as $index => $parameter) { |
|
953 | 24 | if (!$parameter->hasType()) { |
|
954 | 1 | throw new InvalidArgumentException(sprintf('Argument n°%s "$%s" on method "%s" cannot be auto-guessed as there is not type hint.', $index + 1, $parameter->getName(), $method->getName())); |
|
955 | } |
||
956 | |||
957 | try { |
||
958 | // @phpstan-ignore-next-line |
||
959 | 24 | $gqlType = self::resolveGraphQLTypeFromReflectionType($parameter->getType(), self::VALID_INPUT_TYPES, $parameter->isDefaultValueAvailable()); |
|
960 | } catch (Exception $e) { |
||
961 | throw new InvalidArgumentException(sprintf('Argument n°%s "$%s" on method "%s" cannot be auto-guessed : %s".', $index + 1, $parameter->getName(), $method->getName(), $e->getMessage())); |
||
962 | } |
||
963 | |||
964 | 24 | $argumentConfig = []; |
|
965 | 24 | if ($parameter->isDefaultValueAvailable()) { |
|
966 | 24 | $argumentConfig['defaultValue'] = $parameter->getDefaultValue(); |
|
967 | } |
||
968 | |||
969 | 24 | $argumentConfig['type'] = $gqlType; |
|
970 | |||
971 | 24 | $arguments[$parameter->getName()] = $argumentConfig; |
|
972 | } |
||
973 | |||
974 | 24 | return $arguments; |
|
975 | } |
||
976 | |||
977 | 24 | private static function resolveGraphQLTypeFromReflectionType(ReflectionNamedType $type, array $filterGraphQLTypes = [], bool $isOptional = false): string |
|
978 | { |
||
979 | 24 | $sType = $type->getName(); |
|
980 | 24 | if ($type->isBuiltin()) { |
|
981 | 24 | $gqlType = self::resolveTypeFromPhpType($sType); |
|
982 | 24 | if (null === $gqlType) { |
|
983 | 24 | throw new RuntimeException(sprintf('No corresponding GraphQL type found for builtin type "%s"', $sType)); |
|
984 | } |
||
985 | } else { |
||
986 | 24 | $gqlType = self::resolveTypeFromClass($sType, $filterGraphQLTypes); |
|
987 | 24 | if (null === $gqlType) { |
|
988 | throw new RuntimeException(sprintf('No corresponding GraphQL %s found for class "%s"', $filterGraphQLTypes ? implode(',', $filterGraphQLTypes) : 'object', $sType)); |
||
989 | } |
||
990 | } |
||
991 | |||
992 | 24 | return sprintf('%s%s', $gqlType, ($type->allowsNull() || $isOptional) ? '' : '!'); |
|
993 | } |
||
994 | |||
995 | /** |
||
996 | * Resolve a GraphQL Type from a class name. |
||
997 | */ |
||
998 | 24 | private static function resolveTypeFromClass(string $className, array $wantedTypes = []): ?string |
|
1009 | } |
||
1010 | |||
1011 | /** |
||
1012 | * Search the classes map for class by predicate. |
||
1013 | * |
||
1014 | * @return array |
||
1015 | */ |
||
1016 | 25 | private static function searchClassesMapBy(callable $predicate, string $type) |
|
1017 | { |
||
1018 | 25 | $classNames = []; |
|
1019 | 25 | foreach (self::$classesMap as $gqlType => $config) { |
|
1020 | 25 | if ($config['type'] !== $type) { |
|
1021 | 25 | continue; |
|
1022 | } |
||
1023 | |||
1024 | 24 | if ($predicate($gqlType, $config)) { |
|
1025 | 24 | $classNames[$gqlType] = $config; |
|
1026 | } |
||
1027 | } |
||
1028 | |||
1029 | 25 | return $classNames; |
|
1030 | } |
||
1031 | |||
1032 | /** |
||
1033 | * Convert a PHP Builtin type to a GraphQL type. |
||
1034 | */ |
||
1035 | 24 | private static function resolveTypeFromPhpType(string $phpType): ?string |
|
1051 | } |
||
1052 | } |
||
1053 | } |
||
1054 |