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 Overblog\GraphQLBundle\Resolver\Resolver; |
18
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
19
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
20
|
|
|
|
21
|
|
|
class Configuration implements ConfigurationInterface |
22
|
|
|
{ |
23
|
|
|
private $debug; |
24
|
|
|
|
25
|
|
|
private $cacheDir; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Constructor. |
29
|
|
|
* |
30
|
|
|
* @param bool $debug Whether to use the debug mode |
31
|
|
|
* @param null|string $cacheDir |
32
|
|
|
*/ |
33
|
20 |
|
public function __construct($debug, $cacheDir = null) |
34
|
|
|
{ |
35
|
20 |
|
$this->debug = (bool) $debug; |
36
|
20 |
|
$this->cacheDir = $cacheDir; |
37
|
20 |
|
} |
38
|
|
|
|
39
|
20 |
|
public function getConfigTreeBuilder() |
40
|
|
|
{ |
41
|
20 |
|
$treeBuilder = new TreeBuilder(); |
42
|
20 |
|
$rootNode = $treeBuilder->root('overblog_graphql'); |
43
|
|
|
|
44
|
|
|
$rootNode |
45
|
20 |
|
->children() |
46
|
20 |
|
->enumNode('batching_method') |
47
|
20 |
|
->values(['relay', 'apollo']) |
48
|
20 |
|
->defaultValue('relay') |
49
|
20 |
|
->end() |
50
|
20 |
|
->arrayNode('definitions') |
51
|
20 |
|
->addDefaultsIfNotSet() |
52
|
20 |
|
->children() |
53
|
20 |
|
->scalarNode('internal_error_message')->defaultNull()->end() |
54
|
20 |
|
->variableNode('default_resolver')->defaultValue([Resolver::class, 'defaultResolveFn'])->end() |
55
|
20 |
|
->scalarNode('class_namespace')->defaultValue('Overblog\\GraphQLBundle\\__DEFINITIONS__')->end() |
56
|
20 |
|
->scalarNode('cache_dir')->defaultValue($this->cacheDir.'/overblog/graphql-bundle/__definitions__')->end() |
57
|
20 |
|
->booleanNode('use_classloader_listener')->defaultTrue()->end() |
58
|
20 |
|
->booleanNode('show_debug_info')->defaultFalse()->end() |
59
|
20 |
|
->booleanNode('config_validation')->defaultValue($this->debug)->end() |
60
|
20 |
|
->arrayNode('schema') |
61
|
20 |
|
->beforeNormalization() |
62
|
20 |
|
->ifTrue(function ($v) { |
63
|
17 |
|
$needNormalization = isset($v['query']) && is_string($v['query']) || |
64
|
|
|
isset($v['mutation']) && is_string($v['mutation']) || |
65
|
17 |
|
isset($v['subscription']) && is_string($v['subscription']); |
66
|
|
|
|
67
|
17 |
|
return $needNormalization; |
68
|
20 |
|
}) |
69
|
20 |
|
->then(function ($v) { |
70
|
17 |
|
return ['default' => $v]; |
71
|
20 |
|
}) |
72
|
20 |
|
->end() |
73
|
20 |
|
->useAttributeAsKey('name') |
74
|
20 |
|
->prototype('array') |
75
|
20 |
|
->addDefaultsIfNotSet() |
76
|
20 |
|
->children() |
77
|
20 |
|
->scalarNode('query')->defaultNull()->end() |
78
|
20 |
|
->scalarNode('mutation')->defaultNull()->end() |
79
|
20 |
|
->scalarNode('subscription')->defaultNull()->end() |
80
|
20 |
|
->end() |
81
|
20 |
|
->end() |
82
|
20 |
|
->end() |
83
|
20 |
|
->arrayNode('auto_mapping') |
84
|
20 |
|
->treatFalseLike(['enabled' => false]) |
85
|
20 |
|
->treatTrueLike(['enabled' => true]) |
86
|
20 |
|
->treatNullLike(['enabled' => true]) |
87
|
20 |
|
->addDefaultsIfNotSet() |
88
|
20 |
|
->children() |
89
|
20 |
|
->booleanNode('enabled')->defaultTrue()->end() |
90
|
20 |
|
->arrayNode('directories') |
91
|
20 |
|
->info('List of directories containing GraphQL classes.') |
92
|
20 |
|
->prototype('scalar')->end() |
93
|
20 |
|
->end() |
94
|
20 |
|
->end() |
95
|
20 |
|
->end() |
96
|
20 |
|
->arrayNode('mappings') |
97
|
20 |
|
->children() |
98
|
20 |
|
->arrayNode('types') |
99
|
20 |
|
->prototype('array') |
100
|
20 |
|
->addDefaultsIfNotSet() |
101
|
20 |
|
->beforeNormalization() |
102
|
20 |
|
->ifTrue(function ($v) { |
103
|
16 |
|
return isset($v['type']) && $v['type'] === 'yml'; |
104
|
20 |
|
}) |
105
|
20 |
|
->then(function ($v) { |
106
|
2 |
|
$v['type'] = 'yaml'; |
107
|
|
|
|
108
|
2 |
|
return $v; |
109
|
20 |
|
}) |
110
|
20 |
|
->end() |
111
|
20 |
|
->children() |
112
|
20 |
|
->enumNode('type')->isRequired()->values(['yaml', 'xml'])->end() |
113
|
20 |
|
->scalarNode('dir')->defaultNull()->end() |
114
|
20 |
|
->end() |
115
|
20 |
|
->end() |
116
|
20 |
|
->end() |
117
|
20 |
|
->end() |
118
|
20 |
|
->end() |
119
|
20 |
|
->arrayNode('exceptions') |
120
|
20 |
|
->addDefaultsIfNotSet() |
121
|
20 |
|
->children() |
122
|
20 |
|
->arrayNode('warnings') |
123
|
20 |
|
->treatNullLike([]) |
124
|
20 |
|
->prototype('scalar')->end() |
125
|
20 |
|
->end() |
126
|
20 |
|
->arrayNode('errors') |
127
|
20 |
|
->treatNullLike([]) |
128
|
20 |
|
->prototype('scalar')->end() |
129
|
20 |
|
->end() |
130
|
20 |
|
->arrayNode('types') |
131
|
20 |
|
->addDefaultsIfNotSet() |
132
|
20 |
|
->children() |
133
|
20 |
|
->scalarNode('warnings') |
134
|
20 |
|
->defaultValue(ErrorHandler::DEFAULT_USER_WARNING_CLASS) |
135
|
20 |
|
->end() |
136
|
20 |
|
->scalarNode('errors') |
137
|
20 |
|
->defaultValue(ErrorHandler::DEFAULT_USER_ERROR_CLASS) |
138
|
20 |
|
->end() |
139
|
20 |
|
->end() |
140
|
20 |
|
->end() |
141
|
20 |
|
->end() |
142
|
20 |
|
->end() |
143
|
|
|
|
144
|
20 |
|
->arrayNode('builders') |
145
|
20 |
|
->children() |
146
|
20 |
|
->append($this->addBuilderSection('field')) |
147
|
20 |
|
->append($this->addBuilderSection('args')) |
148
|
20 |
|
->end() |
149
|
20 |
|
->end() |
150
|
|
|
|
151
|
20 |
|
->end() |
152
|
20 |
|
->end() |
153
|
20 |
|
->arrayNode('templates') |
154
|
20 |
|
->addDefaultsIfNotSet() |
155
|
20 |
|
->children() |
156
|
20 |
|
->scalarNode('graphiql') |
157
|
20 |
|
->defaultValue('@OverblogGraphQL/GraphiQL/index.html.twig') |
158
|
20 |
|
->end() |
159
|
20 |
|
->end() |
160
|
20 |
|
->end() |
161
|
20 |
|
->arrayNode('services') |
162
|
20 |
|
->addDefaultsIfNotSet() |
163
|
20 |
|
->children() |
164
|
20 |
|
->scalarNode('executor') |
165
|
20 |
|
->defaultValue('overblog_graphql.executor.default') |
166
|
20 |
|
->end() |
167
|
20 |
|
->scalarNode('promise_adapter') |
168
|
20 |
|
->defaultValue('overblog_graphql.promise_adapter.default') |
169
|
20 |
|
->end() |
170
|
20 |
|
->scalarNode('expression_language') |
171
|
20 |
|
->defaultValue('overblog_graphql.expression_language.default') |
172
|
20 |
|
->end() |
173
|
20 |
|
->scalarNode('cache_expression_language_parser') |
174
|
20 |
|
->defaultValue('overblog_graphql.cache_expression_language_parser.default') |
175
|
20 |
|
->end() |
176
|
20 |
|
->end() |
177
|
20 |
|
->end() |
178
|
20 |
|
->arrayNode('security') |
179
|
20 |
|
->addDefaultsIfNotSet() |
180
|
20 |
|
->children() |
181
|
20 |
|
->append($this->addSecurityQuerySection('query_max_depth', QueryDepth::DISABLED)) |
182
|
20 |
|
->append($this->addSecurityQuerySection('query_max_complexity', QueryComplexity::DISABLED)) |
183
|
20 |
|
->booleanNode('handle_cors')->defaultFalse()->end() |
184
|
20 |
|
->end() |
185
|
20 |
|
->end() |
186
|
20 |
|
->arrayNode('versions') |
187
|
20 |
|
->addDefaultsIfNotSet() |
188
|
20 |
|
->children() |
189
|
20 |
|
->scalarNode('graphiql')->defaultValue('0.11')->end() |
190
|
20 |
|
->scalarNode('react')->defaultValue('15.6')->end() |
191
|
20 |
|
->scalarNode('fetch')->defaultValue('2.0')->end() |
192
|
20 |
|
->enumNode('relay')->values(['modern', 'classic'])->defaultValue('classic')->end() |
193
|
20 |
|
->end() |
194
|
20 |
|
->end() |
195
|
20 |
|
->end(); |
196
|
|
|
|
197
|
20 |
|
return $treeBuilder; |
198
|
|
|
} |
199
|
|
|
|
200
|
20 |
|
private function addBuilderSection($name) |
201
|
|
|
{ |
202
|
20 |
|
$builder = new TreeBuilder(); |
203
|
20 |
|
$node = $builder->root($name); |
204
|
20 |
|
$node->beforeNormalization() |
205
|
20 |
|
->ifTrue(function ($v) { |
206
|
1 |
|
return is_array($v) && !empty($v); |
207
|
20 |
|
}) |
208
|
20 |
|
->then(function ($v) { |
209
|
1 |
|
foreach ($v as $key => &$config) { |
210
|
1 |
|
if (is_string($config)) { |
211
|
|
|
$config = [ |
212
|
1 |
|
'alias' => $key, |
213
|
1 |
|
'class' => $config, |
214
|
|
|
]; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
1 |
|
return $v; |
219
|
20 |
|
}) |
220
|
20 |
|
->end(); |
221
|
|
|
|
222
|
20 |
|
$node->prototype('array') |
|
|
|
|
223
|
20 |
|
->children() |
224
|
20 |
|
->scalarNode('alias')->isRequired()->end() |
225
|
20 |
|
->scalarNode('class')->isRequired()->end() |
226
|
20 |
|
->end() |
227
|
20 |
|
->end() |
228
|
|
|
; |
229
|
|
|
|
230
|
20 |
|
return $node; |
231
|
|
|
} |
232
|
|
|
|
233
|
20 |
|
private function addSecurityQuerySection($name, $disabledValue) |
234
|
|
|
{ |
235
|
20 |
|
$builder = new TreeBuilder(); |
236
|
20 |
|
$node = $builder->root($name, 'scalar'); |
237
|
20 |
|
$node->beforeNormalization() |
238
|
20 |
|
->ifTrue(function ($v) { |
239
|
4 |
|
return is_string($v) && is_numeric($v); |
240
|
20 |
|
}) |
241
|
20 |
|
->then(function ($v) { |
242
|
2 |
|
return (int) $v; |
243
|
20 |
|
}) |
244
|
20 |
|
->end(); |
245
|
|
|
|
246
|
|
|
$node |
247
|
20 |
|
->info('Disabled if equal to false.') |
248
|
20 |
|
->beforeNormalization() |
249
|
20 |
|
->ifTrue(function ($v) { |
250
|
4 |
|
return false === $v; |
251
|
20 |
|
}) |
252
|
20 |
|
->then(function () use ($disabledValue) { |
253
|
|
|
return $disabledValue; |
254
|
20 |
|
}) |
255
|
20 |
|
->end() |
256
|
20 |
|
->defaultFalse() |
257
|
20 |
|
->validate() |
258
|
20 |
|
->ifTrue(function ($v) { |
259
|
4 |
|
return is_int($v) && $v < 0; |
260
|
20 |
|
}) |
261
|
|
|
->thenInvalid('"overblog_graphql.security.'.$name.'" must be greater or equal to 0.') |
262
|
|
|
->end() |
263
|
|
|
; |
264
|
|
|
|
265
|
|
|
return $node; |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
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: