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\Type\Definition\EndpointAwareInterface; |
22
|
|
|
use Ynlo\GraphQLBundle\Type\Definition\EnumDefinitionType; |
23
|
|
|
use Ynlo\GraphQLBundle\Type\Definition\InputObjectDefinitionType; |
24
|
|
|
use Ynlo\GraphQLBundle\Type\Definition\InterfaceDefinitionType; |
25
|
|
|
use Ynlo\GraphQLBundle\Type\Definition\ObjectDefinitionType; |
26
|
|
|
use Ynlo\GraphQLBundle\Type\Types; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class Types |
30
|
|
|
*/ |
31
|
|
|
class TypeRegistry |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var Endpoint |
35
|
|
|
*/ |
36
|
|
|
protected static $endpoint; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ContainerInterface |
40
|
|
|
*/ |
41
|
|
|
protected static $container; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Type[] |
45
|
|
|
*/ |
46
|
|
|
protected static $types = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected static $typesMap = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param ContainerInterface $container |
55
|
|
|
* @param Endpoint $endpoint |
56
|
|
|
*/ |
57
|
21 |
|
public static function setUp(ContainerInterface $container, Endpoint $endpoint) |
58
|
|
|
{ |
59
|
21 |
|
self::$container = $container; |
60
|
21 |
|
self::$endpoint = $endpoint; |
61
|
21 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $name |
65
|
|
|
* |
66
|
|
|
* @return Type |
67
|
|
|
* |
68
|
|
|
* @throws \UnexpectedValueException if not valid type found |
69
|
|
|
*/ |
70
|
21 |
|
public static function get($name): Type |
71
|
|
|
{ |
72
|
|
|
//internal type |
73
|
21 |
|
if ($internalType = self::getInternalType($name)) { |
74
|
1 |
|
return $internalType; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
//convert FQN into type, |
78
|
|
|
//allowing the use of FQN for GraphQL scalar types |
79
|
21 |
|
if (class_exists($name) || interface_exists($name)) { |
80
|
21 |
|
if (in_array($name, self::$typesMap)) { |
81
|
1 |
|
$name = array_flip(self::$typesMap)[$name]; |
82
|
21 |
|
} elseif (self::$endpoint->hasTypeForClass($name)) { |
83
|
1 |
|
$name = self::$endpoint->getTypeForClass($name); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
21 |
|
if (!self::has($name)) { |
88
|
1 |
|
self::create($name); |
89
|
|
|
} |
90
|
|
|
|
91
|
21 |
|
if (self::has($name)) { |
92
|
21 |
|
return self::$types[$name]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
throw new \UnexpectedValueException(sprintf('Can`t find a valid type for given type "%s"', $name)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $name |
100
|
|
|
*/ |
101
|
1 |
|
public static function create($name) |
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
|
1 |
|
$type->setContainer(self::$container); |
113
|
|
|
} |
114
|
1 |
|
if (self::$endpoint && $type instanceof EndpointAwareInterface) { |
115
|
1 |
|
$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
|
1 |
|
$definition = self::$endpoint->getType($name); |
123
|
|
|
|
124
|
1 |
|
if ($definition instanceof ObjectDefinition) { |
125
|
1 |
|
$type = new ObjectDefinitionType($definition); |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
if ($definition instanceof InputObjectDefinition) { |
129
|
1 |
|
$type = new InputObjectDefinitionType($definition); |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
if ($definition instanceof InterfaceDefinition) { |
133
|
1 |
|
$type = new InterfaceDefinitionType($definition); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
if ($definition instanceof EnumDefinition) { |
137
|
1 |
|
$type = new EnumDefinitionType($definition); |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
if ($type instanceof ContainerAwareInterface) { |
141
|
1 |
|
$type->setContainer(self::$container); |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
if ($type instanceof EndpointAwareInterface) { |
145
|
1 |
|
$type->setEndpoint(self::$endpoint); |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
if (null !== $type) { |
149
|
1 |
|
self::set($name, $type); |
150
|
|
|
} |
151
|
|
|
} |
152
|
1 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return Type[] |
156
|
|
|
*/ |
157
|
21 |
|
public static function all() |
158
|
|
|
{ |
159
|
21 |
|
return self::$types; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $name |
164
|
|
|
* @param Type $type |
165
|
|
|
*/ |
166
|
1 |
|
public static function set($name, Type $type) |
167
|
|
|
{ |
168
|
1 |
|
self::$types[$name] = $type; |
169
|
1 |
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $name |
173
|
|
|
* |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
21 |
|
public static function has($name) |
177
|
|
|
{ |
178
|
|
|
//convert FQN into type, |
179
|
|
|
//allowing the use of FQN for GraphQL scalar types |
180
|
21 |
|
if (class_exists($name) || interface_exists($name)) { |
181
|
21 |
|
if (in_array($name, self::$typesMap)) { |
182
|
|
|
$name = array_flip(self::$typesMap)[$name]; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
21 |
|
return array_key_exists($name, self::$types); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Add type mapping information to use with the autoloader |
191
|
|
|
* |
192
|
|
|
* @param string $name |
193
|
|
|
* @param string $class |
194
|
|
|
*/ |
195
|
1 |
|
public static function addTypeMapping($name, $class) |
196
|
|
|
{ |
197
|
1 |
|
self::$typesMap[$name] = $class; |
198
|
1 |
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param string $name |
202
|
|
|
* |
203
|
|
|
* @return Type |
204
|
|
|
*/ |
205
|
21 |
|
private static function getInternalType($name): ?Type |
206
|
|
|
{ |
207
|
|
|
switch ($name) { |
208
|
21 |
|
case Types::STRING: |
209
|
21 |
|
case 'string': |
210
|
1 |
|
return Type::string(); |
211
|
21 |
|
case Types::BOOLEAN: |
212
|
21 |
|
case 'boolean': |
213
|
21 |
|
case 'bool': |
214
|
1 |
|
return Type::boolean(); |
215
|
21 |
|
case Types::INT: |
216
|
21 |
|
case 'int': |
217
|
21 |
|
case 'integer': |
218
|
1 |
|
return Type::int(); |
219
|
21 |
|
case Types::FLOAT: |
220
|
21 |
|
case 'float': |
221
|
21 |
|
case 'decimal': |
222
|
21 |
|
case 'double': |
223
|
|
|
return Type::float(); |
224
|
21 |
|
case Types::ID: |
225
|
21 |
|
case 'id': |
226
|
21 |
|
case 'ID': |
227
|
1 |
|
return Type::id(); |
228
|
|
|
} |
229
|
|
|
|
230
|
21 |
|
return null; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|