1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Config\Processor; |
4
|
|
|
|
5
|
|
|
use Overblog\GraphQLBundle\Definition\Builder\MappingInterface; |
6
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\BackwardConnectionArgsDefinition; |
7
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\ConnectionArgsDefinition; |
8
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\ForwardConnectionArgsDefinition; |
9
|
|
|
use Overblog\GraphQLBundle\Relay\Mutation\MutationFieldDefinition; |
10
|
|
|
use Overblog\GraphQLBundle\Relay\Node\GlobalIdFieldDefinition; |
11
|
|
|
use Overblog\GraphQLBundle\Relay\Node\NodeFieldDefinition; |
12
|
|
|
use Overblog\GraphQLBundle\Relay\Node\PluralIdentifyingRootFieldDefinition; |
13
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
14
|
|
|
|
15
|
|
|
final class BuilderProcessor implements ProcessorInterface |
16
|
|
|
{ |
17
|
|
|
const BUILDER_FIELD_TYPE = 'field'; |
18
|
|
|
const BUILDER_ARGS_TYPE = 'args'; |
19
|
|
|
|
20
|
|
|
/** @var MappingInterface[] */ |
21
|
|
|
private static $argsBuilderClassMap = [ |
22
|
|
|
'Relay::ForwardConnection' => ForwardConnectionArgsDefinition::class, |
23
|
|
|
'Relay::BackwardConnection' => BackwardConnectionArgsDefinition::class, |
24
|
|
|
'Relay::Connection' => ConnectionArgsDefinition::class, |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** @var MappingInterface[] */ |
28
|
|
|
private static $fieldBuilderClassMap = [ |
29
|
|
|
'Relay::Mutation' => MutationFieldDefinition::class, |
30
|
|
|
'Relay::GlobalId' => GlobalIdFieldDefinition::class, |
31
|
|
|
'Relay::Node' => NodeFieldDefinition::class, |
32
|
|
|
'Relay::PluralIdentifyingRoot' => PluralIdentifyingRootFieldDefinition::class, |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
29 |
|
public static function process(array $configs) |
39
|
|
|
{ |
40
|
29 |
|
foreach ($configs as &$config) { |
41
|
29 |
|
if (isset($config['config']['fields']) && is_array($config['config']['fields'])) { |
42
|
29 |
|
$config['config']['fields'] = self::processFieldBuilders($config['config']['fields']); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
29 |
|
return $configs; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
public static function addArgsBuilderClass($name, $argBuilderClass) |
50
|
|
|
{ |
51
|
1 |
|
self::checkBuilderClass($argBuilderClass, 'args'); |
52
|
1 |
|
self::$argsBuilderClassMap[$name] = $argBuilderClass; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
1 |
|
public static function addFieldBuilderClass($name, $fieldBuilderClass) |
56
|
|
|
{ |
57
|
1 |
|
self::checkBuilderClass($fieldBuilderClass, 'field'); |
58
|
1 |
|
self::$fieldBuilderClassMap[$name] = $fieldBuilderClass; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $builderClass |
63
|
|
|
* @param string $type |
64
|
|
|
*/ |
65
|
1 |
|
private static function checkBuilderClass($builderClass, $type) |
66
|
|
|
{ |
67
|
1 |
|
$interface = MappingInterface::class; |
68
|
|
|
|
69
|
1 |
|
if (!is_string($builderClass)) { |
70
|
|
|
throw new \InvalidArgumentException( |
71
|
|
|
sprintf('%s builder class should be string, but "%s" given.', ucfirst($type), gettype($builderClass)) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
if (!class_exists($builderClass)) { |
76
|
|
|
throw new \InvalidArgumentException( |
77
|
|
|
sprintf('%s builder class "%s" not found.', ucfirst($type), $builderClass) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
if (!is_subclass_of($builderClass, $interface)) { |
|
|
|
|
82
|
|
|
throw new \InvalidArgumentException( |
83
|
|
|
sprintf( |
84
|
|
|
'%s builder class should be instance of "%s", but "%s" given.', |
85
|
|
|
ucfirst($type), |
86
|
|
|
$interface, |
87
|
|
|
$builderClass |
88
|
|
|
) |
89
|
|
|
); |
90
|
|
|
} |
91
|
1 |
|
} |
92
|
|
|
|
93
|
24 |
|
private static function processFieldBuilders(array $fields) |
94
|
|
|
{ |
95
|
24 |
|
foreach ($fields as &$field) { |
96
|
24 |
|
$fieldBuilderName = null; |
97
|
|
|
|
98
|
24 |
|
if (isset($field['builder']) && is_string($field['builder'])) { |
99
|
7 |
|
$fieldBuilderName = $field['builder']; |
100
|
7 |
|
unset($field['builder']); |
101
|
24 |
|
} elseif (is_string($field)) { |
102
|
2 |
|
@trigger_error( |
|
|
|
|
103
|
|
|
'The builder short syntax (Field: Builder => Field: {builder: Builder}) is deprecated as of 0.7 and will be removed in 0.9. '. |
104
|
2 |
|
'It will be replaced by the field type short syntax (Field: Type => Field: {type: Type})', |
105
|
2 |
|
E_USER_DEPRECATED |
106
|
|
|
); |
107
|
2 |
|
$fieldBuilderName = $field; |
108
|
|
|
} |
109
|
|
|
|
110
|
24 |
|
$builderConfig = []; |
111
|
24 |
|
if (isset($field['builderConfig'])) { |
112
|
6 |
|
if (is_array($field['builderConfig'])) { |
113
|
6 |
|
$builderConfig = $field['builderConfig']; |
114
|
|
|
} |
115
|
6 |
|
unset($field['builderConfig']); |
116
|
|
|
} |
117
|
|
|
|
118
|
24 |
|
if ($fieldBuilderName) { |
119
|
7 |
|
$buildField = self::getBuilder($fieldBuilderName, self::BUILDER_FIELD_TYPE)->toMappingDefinition($builderConfig); |
120
|
7 |
|
$field = is_array($field) ? array_merge($buildField, $field) : $buildField; |
121
|
|
|
} |
122
|
24 |
|
if (isset($field['argsBuilder'])) { |
123
|
24 |
|
$field = self::processFieldArgumentsBuilders($field); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
24 |
|
return $fields; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $name |
132
|
|
|
* @param string $type |
133
|
|
|
* |
134
|
|
|
* @return MappingInterface |
135
|
|
|
* |
136
|
|
|
* @throws InvalidConfigurationException if builder class not define |
137
|
|
|
*/ |
138
|
16 |
|
private static function getBuilder($name, $type) |
139
|
|
|
{ |
140
|
16 |
|
static $builders = []; |
141
|
16 |
|
if (isset($builders[$type][$name])) { |
142
|
13 |
|
return $builders[$type][$name]; |
143
|
|
|
} |
144
|
|
|
|
145
|
6 |
|
$builderClassMap = self::${$type.'BuilderClassMap'}; |
146
|
|
|
|
147
|
6 |
|
if (isset($builderClassMap[$name])) { |
148
|
5 |
|
return $builders[$type][$name] = new $builderClassMap[$name](); |
149
|
|
|
} |
150
|
|
|
// deprecated relay builder name ? |
151
|
1 |
|
$newName = 'Relay::'.rtrim($name, 'Args'); |
152
|
1 |
|
if (isset($builderClassMap[$newName])) { |
153
|
1 |
|
@trigger_error( |
|
|
|
|
154
|
1 |
|
sprintf('The "%s" %s builder is deprecated as of 0.7 and will be removed in 1.0. Use "%s" instead.', $name, $type, $newName), |
155
|
1 |
|
E_USER_DEPRECATED |
156
|
|
|
); |
157
|
|
|
|
158
|
1 |
|
return $builders[$type][$newName] = new $builderClassMap[$newName](); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
throw new InvalidConfigurationException(sprintf('%s builder "%s" not found.', ucfirst($type), $name)); |
162
|
|
|
} |
163
|
|
|
|
164
|
11 |
|
private static function processFieldArgumentsBuilders(array $field) |
165
|
|
|
{ |
166
|
11 |
|
$argsBuilderName = null; |
167
|
|
|
|
168
|
11 |
|
if (is_string($field['argsBuilder'])) { |
169
|
11 |
|
$argsBuilderName = $field['argsBuilder']; |
170
|
1 |
|
} elseif (isset($field['argsBuilder']['builder']) && is_string($field['argsBuilder']['builder'])) { |
171
|
1 |
|
$argsBuilderName = $field['argsBuilder']['builder']; |
172
|
|
|
} |
173
|
|
|
|
174
|
11 |
|
$builderConfig = []; |
175
|
11 |
|
if (isset($field['argsBuilder']['config']) && is_array($field['argsBuilder']['config'])) { |
176
|
1 |
|
$builderConfig = $field['argsBuilder']['config']; |
177
|
|
|
} |
178
|
|
|
|
179
|
11 |
|
if ($argsBuilderName) { |
180
|
11 |
|
$args = self::getBuilder($argsBuilderName, self::BUILDER_ARGS_TYPE)->toMappingDefinition($builderConfig); |
181
|
11 |
|
$field['args'] = isset($field['args']) && is_array($field['args']) ? array_merge($args, $field['args']) : $args; |
182
|
|
|
} |
183
|
|
|
|
184
|
11 |
|
unset($field['argsBuilder']); |
185
|
|
|
|
186
|
11 |
|
return $field; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|