1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use GraphQL\Validator\Rules\QueryComplexity; |
15
|
|
|
use GraphQL\Validator\Rules\QueryDepth; |
16
|
|
|
use Overblog\GraphQLBundle\Error\ErrorHandler; |
17
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
18
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @todo fix xml |
22
|
|
|
*/ |
23
|
|
|
class Configuration implements ConfigurationInterface |
24
|
|
|
{ |
25
|
|
|
private $debug; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Constructor. |
29
|
|
|
* |
30
|
|
|
* @param bool $debug Whether to use the debug mode |
31
|
|
|
*/ |
32
|
73 |
|
public function __construct($debug) |
33
|
|
|
{ |
34
|
73 |
|
$this->debug = (bool) $debug; |
35
|
73 |
|
} |
36
|
|
|
|
37
|
73 |
|
public function getConfigTreeBuilder() |
38
|
|
|
{ |
39
|
73 |
|
$treeBuilder = new TreeBuilder(); |
40
|
73 |
|
$rootNode = $treeBuilder->root('overblog_graphql'); |
41
|
|
|
|
42
|
|
|
$rootNode |
43
|
73 |
|
->children() |
44
|
73 |
|
->arrayNode('definitions') |
45
|
73 |
|
->addDefaultsIfNotSet() |
46
|
73 |
|
->children() |
47
|
73 |
|
->scalarNode('internal_error_message')->defaultNull()->end() |
48
|
73 |
|
->booleanNode('show_debug_info')->defaultValue(false)->end() |
49
|
73 |
|
->booleanNode('config_validation')->defaultValue($this->debug)->end() |
50
|
73 |
|
->arrayNode('schema') |
51
|
73 |
|
->beforeNormalization() |
52
|
|
|
->ifTrue(function ($v) { |
53
|
61 |
|
$needNormalization = isset($v['query']) && is_string($v['query']) || |
54
|
|
|
isset($v['mutation']) && is_string($v['mutation']) || |
55
|
61 |
|
isset($v['subscription']) && is_string($v['subscription']); |
56
|
|
|
|
57
|
61 |
|
return $needNormalization; |
58
|
73 |
|
}) |
59
|
|
|
->then(function ($v) { |
60
|
61 |
|
return ['default' => $v]; |
61
|
73 |
|
}) |
62
|
73 |
|
->end() |
63
|
73 |
|
->useAttributeAsKey('name') |
64
|
73 |
|
->prototype('array') |
65
|
73 |
|
->addDefaultsIfNotSet() |
66
|
73 |
|
->children() |
67
|
73 |
|
->scalarNode('query')->defaultNull()->end() |
68
|
73 |
|
->scalarNode('mutation')->defaultNull()->end() |
69
|
73 |
|
->scalarNode('subscription')->defaultNull()->end() |
70
|
73 |
|
->end() |
71
|
73 |
|
->end() |
72
|
73 |
|
->end() |
73
|
73 |
|
->arrayNode('auto_mapping') |
74
|
73 |
|
->treatFalseLike(['enabled' => false]) |
75
|
73 |
|
->treatTrueLike(['enabled' => true]) |
76
|
73 |
|
->treatNullLike(['enabled' => true]) |
77
|
73 |
|
->addDefaultsIfNotSet() |
78
|
73 |
|
->children() |
79
|
73 |
|
->booleanNode('enabled')->defaultValue(true)->end() |
80
|
73 |
|
->arrayNode('directories') |
81
|
73 |
|
->info('List of directories containing GraphQL classes.') |
82
|
73 |
|
->prototype('scalar')->end() |
83
|
73 |
|
->end() |
84
|
73 |
|
->end() |
85
|
73 |
|
->end() |
86
|
73 |
|
->arrayNode('mappings') |
87
|
73 |
|
->children() |
88
|
73 |
|
->arrayNode('types') |
89
|
73 |
|
->prototype('array') |
90
|
73 |
|
->addDefaultsIfNotSet() |
91
|
73 |
|
->children() |
92
|
73 |
|
->enumNode('type')->isRequired()->values(['yml', 'xml'])->end() |
93
|
73 |
|
->scalarNode('dir')->defaultNull()->end() |
94
|
73 |
|
->end() |
95
|
73 |
|
->end() |
96
|
73 |
|
->end() |
97
|
73 |
|
->end() |
98
|
73 |
|
->end() |
99
|
73 |
|
->arrayNode('exceptions') |
100
|
73 |
|
->addDefaultsIfNotSet() |
101
|
73 |
|
->children() |
102
|
73 |
|
->arrayNode('warnings') |
103
|
73 |
|
->treatNullLike([]) |
104
|
73 |
|
->prototype('scalar')->end() |
105
|
73 |
|
->end() |
106
|
73 |
|
->arrayNode('errors') |
107
|
73 |
|
->treatNullLike([]) |
108
|
73 |
|
->prototype('scalar')->end() |
109
|
73 |
|
->end() |
110
|
73 |
|
->arrayNode('types') |
111
|
73 |
|
->addDefaultsIfNotSet() |
112
|
73 |
|
->children() |
113
|
73 |
|
->scalarNode('warnings') |
114
|
73 |
|
->defaultValue(ErrorHandler::DEFAULT_USER_WARNING_CLASS) |
115
|
73 |
|
->end() |
116
|
73 |
|
->scalarNode('errors') |
117
|
73 |
|
->defaultValue(ErrorHandler::DEFAULT_USER_ERROR_CLASS) |
118
|
73 |
|
->end() |
119
|
73 |
|
->end() |
120
|
73 |
|
->end() |
121
|
73 |
|
->end() |
122
|
73 |
|
->end() |
123
|
|
|
|
124
|
73 |
|
->arrayNode('builders') |
125
|
73 |
|
->children() |
126
|
73 |
|
->append($this->addBuilderSection('field')) |
127
|
73 |
|
->append($this->addBuilderSection('args')) |
128
|
73 |
|
->end() |
129
|
73 |
|
->end() |
130
|
|
|
|
131
|
73 |
|
->end() |
132
|
73 |
|
->end() |
133
|
73 |
|
->arrayNode('templates') |
134
|
73 |
|
->addDefaultsIfNotSet() |
135
|
73 |
|
->children() |
136
|
73 |
|
->scalarNode('graphiql') |
137
|
73 |
|
->defaultValue('OverblogGraphQLBundle:GraphiQL:index.html.twig') |
138
|
73 |
|
->end() |
139
|
73 |
|
->end() |
140
|
73 |
|
->end() |
141
|
73 |
|
->arrayNode('services') |
142
|
73 |
|
->addDefaultsIfNotSet() |
143
|
73 |
|
->children() |
144
|
73 |
|
->scalarNode('executor') |
145
|
73 |
|
->defaultValue('overblog_graphql.executor.default') |
146
|
73 |
|
->end() |
147
|
73 |
|
->scalarNode('promise_adapter') |
148
|
73 |
|
->defaultValue('overblog_graphql.promise_adapter.default') |
149
|
73 |
|
->end() |
150
|
73 |
|
->scalarNode('expression_language') |
151
|
73 |
|
->defaultValue('overblog_graphql.expression_language.default') |
152
|
73 |
|
->end() |
153
|
73 |
|
->scalarNode('cache_expression_language_parser') |
154
|
73 |
|
->defaultValue('overblog_graphql.cache_expression_language_parser.default') |
155
|
73 |
|
->end() |
156
|
73 |
|
->end() |
157
|
73 |
|
->end() |
158
|
73 |
|
->arrayNode('security') |
159
|
73 |
|
->addDefaultsIfNotSet() |
160
|
73 |
|
->children() |
161
|
73 |
|
->append($this->addSecurityQuerySection('query_max_depth', QueryDepth::DISABLED)) |
162
|
73 |
|
->append($this->addSecurityQuerySection('query_max_complexity', QueryComplexity::DISABLED)) |
163
|
73 |
|
->end() |
164
|
73 |
|
->end() |
165
|
73 |
|
->arrayNode('versions') |
166
|
73 |
|
->addDefaultsIfNotSet() |
167
|
73 |
|
->children() |
168
|
73 |
|
->scalarNode('graphiql')->defaultValue('0.9')->end() |
169
|
73 |
|
->scalarNode('react')->defaultValue('15.4')->end() |
170
|
73 |
|
->scalarNode('fetch')->defaultValue('2.0')->end() |
171
|
73 |
|
->end() |
172
|
73 |
|
->end() |
173
|
73 |
|
->end(); |
174
|
|
|
|
175
|
73 |
|
return $treeBuilder; |
176
|
|
|
} |
177
|
|
|
|
178
|
73 |
|
private function addBuilderSection($name) |
179
|
|
|
{ |
180
|
73 |
|
$builder = new TreeBuilder(); |
181
|
73 |
|
$node = $builder->root($name); |
182
|
73 |
|
$node->beforeNormalization() |
183
|
|
|
->ifTrue(function ($v) { |
184
|
1 |
|
return is_array($v) && !empty($v); |
185
|
73 |
|
}) |
186
|
|
|
->then(function ($v) { |
187
|
1 |
|
foreach ($v as $key => &$config) { |
188
|
1 |
|
if (is_string($config)) { |
189
|
|
|
$config = [ |
190
|
1 |
|
'alias' => $key, |
191
|
1 |
|
'class' => $config, |
192
|
1 |
|
]; |
193
|
1 |
|
} |
194
|
1 |
|
} |
195
|
|
|
|
196
|
1 |
|
return $v; |
197
|
73 |
|
}) |
198
|
73 |
|
->end(); |
199
|
|
|
|
200
|
73 |
|
$node->prototype('array') |
|
|
|
|
201
|
73 |
|
->children() |
202
|
73 |
|
->scalarNode('alias')->isRequired()->end() |
203
|
73 |
|
->scalarNode('class')->isRequired()->end() |
204
|
73 |
|
->end() |
205
|
73 |
|
->end() |
206
|
|
|
; |
207
|
|
|
|
208
|
73 |
|
return $node; |
209
|
|
|
} |
210
|
|
|
|
211
|
73 |
|
private function addSecurityQuerySection($name, $disabledValue) |
212
|
|
|
{ |
213
|
73 |
|
$builder = new TreeBuilder(); |
214
|
73 |
|
$node = $builder->root($name, 'integer'); |
215
|
|
|
|
216
|
|
|
$node |
217
|
73 |
|
->info('Disabled if equal to false.') |
218
|
73 |
|
->beforeNormalization() |
219
|
|
|
->ifTrue(function ($v) { |
220
|
2 |
|
return false === $v; |
221
|
73 |
|
}) |
222
|
|
|
->then(function () use ($disabledValue) { |
223
|
|
|
return $disabledValue; |
224
|
73 |
|
}) |
225
|
73 |
|
->end() |
226
|
73 |
|
->defaultFalse() |
227
|
73 |
|
->validate() |
228
|
73 |
|
->ifTrue(function ($v) { |
229
|
2 |
|
return $v < 0; |
230
|
73 |
|
}) |
231
|
73 |
|
->thenInvalid('"overblog_graphql.security.'.$name.'" must be greater or equal to 0.') |
232
|
73 |
|
->end() |
233
|
|
|
; |
234
|
|
|
|
235
|
73 |
|
return $node; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: