1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
6
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
7
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
8
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
9
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
10
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidTypeException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This class contains the configuration information for the bundle. |
14
|
|
|
* |
15
|
|
|
* This information is solely responsible for how the different configuration |
16
|
|
|
* sections are normalized, and merged. |
17
|
|
|
* |
18
|
|
|
* @author David Buchmann <[email protected]> |
19
|
|
|
* @author Tobias Nyholm <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Configuration implements ConfigurationInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Whether to use the debug mode. |
25
|
|
|
* |
26
|
|
|
* @see https://github.com/doctrine/DoctrineBundle/blob/v1.5.2/DependencyInjection/Configuration.php#L31-L41 |
27
|
|
|
* |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
private $debug; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param bool $debug |
34
|
17 |
|
*/ |
35
|
|
|
public function __construct($debug) |
36
|
17 |
|
{ |
37
|
17 |
|
$this->debug = (bool) $debug; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
17 |
|
*/ |
43
|
|
|
public function getConfigTreeBuilder() |
44
|
17 |
|
{ |
45
|
17 |
|
$treeBuilder = new TreeBuilder(); |
46
|
|
|
$rootNode = $treeBuilder->root('httplug'); |
47
|
17 |
|
|
48
|
17 |
|
$this->configureClients($rootNode); |
49
|
|
|
$this->configureSharedPlugins($rootNode); |
50
|
|
|
|
51
|
17 |
|
$rootNode |
|
|
|
|
52
|
|
|
->validate() |
53
|
14 |
|
->ifTrue(function ($v) { |
54
|
14 |
|
return !empty($v['classes']['client']) |
55
|
11 |
|
|| !empty($v['classes']['message_factory']) |
56
|
14 |
|
|| !empty($v['classes']['uri_factory']) |
57
|
17 |
|
|| !empty($v['classes']['stream_factory']); |
58
|
|
|
}) |
59
|
3 |
|
->then(function ($v) { |
60
|
3 |
|
foreach ($v['classes'] as $key => $class) { |
61
|
1 |
|
if (null !== $class && !class_exists($class)) { |
62
|
1 |
|
throw new InvalidConfigurationException(sprintf( |
63
|
1 |
|
'Class %s specified for httplug.classes.%s does not exist.', |
64
|
|
|
$class, |
65
|
1 |
|
$key |
66
|
|
|
)); |
67
|
2 |
|
} |
68
|
|
|
} |
69
|
2 |
|
|
70
|
17 |
|
return $v; |
71
|
17 |
|
}) |
72
|
17 |
|
->end() |
73
|
|
|
->beforeNormalization() |
74
|
17 |
|
->ifTrue(function ($v) { |
75
|
17 |
|
return is_array($v) && array_key_exists('toolbar', $v) && is_array($v['toolbar']); |
76
|
|
|
}) |
77
|
4 |
|
->then(function ($v) { |
78
|
1 |
|
if (array_key_exists('profiling', $v)) { |
79
|
|
|
throw new InvalidConfigurationException('Can\'t configure both "toolbar" and "profiling" section. The "toolbar" config is deprecated as of version 1.3.0, please only use "profiling".'); |
80
|
|
|
} |
81
|
3 |
|
|
82
|
|
|
@trigger_error('"httplug.toolbar" config is deprecated since version 1.3 and will be removed in 2.0. Use "httplug.profiling" instead.', E_USER_DEPRECATED); |
|
|
|
|
83
|
3 |
|
|
84
|
1 |
|
if (array_key_exists('enabled', $v['toolbar']) && 'auto' === $v['toolbar']['enabled']) { |
85
|
1 |
|
@trigger_error('"auto" value in "httplug.toolbar" config is deprecated since version 1.3 and will be removed in 2.0. Use a boolean value instead.', E_USER_DEPRECATED); |
|
|
|
|
86
|
1 |
|
$v['toolbar']['enabled'] = $this->debug; |
87
|
|
|
} |
88
|
3 |
|
|
89
|
|
|
$v['profiling'] = $v['toolbar']; |
90
|
3 |
|
|
91
|
|
|
unset($v['toolbar']); |
92
|
3 |
|
|
93
|
17 |
|
return $v; |
94
|
17 |
|
}) |
95
|
17 |
|
->end() |
96
|
17 |
|
->fixXmlConfig('client') |
97
|
17 |
|
->children() |
98
|
17 |
|
->arrayNode('main_alias') |
99
|
17 |
|
->addDefaultsIfNotSet() |
100
|
17 |
|
->info('Configure which service the main alias point to.') |
101
|
17 |
|
->children() |
102
|
17 |
|
->scalarNode('client')->defaultValue('httplug.client.default')->end() |
103
|
17 |
|
->scalarNode('message_factory')->defaultValue('httplug.message_factory.default')->end() |
104
|
17 |
|
->scalarNode('uri_factory')->defaultValue('httplug.uri_factory.default')->end() |
105
|
17 |
|
->scalarNode('stream_factory')->defaultValue('httplug.stream_factory.default')->end() |
106
|
17 |
|
->end() |
107
|
17 |
|
->end() |
108
|
17 |
|
->arrayNode('classes') |
109
|
17 |
|
->addDefaultsIfNotSet() |
110
|
17 |
|
->info('Overwrite a service class instead of using the discovery mechanism.') |
111
|
17 |
|
->children() |
112
|
17 |
|
->scalarNode('client')->defaultNull()->end() |
113
|
17 |
|
->scalarNode('message_factory')->defaultNull()->end() |
114
|
17 |
|
->scalarNode('uri_factory')->defaultNull()->end() |
115
|
17 |
|
->scalarNode('stream_factory')->defaultNull()->end() |
116
|
17 |
|
->end() |
117
|
17 |
|
->end() |
118
|
17 |
|
->arrayNode('profiling') |
119
|
17 |
|
->addDefaultsIfNotSet() |
120
|
17 |
|
->treatFalseLike(['enabled' => false]) |
121
|
17 |
|
->treatTrueLike(['enabled' => true]) |
122
|
17 |
|
->treatNullLike(['enabled' => $this->debug]) |
123
|
17 |
|
->info('Extend the debug profiler with information about requests.') |
124
|
17 |
|
->children() |
125
|
17 |
|
->booleanNode('enabled') |
126
|
17 |
|
->info('Turn the toolbar on or off. Defaults to kernel debug mode.') |
127
|
17 |
|
->defaultValue($this->debug) |
128
|
17 |
|
->end() |
129
|
17 |
|
->scalarNode('formatter')->defaultNull()->end() |
130
|
17 |
|
->integerNode('captured_body_length') |
131
|
17 |
|
->defaultValue(0) |
132
|
17 |
|
->info('Limit long HTTP message bodies to x characters. If set to 0 we do not read the message body. Only available with the default formatter (FullHttpMessageFormatter).') |
133
|
17 |
|
->end() |
134
|
17 |
|
->end() |
135
|
17 |
|
->end() |
136
|
17 |
|
->arrayNode('discovery') |
137
|
17 |
|
->addDefaultsIfNotSet() |
138
|
17 |
|
->info('Control what clients should be found by the discovery.') |
139
|
17 |
|
->children() |
140
|
17 |
|
->scalarNode('client') |
141
|
17 |
|
->defaultValue('auto') |
142
|
17 |
|
->info('Set to "auto" to see auto discovered client in the web profiler. If provided a service id for a client then this client will be found by auto discovery.') |
143
|
17 |
|
->end() |
144
|
17 |
|
->scalarNode('async_client') |
145
|
17 |
|
->defaultNull() |
146
|
17 |
|
->info('Set to "auto" to see auto discovered client in the web profiler. If provided a service id for a client then this client will be found by auto discovery.') |
147
|
17 |
|
->end() |
148
|
17 |
|
->end() |
149
|
17 |
|
->end() |
150
|
|
|
->end(); |
151
|
17 |
|
|
152
|
|
|
return $treeBuilder; |
153
|
|
|
} |
154
|
17 |
|
|
155
|
|
|
private function configureClients(ArrayNodeDefinition $root) |
156
|
17 |
|
{ |
157
|
17 |
|
$root->children() |
|
|
|
|
158
|
17 |
|
->arrayNode('clients') |
159
|
17 |
|
->useAttributeAsKey('name') |
160
|
17 |
|
->prototype('array') |
161
|
17 |
|
->fixXmlConfig('plugin') |
162
|
|
|
->validate() |
163
|
|
|
->ifTrue(function ($config) { |
164
|
7 |
|
// Make sure we only allow one of these to be true |
165
|
17 |
|
return (bool) $config['flexible_client'] + (bool) $config['http_methods_client'] + (bool) $config['batch_client'] >= 2; |
166
|
17 |
|
}) |
167
|
17 |
|
->thenInvalid('A http client can\'t be decorated with several of FlexibleHttpClient, HttpMethodsClient and BatchClient. Only one of the following options can be true. ("flexible_client", "http_methods_client", "batch_client")') |
168
|
17 |
|
->end() |
169
|
17 |
|
->children() |
170
|
17 |
|
->scalarNode('factory') |
171
|
17 |
|
->isRequired() |
172
|
17 |
|
->cannotBeEmpty() |
173
|
17 |
|
->info('The service id of a factory to use when creating the adapter.') |
174
|
17 |
|
->end() |
175
|
17 |
|
->booleanNode('flexible_client') |
176
|
17 |
|
->defaultFalse() |
177
|
17 |
|
->info('Set to true to get the client wrapped in a FlexibleHttpClient which emulates async or sync behavior.') |
178
|
17 |
|
->end() |
179
|
17 |
|
->booleanNode('http_methods_client') |
180
|
17 |
|
->defaultFalse() |
181
|
17 |
|
->info('Set to true to get the client wrapped in a HttpMethodsClient which emulates provides functions for HTTP verbs.') |
182
|
17 |
|
->end() |
183
|
17 |
|
->booleanNode('batch_client') |
184
|
17 |
|
->defaultFalse() |
185
|
17 |
|
->info('Set to true to get the client wrapped in a BatchClient which allows you to send multiple request at the same time.') |
186
|
17 |
|
->end() |
187
|
17 |
|
->variableNode('config')->defaultValue([])->end() |
188
|
17 |
|
->append($this->createClientPluginNode()) |
189
|
17 |
|
->end() |
190
|
17 |
|
->end() |
191
|
17 |
|
->end(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param ArrayNodeDefinition $root |
196
|
17 |
|
*/ |
197
|
|
|
private function configureSharedPlugins(ArrayNodeDefinition $root) |
198
|
|
|
{ |
199
|
17 |
|
$pluginsNode = $root |
200
|
17 |
|
->children() |
201
|
17 |
|
->arrayNode('plugins') |
202
|
17 |
|
->info('Global plugin configuration. Plugins need to be explicitly added to clients.') |
203
|
|
|
->addDefaultsIfNotSet() |
204
|
17 |
|
// don't call end to get the plugins node |
205
|
17 |
|
; |
206
|
17 |
|
$this->addSharedPluginNodes($pluginsNode); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Createplugins node of a client. |
211
|
|
|
* |
212
|
|
|
* @return ArrayNodeDefinition The plugin node |
213
|
17 |
|
*/ |
214
|
|
|
private function createClientPluginNode() |
215
|
17 |
|
{ |
216
|
17 |
|
$builder = new TreeBuilder(); |
217
|
|
|
$node = $builder->root('plugins'); |
218
|
|
|
|
219
|
|
|
/** @var ArrayNodeDefinition $pluginList */ |
220
|
17 |
|
$pluginList = $node |
221
|
17 |
|
->info('A list of plugin service ids and client specific plugin definitions. The order is important.') |
222
|
17 |
|
->prototype('array') |
223
|
|
|
; |
224
|
|
|
$pluginList |
225
|
17 |
|
// support having just a service id in the list |
226
|
|
|
->beforeNormalization() |
227
|
8 |
|
->always(function ($plugin) { |
228
|
|
|
if (is_string($plugin)) { |
229
|
|
|
return [ |
230
|
7 |
|
'reference' => [ |
231
|
7 |
|
'enabled' => true, |
232
|
7 |
|
'id' => $plugin, |
233
|
7 |
|
], |
234
|
|
|
]; |
235
|
|
|
} |
236
|
5 |
|
|
237
|
17 |
|
return $plugin; |
238
|
17 |
|
}) |
239
|
|
|
->end() |
240
|
17 |
|
|
241
|
|
|
->validate() |
242
|
7 |
|
->always(function ($plugins) { |
243
|
7 |
|
foreach ($plugins as $name => $definition) { |
244
|
7 |
|
if ('authentication' === $name) { |
245
|
7 |
|
if (!count($definition)) { |
246
|
7 |
|
unset($plugins['authentication']); |
247
|
7 |
|
} |
248
|
7 |
|
} elseif (!$definition['enabled']) { |
249
|
7 |
|
unset($plugins[$name]); |
250
|
7 |
|
} |
251
|
|
|
} |
252
|
7 |
|
|
253
|
17 |
|
return $plugins; |
254
|
17 |
|
}) |
255
|
|
|
->end() |
256
|
17 |
|
; |
257
|
|
|
$this->addSharedPluginNodes($pluginList, true); |
258
|
|
|
|
259
|
17 |
|
$pluginList |
260
|
17 |
|
->children() |
261
|
17 |
|
->arrayNode('reference') |
262
|
17 |
|
->canBeEnabled() |
263
|
17 |
|
->info('Reference to a plugin service') |
264
|
17 |
|
->children() |
265
|
17 |
|
->scalarNode('id') |
266
|
17 |
|
->info('Service id of a plugin') |
267
|
17 |
|
->isRequired() |
268
|
17 |
|
->cannotBeEmpty() |
269
|
17 |
|
->end() |
270
|
17 |
|
->end() |
271
|
17 |
|
->end() |
272
|
17 |
|
->arrayNode('add_host') |
273
|
17 |
|
->canBeEnabled() |
274
|
17 |
|
->addDefaultsIfNotSet() |
275
|
17 |
|
->info('Set scheme, host and port in the request URI.') |
276
|
17 |
|
->children() |
277
|
17 |
|
->scalarNode('host') |
278
|
17 |
|
->info('Host name including protocol and optionally the port number, e.g. https://api.local:8000') |
279
|
17 |
|
->isRequired() |
280
|
17 |
|
->cannotBeEmpty() |
281
|
17 |
|
->end() |
282
|
17 |
|
->scalarNode('replace') |
283
|
17 |
|
->info('Whether to replace the host if request already specifies one') |
284
|
17 |
|
->defaultValue(false) |
285
|
17 |
|
->end() |
286
|
17 |
|
->end() |
287
|
17 |
|
->end() |
288
|
17 |
|
->arrayNode('header_append') |
289
|
17 |
|
->canBeEnabled() |
290
|
17 |
|
->info('Append headers to the request. If the header already exists the value will be appended to the current value.') |
291
|
17 |
|
->fixXmlConfig('header') |
292
|
17 |
|
->children() |
293
|
17 |
|
->arrayNode('headers') |
294
|
17 |
|
->info('Keys are the header names, values the header values') |
295
|
17 |
|
->normalizeKeys(false) |
296
|
17 |
|
->useAttributeAsKey('name') |
297
|
17 |
|
->prototype('scalar')->end() |
298
|
17 |
|
->end() |
299
|
17 |
|
->end() |
300
|
17 |
|
->end() |
301
|
17 |
|
->arrayNode('header_defaults') |
302
|
17 |
|
->canBeEnabled() |
303
|
17 |
|
->info('Set header to default value if it does not exist.') |
304
|
17 |
|
->fixXmlConfig('header') |
305
|
17 |
|
->children() |
306
|
17 |
|
->arrayNode('headers') |
307
|
17 |
|
->info('Keys are the header names, values the header values') |
308
|
17 |
|
->normalizeKeys(false) |
309
|
17 |
|
->useAttributeAsKey('name') |
310
|
17 |
|
->prototype('scalar')->end() |
311
|
17 |
|
->end() |
312
|
17 |
|
->end() |
313
|
17 |
|
->end() |
314
|
17 |
|
->arrayNode('header_set') |
315
|
17 |
|
->canBeEnabled() |
316
|
17 |
|
->info('Set headers to requests. If the header does not exist it wil be set, if the header already exists it will be replaced.') |
317
|
17 |
|
->fixXmlConfig('header') |
318
|
17 |
|
->children() |
319
|
17 |
|
->arrayNode('headers') |
320
|
17 |
|
->info('Keys are the header names, values the header values') |
321
|
17 |
|
->normalizeKeys(false) |
322
|
17 |
|
->useAttributeAsKey('name') |
323
|
17 |
|
->prototype('scalar')->end() |
324
|
17 |
|
->end() |
325
|
17 |
|
->end() |
326
|
17 |
|
->end() |
327
|
17 |
|
->arrayNode('header_remove') |
328
|
17 |
|
->canBeEnabled() |
329
|
17 |
|
->info('Remove headers from requests.') |
330
|
17 |
|
->fixXmlConfig('header') |
331
|
17 |
|
->children() |
332
|
17 |
|
->arrayNode('headers') |
333
|
17 |
|
->info('List of header names to remove') |
334
|
17 |
|
->prototype('scalar')->end() |
335
|
17 |
|
->end() |
336
|
17 |
|
->end() |
337
|
17 |
|
->end() |
338
|
17 |
|
->end() |
339
|
|
|
->end(); |
340
|
17 |
|
|
341
|
|
|
return $node; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Add the definitions for shared plugin configurations. |
346
|
|
|
* |
347
|
|
|
* @param ArrayNodeDefinition $pluginNode The node to add to. |
348
|
|
|
* @param bool $disableAll Some shared plugins are enabled by default. On the client, all are disabled by default. |
349
|
17 |
|
*/ |
350
|
|
|
private function addSharedPluginNodes(ArrayNodeDefinition $pluginNode, $disableAll = false) |
351
|
17 |
|
{ |
352
|
|
|
$children = $pluginNode->children(); |
353
|
17 |
|
|
354
|
|
|
$children->append($this->createAuthenticationPluginNode()); |
355
|
17 |
|
|
356
|
17 |
|
$children->arrayNode('cache') |
357
|
17 |
|
->canBeEnabled() |
358
|
17 |
|
->addDefaultsIfNotSet() |
359
|
17 |
|
->children() |
360
|
17 |
|
->scalarNode('cache_pool') |
361
|
17 |
|
->info('This must be a service id to a service implementing Psr\Cache\CacheItemPoolInterface') |
362
|
17 |
|
->isRequired() |
363
|
17 |
|
->cannotBeEmpty() |
364
|
17 |
|
->end() |
365
|
17 |
|
->scalarNode('stream_factory') |
366
|
17 |
|
->info('This must be a service id to a service implementing Http\Message\StreamFactory') |
367
|
17 |
|
->defaultValue('httplug.stream_factory') |
368
|
17 |
|
->cannotBeEmpty() |
369
|
17 |
|
->end() |
370
|
17 |
|
->arrayNode('config') |
371
|
17 |
|
->addDefaultsIfNotSet() |
372
|
17 |
|
->validate() |
373
|
17 |
|
->ifTrue(function ($config) { |
374
|
17 |
|
// Cannot set both respect_cache_headers and respect_response_cache_directives |
375
|
17 |
|
return isset($config['respect_cache_headers'], $config['respect_response_cache_directives']); |
376
|
17 |
|
}) |
377
|
17 |
|
->thenInvalid('You can\'t provide config option "respect_cache_headers" and "respect_response_cache_directives" simultaniously. Use "respect_response_cache_directives" instead.') |
378
|
|
|
->end() |
379
|
|
|
->children() |
380
|
17 |
|
->scalarNode('default_ttl')->defaultValue(0)->end() |
381
|
17 |
|
->enumNode('respect_cache_headers')->values([null, true, false])->end() |
382
|
17 |
|
->variableNode('respect_response_cache_directives') |
383
|
17 |
|
->validate() |
384
|
17 |
|
->always(function ($v) { |
385
|
17 |
|
if (is_null($v) || is_array($v)) { |
386
|
17 |
|
return $v; |
387
|
17 |
|
} |
388
|
17 |
|
throw new InvalidTypeException(); |
389
|
17 |
|
}) |
390
|
|
|
->end() |
391
|
|
|
->defaultNull() |
392
|
17 |
|
->end() |
393
|
17 |
|
->end() |
394
|
17 |
|
->end() |
395
|
17 |
|
->end() |
396
|
17 |
|
->end(); |
397
|
17 |
|
// End cache plugin |
398
|
17 |
|
|
399
|
|
|
$children->arrayNode('cookie') |
400
|
|
|
->canBeEnabled() |
401
|
17 |
|
->children() |
402
|
17 |
|
->scalarNode('cookie_jar') |
403
|
17 |
|
->info('This must be a service id to a service implementing Http\Message\CookieJar') |
404
|
17 |
|
->isRequired() |
405
|
17 |
|
->cannotBeEmpty() |
406
|
17 |
|
->end() |
407
|
17 |
|
->end() |
408
|
17 |
|
->end(); |
409
|
17 |
|
// End cookie plugin |
410
|
17 |
|
|
411
|
|
|
$decoder = $children->arrayNode('decoder'); |
412
|
|
|
$disableAll ? $decoder->canBeEnabled() : $decoder->canBeDisabled(); |
413
|
17 |
|
$decoder->addDefaultsIfNotSet() |
414
|
17 |
|
->children() |
415
|
17 |
|
->scalarNode('use_content_encoding')->defaultTrue()->end() |
416
|
17 |
|
->end() |
417
|
17 |
|
->end(); |
418
|
17 |
|
// End decoder plugin |
419
|
17 |
|
|
420
|
17 |
|
$children->arrayNode('history') |
421
|
17 |
|
->canBeEnabled() |
422
|
17 |
|
->children() |
423
|
17 |
|
->scalarNode('journal') |
424
|
17 |
|
->info('This must be a service id to a service implementing Http\Client\Plugin\Journal') |
425
|
17 |
|
->isRequired() |
426
|
17 |
|
->cannotBeEmpty() |
427
|
17 |
|
->end() |
428
|
|
|
->end() |
429
|
|
|
->end(); |
430
|
17 |
|
// End history plugin |
431
|
17 |
|
|
432
|
17 |
|
$logger = $children->arrayNode('logger'); |
433
|
17 |
|
$disableAll ? $logger->canBeEnabled() : $logger->canBeDisabled(); |
434
|
17 |
|
$logger->addDefaultsIfNotSet() |
435
|
17 |
|
->children() |
436
|
17 |
|
->scalarNode('logger') |
437
|
17 |
|
->info('This must be a service id to a service implementing Psr\Log\LoggerInterface') |
438
|
|
|
->defaultValue('logger') |
439
|
|
|
->cannotBeEmpty() |
440
|
17 |
|
->end() |
441
|
17 |
|
->scalarNode('formatter') |
442
|
17 |
|
->info('This must be a service id to a service implementing Http\Message\Formatter') |
443
|
17 |
|
->defaultNull() |
444
|
17 |
|
->end() |
445
|
17 |
|
->end() |
446
|
17 |
|
->end(); |
447
|
|
|
// End logger plugin |
448
|
|
|
|
449
|
17 |
|
$redirect = $children->arrayNode('redirect'); |
450
|
17 |
|
$disableAll ? $redirect->canBeEnabled() : $redirect->canBeDisabled(); |
451
|
17 |
|
$redirect->addDefaultsIfNotSet() |
452
|
17 |
|
->children() |
453
|
17 |
|
->scalarNode('preserve_header')->defaultTrue()->end() |
454
|
17 |
|
->scalarNode('use_default_for_multiple')->defaultTrue()->end() |
455
|
17 |
|
->end() |
456
|
17 |
|
->end(); |
457
|
17 |
|
// End redirect plugin |
458
|
17 |
|
|
459
|
17 |
|
$retry = $children->arrayNode('retry'); |
460
|
|
|
$disableAll ? $retry->canBeEnabled() : $retry->canBeDisabled(); |
461
|
17 |
|
$retry->addDefaultsIfNotSet() |
462
|
|
|
->children() |
463
|
|
|
->scalarNode('retry')->defaultValue(1)->end() // TODO: should be called retries for consistency with the class |
464
|
|
|
->end() |
465
|
|
|
->end(); |
466
|
|
|
// End retry plugin |
467
|
|
|
|
468
|
17 |
|
$stopwatch = $children->arrayNode('stopwatch'); |
469
|
|
|
$disableAll ? $stopwatch->canBeEnabled() : $stopwatch->canBeDisabled(); |
470
|
17 |
|
$stopwatch->addDefaultsIfNotSet() |
471
|
17 |
|
->children() |
472
|
|
|
->scalarNode('stopwatch') |
473
|
17 |
|
->info('This must be a service id to a service extending Symfony\Component\Stopwatch\Stopwatch') |
474
|
17 |
|
->defaultValue('debug.stopwatch') |
475
|
17 |
|
->cannotBeEmpty() |
476
|
17 |
|
->end() |
477
|
17 |
|
->end() |
478
|
5 |
|
->end(); |
479
|
5 |
|
// End stopwatch plugin |
480
|
4 |
|
} |
481
|
4 |
|
|
482
|
2 |
|
/** |
483
|
1 |
|
* Create configuration for authentication plugin. |
484
|
1 |
|
* |
485
|
2 |
|
* @return NodeDefinition Definition for the authentication node in the plugins list. |
486
|
2 |
|
*/ |
487
|
1 |
|
private function createAuthenticationPluginNode() |
488
|
1 |
|
{ |
489
|
1 |
|
$builder = new TreeBuilder(); |
490
|
1 |
|
$node = $builder->root('authentication'); |
491
|
4 |
|
$node |
|
|
|
|
492
|
|
|
->useAttributeAsKey('name') |
493
|
4 |
|
->prototype('array') |
494
|
17 |
|
->validate() |
495
|
17 |
|
->always() |
496
|
17 |
|
->then(function ($config) { |
497
|
17 |
|
switch ($config['type']) { |
498
|
17 |
|
case 'basic': |
499
|
17 |
|
$this->validateAuthenticationType(['username', 'password'], $config, 'basic'); |
500
|
17 |
|
break; |
501
|
17 |
|
case 'bearer': |
502
|
17 |
|
$this->validateAuthenticationType(['token'], $config, 'bearer'); |
503
|
17 |
|
break; |
504
|
17 |
|
case 'service': |
505
|
17 |
|
$this->validateAuthenticationType(['service'], $config, 'service'); |
506
|
17 |
|
break; |
507
|
17 |
|
case 'wsse': |
508
|
17 |
|
$this->validateAuthenticationType(['username', 'password'], $config, 'wsse'); |
509
|
|
|
break; |
510
|
17 |
|
} |
511
|
|
|
|
512
|
|
|
return $config; |
513
|
|
|
}) |
514
|
|
|
->end() |
515
|
|
|
->children() |
516
|
|
|
->enumNode('type') |
517
|
|
|
->values(['basic', 'bearer', 'wsse', 'service']) |
518
|
|
|
->isRequired() |
519
|
|
|
->cannotBeEmpty() |
520
|
|
|
->end() |
521
|
|
|
->scalarNode('username')->end() |
522
|
5 |
|
->scalarNode('password')->end() |
523
|
|
|
->scalarNode('token')->end() |
524
|
5 |
|
->scalarNode('service')->end() |
525
|
5 |
|
->end() |
526
|
5 |
|
->end() |
527
|
5 |
|
->end(); // End authentication plugin |
528
|
|
|
|
529
|
5 |
|
return $node; |
530
|
4 |
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
1 |
|
* Validate that the configuration fragment has the specified keys and none other. |
534
|
1 |
|
* |
535
|
1 |
|
* @param array $expected Fields that must exist |
536
|
1 |
|
* @param array $actual Actual configuration hashmap |
537
|
1 |
|
* @param string $authName Name of authentication method for error messages |
538
|
1 |
|
* |
539
|
|
|
* @throws InvalidConfigurationException If $actual does not have exactly the keys specified in $expected (plus 'type') |
540
|
|
|
*/ |
541
|
|
|
private function validateAuthenticationType(array $expected, array $actual, $authName) |
542
|
|
|
{ |
543
|
|
|
unset($actual['type']); |
544
|
|
|
$actual = array_keys($actual); |
545
|
|
|
sort($actual); |
546
|
|
|
sort($expected); |
547
|
|
|
|
548
|
|
|
if ($expected === $actual) { |
549
|
|
|
return; |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
throw new InvalidConfigurationException(sprintf( |
553
|
|
|
'Authentication "%s" requires %s but got %s', |
554
|
|
|
$authName, |
555
|
|
|
implode(', ', $expected), |
556
|
|
|
implode(', ', $actual) |
557
|
|
|
)); |
558
|
|
|
} |
559
|
|
|
} |
560
|
|
|
|
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: