1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Definition\Loader\Annotation; |
12
|
|
|
|
13
|
|
|
use Ynlo\GraphQLBundle\Annotation; |
14
|
|
|
use Ynlo\GraphQLBundle\Definition\FieldDefinition; |
15
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
16
|
|
|
use Ynlo\GraphQLBundle\Util\ClassUtils; |
17
|
|
|
use Ynlo\GraphQLBundle\Util\TypeUtil; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Resolve field of types queries using naming conventions |
21
|
|
|
*/ |
22
|
|
|
class StandaloneFieldParser extends QueryAnnotationParser |
23
|
|
|
{ |
24
|
|
|
use AnnotationReaderAwareTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
1 |
|
public function supports($annotation): bool |
30
|
|
|
{ |
31
|
1 |
|
return $annotation instanceof Annotation\Field; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
1 |
|
public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint) |
38
|
|
|
{ |
39
|
|
|
/** @var Annotation\Field $annotation */ |
40
|
|
|
|
41
|
1 |
|
$field = new FieldDefinition(); |
42
|
|
|
|
43
|
1 |
View Code Duplication |
if ($annotation->name) { |
|
|
|
|
44
|
|
|
$field->setName($annotation->name); |
45
|
|
|
} else { |
46
|
1 |
|
$field->setName(lcfirst(ClassUtils::getDefaultName($refClass->getName()))); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
$objectType = null; |
|
|
|
|
50
|
1 |
|
preg_match('/(\w+)\\\\Field\\\\(\w+)$/', $refClass->getName(), $matches); |
51
|
1 |
|
if (!isset($matches[1]) || !$endpoint->hasType($matches[1])) { |
52
|
|
|
$error = sprintf('Can`t resolve a valid object type for field "%s"', $refClass->getName()); |
53
|
|
|
throw new \RuntimeException($error); |
54
|
|
|
} |
55
|
1 |
|
$objectDefinition = $endpoint->getType($matches[1]); |
56
|
1 |
|
$objectDefinition->addField($field); |
57
|
|
|
|
58
|
1 |
|
$argAnnotations = $this->reader->getClassAnnotations($refClass); |
59
|
1 |
|
foreach ($argAnnotations as $argAnnotation) { |
60
|
1 |
|
if ($argAnnotation instanceof Annotation\Argument) { |
61
|
1 |
|
$this->resolveArgument($field, $argAnnotation); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
$field->setType(TypeUtil::normalize($annotation->type)); |
66
|
1 |
|
$field->setList(TypeUtil::isTypeList($annotation->type)); |
67
|
1 |
|
$field->setResolver($annotation->resolver ?? $refClass->getName()); |
|
|
|
|
68
|
1 |
|
$field->setDeprecationReason($annotation->deprecationReason); |
69
|
1 |
|
$field->setDescription($annotation->description); |
70
|
|
|
|
71
|
1 |
|
foreach ($annotation->options as $option => $value) { |
72
|
1 |
|
$field->setMeta($option, $value); |
73
|
|
|
} |
74
|
1 |
|
} |
75
|
|
|
} |
76
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.