1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Config\Processor; |
4
|
|
|
|
5
|
|
|
final class InheritanceProcessor implements ProcessorInterface |
6
|
|
|
{ |
7
|
|
|
const HEIRS_KEY = 'heirs'; |
8
|
|
|
const INHERITS_KEY = 'inherits'; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* {@inheritdoc} |
12
|
|
|
*/ |
13
|
33 |
|
public static function process(array $configs) |
14
|
|
|
{ |
15
|
33 |
|
$configs = self::processConfigsHeirs($configs); |
16
|
32 |
|
$configs = self::processConfigsInherits($configs); |
17
|
29 |
|
$configs = self::removedDecorators($configs); |
18
|
|
|
|
19
|
29 |
|
return $configs; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
private static function removedDecorators(array $configs) |
23
|
|
|
{ |
24
|
29 |
|
return array_filter($configs, function ($config) { |
25
|
29 |
|
return !isset($config['decorator']) || true !== $config['decorator']; |
26
|
29 |
|
}); |
27
|
|
|
} |
28
|
|
|
|
29
|
33 |
|
private static function processConfigsHeirs(array $configs) |
30
|
|
|
{ |
31
|
33 |
|
foreach ($configs as $parentName => &$config) { |
32
|
33 |
|
if (!empty($config[self::HEIRS_KEY])) { |
33
|
|
|
// allows shorthand configuration `heirs: QueryFooDecorator` is equivalent to `heirs: [QueryFooDecorator]` |
34
|
5 |
View Code Duplication |
if (!is_array($config[self::HEIRS_KEY])) { |
|
|
|
|
35
|
1 |
|
$config[self::HEIRS_KEY] = [$config[self::HEIRS_KEY]]; |
36
|
|
|
} |
37
|
5 |
|
foreach ($config[self::HEIRS_KEY] as $heirs) { |
38
|
5 |
|
if (!isset($configs[$heirs])) { |
39
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
40
|
1 |
|
'Type %s child of %s not found.', |
41
|
1 |
|
json_encode($heirs), |
42
|
1 |
|
json_encode($parentName) |
43
|
|
|
)); |
44
|
|
|
} |
45
|
|
|
|
46
|
4 |
|
if (!isset($configs[$heirs][self::INHERITS_KEY])) { |
47
|
1 |
|
$configs[$heirs][self::INHERITS_KEY] = []; |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
$configs[$heirs][self::INHERITS_KEY][] = $parentName; |
51
|
|
|
} |
52
|
|
|
} |
53
|
33 |
|
unset($config[self::HEIRS_KEY]); |
54
|
|
|
} |
55
|
|
|
|
56
|
32 |
|
return $configs; |
57
|
|
|
} |
58
|
|
|
|
59
|
32 |
|
private static function processConfigsInherits(array $configs) |
60
|
|
|
{ |
61
|
32 |
|
foreach ($configs as $name => &$config) { |
62
|
32 |
|
if (!isset($config['type'])) { |
63
|
6 |
|
continue; |
64
|
|
|
} |
65
|
|
|
|
66
|
27 |
|
$allowedTypes = [$config['type']]; |
67
|
27 |
|
if ('object' === $config['type']) { |
68
|
27 |
|
$allowedTypes[] = 'interface'; |
69
|
|
|
} |
70
|
27 |
|
$flattenInherits = self::flattenInherits($name, $configs, $allowedTypes); |
71
|
24 |
|
if (empty($flattenInherits)) { |
72
|
24 |
|
continue; |
73
|
|
|
} |
74
|
1 |
|
$config = self::inheritsTypeConfig($name, $flattenInherits, $configs); |
75
|
|
|
} |
76
|
|
|
|
77
|
29 |
|
return $configs; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
private static function inheritsTypeConfig($child, array $parents, array $configs) |
81
|
|
|
{ |
82
|
1 |
|
$parentTypes = array_intersect_key($configs, array_flip($parents)); |
83
|
1 |
|
$parentTypes = array_reverse($parentTypes); |
84
|
1 |
|
$mergedParentsConfig = call_user_func_array('array_replace_recursive', array_column($parentTypes, 'config')); |
85
|
1 |
|
$childType = $configs[$child]; |
86
|
|
|
// unset resolveType field resulting from the merge of a "interface" type |
87
|
1 |
|
if ('object' === $childType['type']) { |
88
|
1 |
|
unset($mergedParentsConfig['resolveType']); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
$configs = array_replace_recursive(['config' => $mergedParentsConfig], $childType); |
92
|
|
|
|
93
|
1 |
|
return $configs; |
94
|
|
|
} |
95
|
|
|
|
96
|
27 |
|
private static function flattenInherits($name, array $configs, array $allowedTypes, $child = null, array $typesTreated = []) |
97
|
|
|
{ |
98
|
27 |
|
self::checkTypeExists($name, $configs, $child); |
99
|
27 |
|
self::checkCircularReferenceInheritsTypes($name, $typesTreated); |
100
|
27 |
|
self::checkAllowedInheritsTypes($name, $configs[$name], $allowedTypes, $child); |
101
|
|
|
|
102
|
|
|
// flatten |
103
|
27 |
|
$config = $configs[$name]; |
104
|
27 |
View Code Duplication |
if (empty($config[self::INHERITS_KEY]) || !is_array($config[self::INHERITS_KEY])) { |
|
|
|
|
105
|
24 |
|
return []; |
106
|
|
|
} |
107
|
4 |
|
$typesTreated[$name] = true; |
108
|
4 |
|
$flattenInheritsTypes = []; |
109
|
4 |
|
foreach ($config[self::INHERITS_KEY] as $typeToInherit) { |
110
|
4 |
|
$flattenInheritsTypes[] = $typeToInherit; |
111
|
4 |
|
$flattenInheritsTypes = array_merge( |
112
|
4 |
|
$flattenInheritsTypes, |
113
|
4 |
|
self::flattenInherits($typeToInherit, $configs, $allowedTypes, $name, $typesTreated) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
return $flattenInheritsTypes; |
118
|
|
|
} |
119
|
|
|
|
120
|
27 |
|
private static function checkTypeExists($name, array $configs, $child) |
121
|
|
|
{ |
122
|
27 |
|
if (!isset($configs[$name])) { |
123
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
124
|
1 |
|
'Type %s inherits by %s not found.', |
125
|
1 |
|
json_encode($name), |
126
|
1 |
|
json_encode($child) |
127
|
|
|
)); |
128
|
|
|
} |
129
|
27 |
|
} |
130
|
|
|
|
131
|
27 |
|
private static function checkCircularReferenceInheritsTypes($name, array $typesTreated) |
132
|
|
|
{ |
133
|
27 |
|
if (isset($typesTreated[$name])) { |
134
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
135
|
1 |
|
'Type circular inheritance detected (%s).', |
136
|
1 |
|
implode('->', array_merge(array_keys($typesTreated), [$name])) |
137
|
|
|
)); |
138
|
|
|
} |
139
|
27 |
|
} |
140
|
|
|
|
141
|
27 |
|
private static function checkAllowedInheritsTypes($name, array $config, array $allowedTypes, $child) |
142
|
|
|
{ |
143
|
27 |
|
if (empty($config['decorator']) && isset($config['type']) && !in_array($config['type'], $allowedTypes)) { |
144
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
145
|
1 |
|
'Type %s can\'t inherits %s because %s is not allowed type (%s).', |
146
|
1 |
|
json_encode($name), |
147
|
1 |
|
json_encode($child), |
148
|
1 |
|
json_encode($config['type']), |
149
|
1 |
|
json_encode($allowedTypes) |
150
|
|
|
)); |
151
|
|
|
} |
152
|
27 |
|
} |
153
|
|
|
} |
154
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.