|
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\Type\Registry; |
|
12
|
|
|
|
|
13
|
|
|
use GraphQL\Type\Definition\Type; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
16
|
|
|
use Ynlo\GraphQLBundle\Definition\EnumDefinition; |
|
17
|
|
|
use Ynlo\GraphQLBundle\Definition\InputObjectDefinition; |
|
18
|
|
|
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition; |
|
19
|
|
|
use Ynlo\GraphQLBundle\Definition\ObjectDefinition; |
|
20
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
|
21
|
|
|
use Ynlo\GraphQLBundle\Definition\UnionDefinition; |
|
22
|
|
|
use Ynlo\GraphQLBundle\Type\Definition\EndpointAwareInterface; |
|
23
|
|
|
use Ynlo\GraphQLBundle\Type\Definition\EnumDefinitionType; |
|
24
|
|
|
use Ynlo\GraphQLBundle\Type\Definition\InputObjectDefinitionType; |
|
25
|
|
|
use Ynlo\GraphQLBundle\Type\Definition\InterfaceDefinitionType; |
|
26
|
|
|
use Ynlo\GraphQLBundle\Type\Definition\ObjectDefinitionType; |
|
27
|
|
|
use Ynlo\GraphQLBundle\Type\Definition\UnionDefinitionType; |
|
28
|
|
|
use Ynlo\GraphQLBundle\Type\Types; |
|
29
|
|
|
|
|
30
|
|
|
class TypeRegistry |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var Endpoint |
|
34
|
|
|
*/ |
|
35
|
|
|
protected static $endpoint; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var ContainerInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
protected static $container; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var Type[] |
|
44
|
|
|
*/ |
|
45
|
|
|
protected static $types = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
protected static $typesMap = []; |
|
51
|
|
|
|
|
52
|
|
|
public static function setUp(ContainerInterface $container, Endpoint $endpoint) |
|
53
|
|
|
{ |
|
54
|
|
|
self::$container = $container; |
|
55
|
|
|
self::$endpoint = $endpoint; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Clear all registered instantiated types |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function clear() |
|
62
|
|
|
{ |
|
63
|
|
|
self::$types = []; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $name |
|
68
|
|
|
* |
|
69
|
|
|
* @return Type |
|
70
|
|
|
* |
|
71
|
|
|
* @throws \UnexpectedValueException if not valid type found |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public static function get($name): Type |
|
74
|
|
|
{ |
|
75
|
|
|
//internal type |
|
76
|
1 |
|
if ($internalType = self::getInternalType($name)) { |
|
77
|
|
|
return $internalType; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
//convert FQCN into type, |
|
81
|
|
|
//allowing the use of FQCN for GraphQL scalar types |
|
82
|
1 |
|
if (class_exists($name) || interface_exists($name)) { |
|
83
|
|
|
if (\in_array($name, self::$typesMap, true)) { |
|
84
|
|
|
$name = array_flip(self::$typesMap)[$name]; |
|
85
|
|
|
} elseif (self::$endpoint->hasTypeForClass($name)) { |
|
86
|
|
|
$name = self::$endpoint->getTypeForClass($name); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
if (!self::has($name)) { |
|
91
|
|
|
self::create($name); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
if (self::has($name)) { |
|
95
|
1 |
|
return self::$types[$name]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
throw new InvalidTypeException($name); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
public static function create(string $name): void |
|
102
|
|
|
{ |
|
103
|
1 |
|
$type = null; |
|
104
|
|
|
|
|
105
|
|
|
//create using auto-loaded types |
|
106
|
1 |
|
if (array_key_exists($name, self::$typesMap)) { |
|
107
|
1 |
|
$class = self::$typesMap[$name]; |
|
108
|
|
|
|
|
109
|
|
|
/** @var Type $type */ |
|
110
|
1 |
|
$type = new $class(); |
|
111
|
1 |
|
if (self::$container && $type instanceof ContainerAwareInterface) { |
|
112
|
|
|
$type->setContainer(self::$container); |
|
113
|
|
|
} |
|
114
|
1 |
|
if (self::$endpoint && $type instanceof EndpointAwareInterface) { |
|
115
|
|
|
$type->setEndpoint(self::$endpoint); |
|
116
|
|
|
} |
|
117
|
1 |
|
self::set($name, $type); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
//create using definition endpoint |
|
121
|
1 |
|
if (self::$endpoint && self::$endpoint->hasType($name)) { |
|
122
|
|
|
$definition = self::$endpoint->getType($name); |
|
123
|
|
|
|
|
124
|
|
|
if ($definition instanceof ObjectDefinition) { |
|
125
|
|
|
$type = new ObjectDefinitionType($definition); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if ($definition instanceof UnionDefinition) { |
|
129
|
|
|
$type = new UnionDefinitionType($definition); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if ($definition instanceof InputObjectDefinition) { |
|
133
|
|
|
$type = new InputObjectDefinitionType($definition); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if ($definition instanceof InterfaceDefinition) { |
|
137
|
|
|
$type = new InterfaceDefinitionType($definition); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if ($definition instanceof EnumDefinition) { |
|
141
|
|
|
$type = new EnumDefinitionType($definition); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if ($type instanceof ContainerAwareInterface) { |
|
145
|
|
|
$type->setContainer(self::$container); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if ($type instanceof EndpointAwareInterface) { |
|
149
|
|
|
$type->setEndpoint(self::$endpoint); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
if (null !== $type) { |
|
153
|
|
|
self::set($name, $type); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
1 |
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @return Type[] |
|
160
|
|
|
*/ |
|
161
|
|
|
public static function all() |
|
162
|
|
|
{ |
|
163
|
|
|
return self::$types; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param string $name |
|
168
|
|
|
* @param Type $type |
|
169
|
|
|
*/ |
|
170
|
1 |
|
public static function set($name, Type $type) |
|
171
|
|
|
{ |
|
172
|
1 |
|
self::$types[$name] = $type; |
|
173
|
1 |
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param string $name |
|
177
|
|
|
* |
|
178
|
|
|
* @return bool |
|
179
|
|
|
*/ |
|
180
|
1 |
|
public static function has($name) |
|
181
|
|
|
{ |
|
182
|
|
|
//convert FQN into type, |
|
183
|
|
|
//allowing the use of FQN for GraphQL scalar types |
|
184
|
1 |
|
if (class_exists($name) || interface_exists($name)) { |
|
185
|
|
|
if (\in_array($name, self::$typesMap, true)) { |
|
186
|
|
|
$name = array_flip(self::$typesMap)[$name]; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
1 |
|
if (!array_key_exists($name, self::$types) && array_key_exists($name, self::$typesMap)) { |
|
191
|
1 |
|
static::create($name); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
1 |
|
return array_key_exists($name, self::$types); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Add type mapping information to use with the autoloader |
|
199
|
|
|
* |
|
200
|
|
|
* @param string $name |
|
201
|
|
|
* @param string $class |
|
202
|
|
|
*/ |
|
203
|
1 |
|
public static function addTypeMapping($name, $class) |
|
204
|
|
|
{ |
|
205
|
1 |
|
self::$typesMap[$name] = $class; |
|
206
|
1 |
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param array $map |
|
210
|
|
|
*/ |
|
211
|
|
|
public static function setTypeMapping($map) |
|
212
|
|
|
{ |
|
213
|
|
|
self::$typesMap = $map; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @return array |
|
218
|
|
|
*/ |
|
219
|
|
|
public static function getTypeMapp() |
|
220
|
|
|
{ |
|
221
|
|
|
return self::$typesMap; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @param string $name |
|
226
|
|
|
* |
|
227
|
|
|
* @return Type |
|
228
|
|
|
*/ |
|
229
|
1 |
|
private static function getInternalType($name): ?Type |
|
230
|
|
|
{ |
|
231
|
1 |
|
switch ($name) { |
|
232
|
|
|
case Types::STRING: |
|
233
|
1 |
|
case 'string': |
|
234
|
|
|
return Type::string(); |
|
235
|
|
|
case Types::BOOLEAN: |
|
236
|
1 |
|
case 'boolean': |
|
237
|
1 |
|
case 'bool': |
|
238
|
|
|
return Type::boolean(); |
|
239
|
|
|
case Types::INT: |
|
240
|
1 |
|
case 'int': |
|
241
|
1 |
|
case 'integer': |
|
242
|
|
|
return Type::int(); |
|
243
|
|
|
case Types::FLOAT: |
|
244
|
1 |
|
case 'float': |
|
245
|
1 |
|
case 'decimal': |
|
246
|
1 |
|
case 'double': |
|
247
|
|
|
return Type::float(); |
|
248
|
|
|
case Types::ID: |
|
249
|
1 |
|
case 'id': |
|
250
|
1 |
|
case 'ID': |
|
251
|
|
|
return Type::id(); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
1 |
|
return null; |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|