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