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