|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQL\Doctrine\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use GraphQL\Doctrine\Attribute\Argument; |
|
8
|
|
|
use GraphQL\Doctrine\Attribute\Field; |
|
9
|
|
|
use GraphQL\Doctrine\DocBlockReader; |
|
10
|
|
|
use GraphQL\Doctrine\Exception; |
|
11
|
|
|
use GraphQL\Type\Definition\Type; |
|
12
|
|
|
use ReflectionMethod; |
|
13
|
|
|
use ReflectionNamedType; |
|
14
|
|
|
use ReflectionParameter; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* A factory to create a configuration for all getters of an entity. |
|
18
|
|
|
*/ |
|
19
|
|
|
final class OutputFieldsConfigurationFactory extends AbstractFieldsConfigurationFactory |
|
20
|
|
|
{ |
|
21
|
16 |
|
protected function getMethodPattern(): string |
|
22
|
|
|
{ |
|
23
|
16 |
|
return '~^(get|is|has)[A-Z]~'; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get the entire configuration for a method. |
|
28
|
|
|
*/ |
|
29
|
16 |
|
protected function methodToConfiguration(ReflectionMethod $method): array |
|
30
|
|
|
{ |
|
31
|
|
|
// Get a field from attribute, or an empty one |
|
32
|
16 |
|
$field = $this->reader->getAttribute($method, Field::class) ?? new Field(); |
|
33
|
|
|
|
|
34
|
16 |
|
if (!$field->type instanceof Type) { |
|
35
|
16 |
|
$field->type = $this->getTypeFromPhpDeclaration($method->getDeclaringClass(), $field->type); |
|
36
|
16 |
|
$this->completeField($field, $method); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
11 |
|
return $field->toArray(); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Complete field with info from doc blocks and type hints. |
|
44
|
|
|
*/ |
|
45
|
16 |
|
private function completeField(Field $field, ReflectionMethod $method): void |
|
46
|
|
|
{ |
|
47
|
16 |
|
$fieldName = lcfirst(preg_replace('~^get~', '', $method->getName()) ?? ''); |
|
48
|
16 |
|
if (!$field->name) { |
|
49
|
16 |
|
$field->name = $fieldName; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
16 |
|
$docBlock = new DocBlockReader($method); |
|
53
|
16 |
|
if (!$field->description) { |
|
54
|
16 |
|
$field->description = $docBlock->getMethodDescription(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
16 |
|
$this->completeFieldArguments($field, $method, $docBlock); |
|
58
|
13 |
|
$this->completeFieldType($field, $method, $fieldName, $docBlock); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Complete arguments configuration from existing type hints. |
|
63
|
|
|
*/ |
|
64
|
16 |
|
private function completeFieldArguments(Field $field, ReflectionMethod $method, DocBlockReader $docBlock): void |
|
65
|
|
|
{ |
|
66
|
16 |
|
$args = []; |
|
67
|
16 |
|
foreach ($method->getParameters() as $param) { |
|
68
|
|
|
// Either get existing, or create new argument |
|
69
|
10 |
|
$arg = $this->reader->getAttribute($param, Argument::class) ?? new Argument(); |
|
70
|
10 |
|
$arg->setName($param->getName()); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
10 |
|
$arg->setTypeInstance($this->getTypeFromPhpDeclaration($method->getDeclaringClass(), $arg->getType())); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
10 |
|
$args[$param->getName()] = $arg; |
|
75
|
|
|
|
|
76
|
10 |
|
$this->completeArgumentFromTypeHint($arg, $method, $param, $docBlock); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
13 |
|
$field->args = $args; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Complete a single argument from its type hint. |
|
84
|
|
|
*/ |
|
85
|
10 |
|
private function completeArgumentFromTypeHint(Argument $arg, ReflectionMethod $method, ReflectionParameter $param, DocBlockReader $docBlock): void |
|
86
|
|
|
{ |
|
87
|
10 |
|
if (!$arg->getDescription()) { |
|
88
|
10 |
|
$arg->setDescription($docBlock->getParameterDescription($param)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
10 |
|
if (!$arg->hasDefaultValue() && $param->isDefaultValueAvailable()) { |
|
92
|
6 |
|
$arg->setDefaultValue($param->getDefaultValue()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
10 |
|
$this->completeArgumentTypeFromTypeHint($arg, $method, $param, $docBlock); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Complete a single argument type from its type hint and doc block. |
|
100
|
|
|
*/ |
|
101
|
10 |
|
private function completeArgumentTypeFromTypeHint(Argument $arg, ReflectionMethod $method, ReflectionParameter $param, DocBlockReader $docBlock): void |
|
102
|
|
|
{ |
|
103
|
10 |
|
if (!$arg->getTypeInstance()) { |
|
104
|
9 |
|
$typeDeclaration = $docBlock->getParameterType($param); |
|
105
|
9 |
|
$this->throwIfArray($param, $typeDeclaration); |
|
106
|
9 |
|
$arg->setTypeInstance($this->getTypeFromPhpDeclaration($method->getDeclaringClass(), $typeDeclaration, true)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
10 |
|
$type = $param->getType(); |
|
110
|
10 |
|
if (!$arg->getTypeInstance() && $type instanceof ReflectionNamedType) { |
|
111
|
8 |
|
$this->throwIfArray($param, $type->getName()); |
|
112
|
7 |
|
$arg->setTypeInstance($this->reflectionTypeToType($type, true)); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
9 |
|
$this->nonNullIfHasDefault($arg); |
|
116
|
|
|
|
|
117
|
9 |
|
$this->throwIfNotInputType($param, $arg); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get a GraphQL type instance from dock block return type. |
|
122
|
|
|
*/ |
|
123
|
13 |
|
private function getTypeFromDocBock(ReflectionMethod $method, DocBlockReader $docBlock): ?Type |
|
124
|
|
|
{ |
|
125
|
13 |
|
$typeDeclaration = $docBlock->getReturnType(); |
|
126
|
13 |
|
$blacklist = [ |
|
127
|
13 |
|
'Collection', |
|
128
|
13 |
|
'array', |
|
129
|
13 |
|
]; |
|
130
|
|
|
|
|
131
|
13 |
|
if ($typeDeclaration && !in_array($typeDeclaration, $blacklist, true)) { |
|
132
|
7 |
|
return $this->getTypeFromPhpDeclaration($method->getDeclaringClass(), $typeDeclaration); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
11 |
|
return null; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Complete field type from doc blocks and type hints. |
|
140
|
|
|
*/ |
|
141
|
13 |
|
private function completeFieldType(Field $field, ReflectionMethod $method, string $fieldName, DocBlockReader $docBlock): void |
|
142
|
|
|
{ |
|
143
|
13 |
|
if ($this->isIdentityField($fieldName)) { |
|
144
|
11 |
|
$field->type = Type::nonNull(Type::id()); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
// If still no type, look for docBlock |
|
148
|
13 |
|
if (!$field->type) { |
|
149
|
13 |
|
$field->type = $this->getTypeFromDocBock($method, $docBlock); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
// If still no type, look for type hint |
|
153
|
13 |
|
if (!$field->type) { |
|
154
|
11 |
|
$field->type = $this->getTypeFromReturnTypeHint($method, $fieldName); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// If still no type, cannot continue |
|
158
|
12 |
|
if (!$field->type) { |
|
159
|
1 |
|
throw new Exception('Could not find type for method ' . $this->getMethodFullName($method) . '. Either type hint the return value, or specify the type with `#[API\Field]` attribute.'); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|