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