|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Config; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\ExpressionLanguage\Expression; |
|
6
|
|
|
|
|
7
|
|
|
class Processor |
|
8
|
|
|
{ |
|
9
|
|
|
const DEFAULT_EXPRESSION_LANGUAGE_TRIGGER = '@='; |
|
10
|
|
|
|
|
11
|
|
|
/** @var string */ |
|
12
|
|
|
private $expressionLanguageTrigger; |
|
13
|
|
|
|
|
14
|
|
|
/** @var callable[] */ |
|
15
|
|
|
private $processors = []; |
|
16
|
|
|
|
|
17
|
26 |
|
public function __construct($expressionLanguageTrigger = self::DEFAULT_EXPRESSION_LANGUAGE_TRIGGER, array $processors = []) |
|
18
|
|
|
{ |
|
19
|
|
|
// add the default $processors to let users override it easily |
|
20
|
26 |
|
array_push($processors, [$this, 'convertStringToExpressionObject']); |
|
21
|
|
|
|
|
22
|
26 |
|
$this->processors = $processors; |
|
23
|
26 |
|
$this->expressionLanguageTrigger = $expressionLanguageTrigger; |
|
24
|
26 |
|
} |
|
25
|
|
|
|
|
26
|
26 |
|
public function process(array $configs) |
|
27
|
|
|
{ |
|
28
|
26 |
|
foreach ($this->processors as $processor) { |
|
29
|
26 |
|
$configs = call_user_func($processor, $configs); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
26 |
|
return $configs; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
26 |
|
public static function staticProcess( |
|
36
|
|
|
array $configs, |
|
37
|
|
|
$expressionLanguageTrigger = self::DEFAULT_EXPRESSION_LANGUAGE_TRIGGER, |
|
38
|
|
|
array $processors = [] |
|
39
|
|
|
) { |
|
40
|
26 |
|
return (new static($expressionLanguageTrigger, $processors))->process($configs); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function convertStringToExpressionObject(array $configs) |
|
44
|
|
|
{ |
|
45
|
26 |
|
return array_map(function ($v) { |
|
46
|
26 |
|
if (is_array($v)) { |
|
47
|
26 |
|
return $this->convertStringToExpressionObject($v); |
|
48
|
26 |
|
} elseif (is_string($v) && 0 === strpos($v, $this->expressionLanguageTrigger)) { |
|
49
|
19 |
|
return new Expression(substr($v, 2)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
26 |
|
return $v; |
|
53
|
26 |
|
}, $configs); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
30 |
|
public static function processBeforeNormalization(array $configs) |
|
57
|
|
|
{ |
|
58
|
30 |
|
$configs = static::processConfigsExtends($configs); |
|
59
|
30 |
|
$configs = static::removedVirtualTypes($configs); |
|
60
|
|
|
|
|
61
|
30 |
|
return $configs; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public static function removedVirtualTypes(array $configs) |
|
65
|
|
|
{ |
|
66
|
30 |
|
return array_filter($configs, function ($config) { |
|
67
|
30 |
|
return !isset($config['virtual']) || true !== $config['virtual']; |
|
68
|
30 |
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
33 |
|
public static function processConfigsExtends(array $configs) |
|
72
|
|
|
{ |
|
73
|
33 |
|
foreach ($configs as $name => &$config) { |
|
74
|
33 |
|
if (!isset($config['type'])) { |
|
75
|
5 |
|
continue; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
28 |
|
$allowedTypes = [$config['type']]; |
|
79
|
28 |
|
if ('object' === $config['type']) { |
|
80
|
28 |
|
$allowedTypes[] = 'interface'; |
|
81
|
|
|
} |
|
82
|
28 |
|
$flattenExtends = self::flattenExtends($name, $configs, $allowedTypes); |
|
83
|
25 |
|
if (empty($flattenExtends)) { |
|
84
|
25 |
|
continue; |
|
85
|
|
|
} |
|
86
|
1 |
|
$config = self::extendsTypeConfig($name, $flattenExtends, $configs); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
30 |
|
return $configs; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
private static function extendsTypeConfig($child, array $parents, array $configs) |
|
93
|
|
|
{ |
|
94
|
1 |
|
$parentTypes = array_intersect_key($configs, array_flip($parents)); |
|
95
|
1 |
|
$parentTypes = array_reverse($parentTypes); |
|
96
|
1 |
|
$mergedParentsConfig = call_user_func_array('array_replace_recursive', array_column($parentTypes, 'config')); |
|
97
|
1 |
|
$childType = $configs[$child]; |
|
98
|
|
|
// unset resolveType field resulting from the merge of a "interface" type |
|
99
|
1 |
|
if ('object' === $childType['type']) { |
|
100
|
1 |
|
unset($mergedParentsConfig['resolveType']); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
$configs = array_replace_recursive(['config' => $mergedParentsConfig], $childType); |
|
104
|
|
|
|
|
105
|
1 |
|
return $configs; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
28 |
|
private static function flattenExtends($name, array $configs, array $allowedTypes, $child = null, array $typesTreated = []) |
|
109
|
|
|
{ |
|
110
|
28 |
|
self::checkTypeExists($name, $configs, $child); |
|
111
|
28 |
|
self::checkCircularReferenceExtendsTypes($name, $typesTreated); |
|
112
|
28 |
|
self::checkAllowedExtendsTypes($name, $configs[$name], $allowedTypes, $child); |
|
113
|
|
|
|
|
114
|
|
|
// flatten |
|
115
|
28 |
|
$config = $configs[$name]; |
|
116
|
28 |
|
if (empty($config['extends']) || !is_array($config['extends'])) { |
|
117
|
25 |
|
return []; |
|
118
|
|
|
} |
|
119
|
4 |
|
$typesTreated[$name] = true; |
|
120
|
4 |
|
$flattenExtendsTypes = []; |
|
121
|
4 |
|
foreach ($config['extends'] as $typeToExtend) { |
|
122
|
4 |
|
$flattenExtendsTypes[] = $typeToExtend; |
|
123
|
4 |
|
$flattenExtendsTypes = array_merge( |
|
124
|
4 |
|
$flattenExtendsTypes, |
|
125
|
4 |
|
self::flattenExtends($typeToExtend, $configs, $allowedTypes, $name, $typesTreated) |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
1 |
|
return $flattenExtendsTypes; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
28 |
|
private static function checkTypeExists($name, array $configs, $child) |
|
133
|
|
|
{ |
|
134
|
28 |
|
if (!isset($configs[$name])) { |
|
135
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
|
136
|
1 |
|
'Type %s extends by %s not be found.', |
|
137
|
1 |
|
json_encode($name), |
|
138
|
1 |
|
json_encode($child) |
|
139
|
|
|
)); |
|
140
|
|
|
} |
|
141
|
28 |
|
} |
|
142
|
|
|
|
|
143
|
28 |
|
private static function checkCircularReferenceExtendsTypes($name, array $typesTreated) |
|
144
|
|
|
{ |
|
145
|
28 |
|
if (isset($typesTreated[$name])) { |
|
146
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
|
147
|
1 |
|
'Type circular inheritance detected (%s).', |
|
148
|
1 |
|
implode('->', array_merge(array_keys($typesTreated), [$name])) |
|
149
|
|
|
)); |
|
150
|
|
|
} |
|
151
|
28 |
|
} |
|
152
|
|
|
|
|
153
|
28 |
|
private static function checkAllowedExtendsTypes($name, array $config, array $allowedTypes, $child) |
|
154
|
|
|
{ |
|
155
|
28 |
|
if (!in_array($config['type'], $allowedTypes)) { |
|
156
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
|
157
|
1 |
|
'Type %s can\'t extends %s because %s is not allowed type (%s).', |
|
158
|
1 |
|
json_encode($name), |
|
159
|
1 |
|
json_encode($child), |
|
160
|
1 |
|
json_encode($config['type']), |
|
161
|
1 |
|
json_encode($allowedTypes) |
|
162
|
|
|
)); |
|
163
|
|
|
} |
|
164
|
28 |
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|