1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Config\Processor; |
6
|
|
|
|
7
|
|
|
use Overblog\GraphQLBundle\Definition\Builder\MappingInterface; |
8
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\BackwardConnectionArgsDefinition; |
9
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\ConnectionArgsDefinition; |
10
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\ForwardConnectionArgsDefinition; |
11
|
|
|
use Overblog\GraphQLBundle\Relay\Mutation\MutationFieldDefinition; |
12
|
|
|
use Overblog\GraphQLBundle\Relay\Node\GlobalIdFieldDefinition; |
13
|
|
|
use Overblog\GraphQLBundle\Relay\Node\NodeFieldDefinition; |
14
|
|
|
use Overblog\GraphQLBundle\Relay\Node\PluralIdentifyingRootFieldDefinition; |
15
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
16
|
|
|
|
17
|
|
|
final class BuilderProcessor implements ProcessorInterface |
18
|
|
|
{ |
19
|
|
|
public const BUILDER_FIELD_TYPE = 'field'; |
20
|
|
|
public const BUILDER_FIELDS_TYPE = 'fields'; |
21
|
|
|
public const BUILDER_ARGS_TYPE = 'args'; |
22
|
|
|
|
23
|
|
|
public const BUILDER_TYPES = [ |
24
|
|
|
self::BUILDER_FIELD_TYPE, |
25
|
|
|
self::BUILDER_FIELDS_TYPE, |
26
|
|
|
self::BUILDER_ARGS_TYPE, |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** @var MappingInterface[] */ |
30
|
|
|
private static $builderClassMap = [ |
31
|
|
|
self::BUILDER_ARGS_TYPE => [ |
32
|
|
|
'Relay::ForwardConnection' => ForwardConnectionArgsDefinition::class, |
33
|
|
|
'Relay::BackwardConnection' => BackwardConnectionArgsDefinition::class, |
34
|
|
|
'Relay::Connection' => ConnectionArgsDefinition::class, |
35
|
|
|
], |
36
|
|
|
self::BUILDER_FIELD_TYPE => [ |
37
|
|
|
'Relay::Mutation' => MutationFieldDefinition::class, |
38
|
|
|
'Relay::GlobalId' => GlobalIdFieldDefinition::class, |
39
|
|
|
'Relay::Node' => NodeFieldDefinition::class, |
40
|
|
|
'Relay::PluralIdentifyingRoot' => PluralIdentifyingRootFieldDefinition::class, |
41
|
|
|
], |
42
|
|
|
self::BUILDER_FIELDS_TYPE => [], |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
39 |
|
public static function process(array $configs): array |
49
|
|
|
{ |
50
|
39 |
|
$addedTypes = []; |
51
|
|
|
|
52
|
39 |
|
foreach ($configs as &$config) { |
53
|
39 |
|
if (isset($config['config']['builders']) && \is_array($config['config']['builders'])) { |
54
|
1 |
|
['fields' => $buildersFields, 'types' => $buildersTypes] = self::processFieldsBuilders($config['config']['builders']); |
55
|
|
|
|
56
|
1 |
|
$config['config']['fields'] = isset($config['config']['fields']) ? \array_merge($buildersFields, $config['config']['fields']) : $buildersFields; |
57
|
|
|
|
58
|
1 |
|
$addedTypes = \array_merge($addedTypes, $buildersTypes); |
59
|
|
|
|
60
|
1 |
|
unset($config['config']['builders']); |
61
|
|
|
} |
62
|
|
|
|
63
|
39 |
|
if (isset($config['config']['fields']) && \is_array($config['config']['fields'])) { |
64
|
34 |
|
['fields' => $buildersFields, 'types' => $buildersTypes] = self::processFieldBuilders($config['config']['fields']); |
65
|
|
|
|
66
|
32 |
|
$config['config']['fields'] = $buildersFields; |
67
|
|
|
|
68
|
32 |
|
$addedTypes = \array_merge($addedTypes, $buildersTypes); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
37 |
|
return \array_merge($configs, $addedTypes); |
73
|
|
|
} |
74
|
|
|
|
75
|
11 |
|
public static function addBuilderClass($name, $type, $builderClass): void |
76
|
|
|
{ |
77
|
11 |
|
self::checkBuilderClass($builderClass, $type); |
78
|
1 |
|
self::$builderClassMap[$type][$name] = $builderClass; |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $builderClass |
83
|
|
|
* @param string $type |
84
|
|
|
*/ |
85
|
11 |
|
private static function checkBuilderClass($builderClass, $type): void |
86
|
|
|
{ |
87
|
11 |
|
$interface = MappingInterface::class; |
88
|
|
|
|
89
|
11 |
|
if (!\is_string($builderClass)) { |
|
|
|
|
90
|
5 |
|
throw new \InvalidArgumentException( |
91
|
5 |
|
\sprintf('%s builder class should be string, but %s given.', \ucfirst($type), \gettype($builderClass)) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
6 |
|
if (!\class_exists($builderClass)) { |
96
|
2 |
|
throw new \InvalidArgumentException( |
97
|
2 |
|
\sprintf('%s builder class "%s" not found.', \ucfirst($type), $builderClass) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
4 |
|
if (!\is_subclass_of($builderClass, $interface)) { |
102
|
3 |
|
throw new \InvalidArgumentException( |
103
|
3 |
|
\sprintf( |
104
|
3 |
|
'%s builder class should implement "%s", but "%s" given.', |
105
|
3 |
|
\ucfirst($type), |
106
|
3 |
|
$interface, |
107
|
3 |
|
$builderClass |
108
|
|
|
) |
109
|
|
|
); |
110
|
|
|
} |
111
|
1 |
|
} |
112
|
|
|
|
113
|
34 |
|
private static function processFieldBuilders(array $fields) |
114
|
|
|
{ |
115
|
34 |
|
$types = []; |
116
|
|
|
|
117
|
34 |
|
foreach ($fields as &$field) { |
118
|
34 |
|
$fieldBuilderName = null; |
119
|
|
|
|
120
|
34 |
|
if (isset($field['builder']) && \is_string($field['builder'])) { |
121
|
9 |
|
$fieldBuilderName = $field['builder']; |
122
|
9 |
|
unset($field['builder']); |
123
|
33 |
|
} elseif (\is_string($field)) { |
124
|
2 |
|
@\trigger_error( |
125
|
|
|
'The builder short syntax (Field: Builder => Field: {builder: Builder}) is deprecated as of 0.7 and will be removed in 0.12. '. |
126
|
2 |
|
'It will be replaced by the field type short syntax (Field: Type => Field: {type: Type})', |
127
|
2 |
|
\E_USER_DEPRECATED |
128
|
|
|
); |
129
|
2 |
|
$fieldBuilderName = $field; |
130
|
|
|
} |
131
|
|
|
|
132
|
34 |
|
$builderConfig = []; |
133
|
34 |
|
if (isset($field['builderConfig'])) { |
134
|
8 |
|
if (\is_array($field['builderConfig'])) { |
135
|
8 |
|
$builderConfig = $field['builderConfig']; |
136
|
|
|
} |
137
|
8 |
|
unset($field['builderConfig']); |
138
|
|
|
} |
139
|
|
|
|
140
|
34 |
|
if ($fieldBuilderName) { |
141
|
9 |
|
$mapping = self::getBuilder($fieldBuilderName, self::BUILDER_FIELD_TYPE)->toMappingDefinition($builderConfig); |
142
|
|
|
|
143
|
8 |
|
$fieldMapping = $mapping['field'] ?? $mapping; |
144
|
8 |
|
$field = \is_array($field) ? \array_merge($fieldMapping, $field) : $fieldMapping; |
145
|
|
|
|
146
|
8 |
|
if (isset($mapping['field'], $mapping['types'])) { |
147
|
1 |
|
$types = \array_merge($types, $mapping['types']); |
148
|
|
|
} |
149
|
|
|
} |
150
|
33 |
|
if (isset($field['argsBuilder'])) { |
151
|
15 |
|
$field = self::processFieldArgumentsBuilders($field); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return [ |
156
|
32 |
|
'fields' => $fields, |
157
|
32 |
|
'types' => $types, |
158
|
|
|
]; |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
private static function processFieldsBuilders(array $builders) |
162
|
|
|
{ |
163
|
1 |
|
$fields = []; |
164
|
1 |
|
$types = []; |
165
|
|
|
|
166
|
1 |
|
foreach ($builders as $builder) { |
167
|
1 |
|
$builderName = $builder['builder']; |
168
|
1 |
|
$builderConfig = null; |
169
|
|
|
|
170
|
1 |
|
if (isset($builder['builderConfig'])) { |
171
|
1 |
|
$builderConfig = $builder['builderConfig']; |
172
|
|
|
} |
173
|
|
|
|
174
|
1 |
|
$mapping = self::getBuilder($builderName, self::BUILDER_FIELDS_TYPE)->toMappingDefinition($builderConfig); |
175
|
|
|
|
176
|
1 |
|
$fieldsMapping = $mapping['fields'] ?? $mapping; |
177
|
1 |
|
$fields = \array_merge($fields, $fieldsMapping); |
178
|
|
|
|
179
|
1 |
|
if (isset($mapping['fields'], $mapping['types'])) { |
180
|
|
|
$types = \array_merge($types, $mapping['types']); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return [ |
185
|
1 |
|
'fields' => $fields, |
186
|
1 |
|
'types' => $types, |
187
|
|
|
]; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $name |
192
|
|
|
* @param string $type |
193
|
|
|
* |
194
|
|
|
* @return MappingInterface |
195
|
|
|
* |
196
|
|
|
* @throws InvalidConfigurationException if builder class not define |
197
|
|
|
*/ |
198
|
22 |
|
private static function getBuilder($name, $type) |
199
|
|
|
{ |
200
|
22 |
|
static $builders = []; |
201
|
22 |
|
if (isset($builders[$type][$name])) { |
202
|
19 |
|
return $builders[$type][$name]; |
203
|
|
|
} |
204
|
|
|
|
205
|
8 |
|
$builderClassMap = self::$builderClassMap[$type]; |
206
|
|
|
|
207
|
8 |
|
if (isset($builderClassMap[$name])) { |
208
|
5 |
|
return $builders[$type][$name] = new $builderClassMap[$name](); |
209
|
|
|
} |
210
|
|
|
// deprecated relay builder name ? |
211
|
3 |
|
$newName = 'Relay::'.\rtrim($name, 'Args'); |
212
|
3 |
|
if (isset($builderClassMap[$newName])) { |
213
|
1 |
|
@\trigger_error( |
214
|
1 |
|
\sprintf('The "%s" %s builder is deprecated as of 0.7 and will be removed in 0.12. Use "%s" instead.', $name, $type, $newName), |
215
|
1 |
|
\E_USER_DEPRECATED |
216
|
|
|
); |
217
|
|
|
|
218
|
1 |
|
return $builders[$type][$newName] = new $builderClassMap[$newName](); |
219
|
|
|
} |
220
|
|
|
|
221
|
2 |
|
throw new InvalidConfigurationException(\sprintf('%s builder "%s" not found.', \ucfirst($type), $name)); |
222
|
|
|
} |
223
|
|
|
|
224
|
15 |
|
private static function processFieldArgumentsBuilders(array $field) |
225
|
|
|
{ |
226
|
15 |
|
$argsBuilderName = null; |
227
|
|
|
|
228
|
15 |
|
if (\is_string($field['argsBuilder'])) { |
229
|
15 |
|
$argsBuilderName = $field['argsBuilder']; |
230
|
1 |
|
} elseif (isset($field['argsBuilder']['builder']) && \is_string($field['argsBuilder']['builder'])) { |
231
|
1 |
|
$argsBuilderName = $field['argsBuilder']['builder']; |
232
|
|
|
} |
233
|
|
|
|
234
|
15 |
|
$builderConfig = []; |
235
|
15 |
|
if (isset($field['argsBuilder']['config']) && \is_array($field['argsBuilder']['config'])) { |
236
|
1 |
|
$builderConfig = $field['argsBuilder']['config']; |
237
|
|
|
} |
238
|
|
|
|
239
|
15 |
|
if ($argsBuilderName) { |
240
|
15 |
|
$args = self::getBuilder($argsBuilderName, self::BUILDER_ARGS_TYPE)->toMappingDefinition($builderConfig); |
241
|
14 |
|
$field['args'] = isset($field['args']) && \is_array($field['args']) ? \array_merge($args, $field['args']) : $args; |
242
|
|
|
} |
243
|
|
|
|
244
|
14 |
|
unset($field['argsBuilder']); |
245
|
|
|
|
246
|
14 |
|
return $field; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|