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