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 |
|
foreach ($configs as &$config) { |
51
|
39 |
|
if (isset($config['config']['builders']) && \is_array($config['config']['builders'])) { |
52
|
1 |
|
$buildersFields = self::processFieldsBuilders($config['config']['builders']); |
53
|
1 |
|
$config['config']['fields'] = isset($config['config']['fields']) ? \array_merge($buildersFields, $config['config']['fields']) : $buildersFields; |
54
|
1 |
|
unset($config['config']['builders']); |
55
|
|
|
} |
56
|
|
|
|
57
|
39 |
|
if (isset($config['config']['fields']) && \is_array($config['config']['fields'])) { |
58
|
39 |
|
$config['config']['fields'] = self::processFieldBuilders($config['config']['fields']); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
37 |
|
return $configs; |
63
|
|
|
} |
64
|
|
|
|
65
|
11 |
|
public static function addBuilderClass($name, $type, $builderClass): void |
66
|
|
|
{ |
67
|
11 |
|
self::checkBuilderClass($builderClass, $type); |
68
|
1 |
|
self::$builderClassMap[$type][$name] = $builderClass; |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $builderClass |
73
|
|
|
* @param string $type |
74
|
|
|
*/ |
75
|
11 |
|
private static function checkBuilderClass($builderClass, $type): void |
76
|
|
|
{ |
77
|
11 |
|
$interface = MappingInterface::class; |
78
|
|
|
|
79
|
11 |
|
if (!\is_string($builderClass)) { |
|
|
|
|
80
|
5 |
|
throw new \InvalidArgumentException( |
81
|
5 |
|
\sprintf('%s builder class should be string, but %s given.', \ucfirst($type), \gettype($builderClass)) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
6 |
|
if (!\class_exists($builderClass)) { |
86
|
2 |
|
throw new \InvalidArgumentException( |
87
|
2 |
|
\sprintf('%s builder class "%s" not found.', \ucfirst($type), $builderClass) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
4 |
|
if (!\is_subclass_of($builderClass, $interface)) { |
92
|
3 |
|
throw new \InvalidArgumentException( |
93
|
3 |
|
\sprintf( |
94
|
3 |
|
'%s builder class should implement "%s", but "%s" given.', |
95
|
3 |
|
\ucfirst($type), |
96
|
3 |
|
$interface, |
97
|
3 |
|
$builderClass |
98
|
|
|
) |
99
|
|
|
); |
100
|
|
|
} |
101
|
1 |
|
} |
102
|
|
|
|
103
|
34 |
|
private static function processFieldBuilders(array $fields) |
104
|
|
|
{ |
105
|
34 |
|
foreach ($fields as &$field) { |
106
|
34 |
|
$fieldBuilderName = null; |
107
|
|
|
|
108
|
34 |
|
if (isset($field['builder']) && \is_string($field['builder'])) { |
109
|
9 |
|
$fieldBuilderName = $field['builder']; |
110
|
9 |
|
unset($field['builder']); |
111
|
33 |
|
} elseif (\is_string($field)) { |
112
|
2 |
|
@\trigger_error( |
113
|
|
|
'The builder short syntax (Field: Builder => Field: {builder: Builder}) is deprecated as of 0.7 and will be removed in 0.12. '. |
114
|
2 |
|
'It will be replaced by the field type short syntax (Field: Type => Field: {type: Type})', |
115
|
2 |
|
\E_USER_DEPRECATED |
116
|
|
|
); |
117
|
2 |
|
$fieldBuilderName = $field; |
118
|
|
|
} |
119
|
|
|
|
120
|
34 |
|
$builderConfig = []; |
121
|
34 |
|
if (isset($field['builderConfig'])) { |
122
|
7 |
|
if (\is_array($field['builderConfig'])) { |
123
|
7 |
|
$builderConfig = $field['builderConfig']; |
124
|
|
|
} |
125
|
7 |
|
unset($field['builderConfig']); |
126
|
|
|
} |
127
|
|
|
|
128
|
34 |
|
if ($fieldBuilderName) { |
129
|
9 |
|
$buildField = self::getBuilder($fieldBuilderName, self::BUILDER_FIELD_TYPE)->toMappingDefinition($builderConfig); |
130
|
8 |
|
$field = \is_array($field) ? \array_merge($buildField, $field) : $buildField; |
131
|
|
|
} |
132
|
33 |
|
if (isset($field['argsBuilder'])) { |
133
|
33 |
|
$field = self::processFieldArgumentsBuilders($field); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
32 |
|
return $fields; |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
private static function processFieldsBuilders(array $builders) |
141
|
|
|
{ |
142
|
1 |
|
$fields = []; |
143
|
1 |
|
foreach ($builders as $builder) { |
144
|
1 |
|
$builderName = $builder['builder']; |
145
|
1 |
|
$builderConfig = null; |
146
|
1 |
|
if (isset($builder['builderConfig'])) { |
147
|
1 |
|
$builderConfig = $builder['builderConfig']; |
148
|
|
|
} |
149
|
1 |
|
$fields = \array_merge($fields, self::getBuilder($builderName, self::BUILDER_FIELDS_TYPE)->toMappingDefinition($builderConfig)); |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
return $fields; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $name |
157
|
|
|
* @param string $type |
158
|
|
|
* |
159
|
|
|
* @return MappingInterface |
160
|
|
|
* |
161
|
|
|
* @throws InvalidConfigurationException if builder class not define |
162
|
|
|
*/ |
163
|
22 |
|
private static function getBuilder($name, $type) |
164
|
|
|
{ |
165
|
22 |
|
static $builders = []; |
166
|
22 |
|
if (isset($builders[$type][$name])) { |
167
|
19 |
|
return $builders[$type][$name]; |
168
|
|
|
} |
169
|
|
|
|
170
|
8 |
|
$builderClassMap = self::$builderClassMap[$type]; |
171
|
|
|
|
172
|
8 |
|
if (isset($builderClassMap[$name])) { |
173
|
5 |
|
return $builders[$type][$name] = new $builderClassMap[$name](); |
174
|
|
|
} |
175
|
|
|
// deprecated relay builder name ? |
176
|
3 |
|
$newName = 'Relay::'.\rtrim($name, 'Args'); |
177
|
3 |
|
if (isset($builderClassMap[$newName])) { |
178
|
1 |
|
@\trigger_error( |
179
|
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), |
180
|
1 |
|
\E_USER_DEPRECATED |
181
|
|
|
); |
182
|
|
|
|
183
|
1 |
|
return $builders[$type][$newName] = new $builderClassMap[$newName](); |
184
|
|
|
} |
185
|
|
|
|
186
|
2 |
|
throw new InvalidConfigurationException(\sprintf('%s builder "%s" not found.', \ucfirst($type), $name)); |
187
|
|
|
} |
188
|
|
|
|
189
|
15 |
|
private static function processFieldArgumentsBuilders(array $field) |
190
|
|
|
{ |
191
|
15 |
|
$argsBuilderName = null; |
192
|
|
|
|
193
|
15 |
|
if (\is_string($field['argsBuilder'])) { |
194
|
15 |
|
$argsBuilderName = $field['argsBuilder']; |
195
|
1 |
|
} elseif (isset($field['argsBuilder']['builder']) && \is_string($field['argsBuilder']['builder'])) { |
196
|
1 |
|
$argsBuilderName = $field['argsBuilder']['builder']; |
197
|
|
|
} |
198
|
|
|
|
199
|
15 |
|
$builderConfig = []; |
200
|
15 |
|
if (isset($field['argsBuilder']['config']) && \is_array($field['argsBuilder']['config'])) { |
201
|
1 |
|
$builderConfig = $field['argsBuilder']['config']; |
202
|
|
|
} |
203
|
|
|
|
204
|
15 |
|
if ($argsBuilderName) { |
205
|
15 |
|
$args = self::getBuilder($argsBuilderName, self::BUILDER_ARGS_TYPE)->toMappingDefinition($builderConfig); |
206
|
14 |
|
$field['args'] = isset($field['args']) && \is_array($field['args']) ? \array_merge($args, $field['args']) : $args; |
207
|
|
|
} |
208
|
|
|
|
209
|
14 |
|
unset($field['argsBuilder']); |
210
|
|
|
|
211
|
14 |
|
return $field; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|