1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\ArrayNode; |
6
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
7
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
8
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
9
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This class contains the configuration information for the bundle. |
13
|
|
|
* |
14
|
|
|
* This information is solely responsible for how the different configuration |
15
|
|
|
* sections are normalized, and merged. |
16
|
|
|
* |
17
|
|
|
* @author David Buchmann <[email protected]> |
18
|
|
|
* @author Tobias Nyholm <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Configuration implements ConfigurationInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Whether to use the debug mode. |
24
|
|
|
* |
25
|
|
|
* @see https://github.com/doctrine/DoctrineBundle/blob/v1.5.2/DependencyInjection/Configuration.php#L31-L41 |
26
|
|
|
* |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private $debug; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param bool $debug |
33
|
|
|
*/ |
34
|
12 |
|
public function __construct($debug) |
35
|
|
|
{ |
36
|
12 |
|
$this->debug = (bool) $debug; |
37
|
12 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
12 |
|
public function getConfigTreeBuilder() |
43
|
|
|
{ |
44
|
12 |
|
$treeBuilder = new TreeBuilder(); |
45
|
12 |
|
$rootNode = $treeBuilder->root('httplug'); |
46
|
|
|
|
47
|
12 |
|
$this->configureClients($rootNode); |
48
|
12 |
|
$this->configurePlugins($rootNode); |
49
|
|
|
|
50
|
|
|
$rootNode |
51
|
12 |
|
->validate() |
52
|
|
|
->ifTrue(function ($v) { |
53
|
11 |
|
return !empty($v['classes']['client']) |
54
|
11 |
|
|| !empty($v['classes']['message_factory']) |
55
|
8 |
|
|| !empty($v['classes']['uri_factory']) |
56
|
11 |
|
|| !empty($v['classes']['stream_factory']); |
57
|
12 |
|
}) |
58
|
|
|
->then(function ($v) { |
59
|
3 |
|
foreach ($v['classes'] as $key => $class) { |
60
|
3 |
|
if (null !== $class && !class_exists($class)) { |
61
|
1 |
|
throw new InvalidConfigurationException(sprintf( |
62
|
1 |
|
'Class %s specified for httplug.classes.%s does not exist.', |
63
|
1 |
|
$class, |
64
|
|
|
$key |
65
|
1 |
|
)); |
66
|
|
|
} |
67
|
2 |
|
} |
68
|
|
|
|
69
|
2 |
|
return $v; |
70
|
12 |
|
}) |
71
|
12 |
|
->end() |
72
|
12 |
|
->beforeNormalization() // @deprecated toolbar in 1.3.0, use profiling instead |
73
|
12 |
|
->ifTrue(function ($v) { |
74
|
12 |
|
return is_array($v) && array_key_exists('toolbar', $v) && is_array($v['toolbar']); |
75
|
12 |
|
}) |
76
|
12 |
|
->then(function ($v) { |
77
|
12 |
|
if (array_key_exists('enabled', $v['toolbar']) && 'auto' === $v['toolbar']['enabled']) { |
78
|
12 |
|
$v['toolbar']['enabled'] = $this->debug; // @deprecated value auto in 1.3.0 |
79
|
12 |
|
} |
80
|
12 |
|
|
81
|
12 |
|
if (array_key_exists('profiling', $v) && is_array($v['profiling'])) { |
82
|
12 |
|
$v['profiling'] = array_merge($v['profiling'], $v['toolbar']); |
83
|
12 |
|
} else { |
84
|
12 |
|
$v['profiling'] = $v['toolbar']; |
85
|
12 |
|
} |
86
|
12 |
|
|
87
|
12 |
|
unset($v['toolbar']); |
88
|
12 |
|
|
89
|
12 |
|
return $v; |
90
|
12 |
|
}) |
91
|
12 |
|
->end() |
92
|
12 |
|
->children() |
93
|
12 |
|
->arrayNode('main_alias') |
94
|
12 |
|
->addDefaultsIfNotSet() |
95
|
12 |
|
->info('Configure which service the main alias point to.') |
96
|
12 |
|
->children() |
97
|
12 |
|
->scalarNode('client')->defaultValue('httplug.client.default')->end() |
98
|
12 |
|
->scalarNode('message_factory')->defaultValue('httplug.message_factory.default')->end() |
99
|
12 |
|
->scalarNode('uri_factory')->defaultValue('httplug.uri_factory.default')->end() |
100
|
|
|
->scalarNode('stream_factory')->defaultValue('httplug.stream_factory.default')->end() |
101
|
1 |
|
->end() |
102
|
12 |
|
->end() |
103
|
12 |
|
->arrayNode('classes') |
104
|
12 |
|
->addDefaultsIfNotSet() |
105
|
12 |
|
->info('Overwrite a service class instead of using the discovery mechanism.') |
106
|
12 |
|
->children() |
107
|
12 |
|
->scalarNode('client')->defaultNull()->end() |
108
|
12 |
|
->scalarNode('message_factory')->defaultNull()->end() |
109
|
12 |
|
->scalarNode('uri_factory')->defaultNull()->end() |
110
|
12 |
|
->scalarNode('stream_factory')->defaultNull()->end() |
111
|
12 |
|
->end() |
112
|
12 |
|
->end() |
113
|
12 |
|
->arrayNode('profiling') |
114
|
12 |
|
->addDefaultsIfNotSet() |
115
|
12 |
|
->treatFalseLike(['enabled' => false]) |
116
|
12 |
|
->treatTrueLike(['enabled' => true]) |
117
|
12 |
|
->treatNullLike(['enabled' => $this->debug]) |
118
|
12 |
|
->info('Extend the debug profiler with information about requests.') |
119
|
12 |
|
->children() |
120
|
12 |
|
->booleanNode('enabled') |
121
|
12 |
|
->info('Turn the toolbar on or off. Defaults to kernel debug mode.') |
122
|
12 |
|
->defaultValue($this->debug) |
123
|
12 |
|
->end() |
124
|
12 |
|
->scalarNode('formatter')->defaultNull()->end() |
125
|
12 |
|
->scalarNode('captured_body_length') |
126
|
12 |
|
->defaultValue(0) |
127
|
12 |
|
->canNotBeEmpty() |
128
|
12 |
|
->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).') |
129
|
12 |
|
->end() |
130
|
|
|
->end() |
131
|
12 |
|
->end() |
132
|
|
|
->arrayNode('discovery') |
133
|
|
|
->addDefaultsIfNotSet() |
134
|
12 |
|
->info('Control what clients should be found by the discovery.') |
135
|
|
|
->children() |
136
|
12 |
|
->scalarNode('client') |
137
|
12 |
|
->defaultValue('auto') |
138
|
12 |
|
->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.') |
139
|
|
|
->end() |
140
|
3 |
|
->scalarNode('async_client') |
141
|
3 |
|
->defaultNull() |
142
|
|
|
->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
|
|
|
->end() |
144
|
|
|
->end() |
145
|
12 |
|
->end() |
146
|
12 |
|
->end(); |
147
|
12 |
|
|
148
|
12 |
|
return $treeBuilder; |
149
|
12 |
|
} |
150
|
12 |
|
|
151
|
12 |
|
protected function configureClients(ArrayNodeDefinition $root) |
152
|
12 |
|
{ |
153
|
12 |
|
$root->children() |
154
|
12 |
|
->arrayNode('clients') |
155
|
12 |
|
->validate() |
156
|
12 |
|
->ifTrue(function ($clients) { |
157
|
12 |
|
foreach ($clients as $name => $config) { |
158
|
12 |
|
return $config['flexible_client'] && $config['http_methods_client']; |
159
|
12 |
|
} |
160
|
12 |
|
|
161
|
12 |
|
return false; |
162
|
12 |
|
}) |
163
|
12 |
|
->thenInvalid('A http client can\'t be decorated with both FlexibleHttpClient and HttpMethodsClient. Only one of the following options can be true. ("flexible_client", "http_methods_client")')->end() |
164
|
12 |
|
->useAttributeAsKey('name') |
165
|
12 |
|
->prototype('array') |
166
|
12 |
|
->children() |
167
|
12 |
|
->scalarNode('factory') |
168
|
12 |
|
->isRequired() |
169
|
12 |
|
->cannotBeEmpty() |
170
|
12 |
|
->info('The service id of a factory to use when creating the adapter.') |
171
|
|
|
->end() |
172
|
|
|
->booleanNode('flexible_client') |
173
|
|
|
->defaultFalse() |
174
|
|
|
->info('Set to true to get the client wrapped in a FlexibleHttpClient which emulates async or sync behavior.') |
175
|
12 |
|
->end() |
176
|
|
|
->booleanNode('http_methods_client') |
177
|
12 |
|
->defaultFalse() |
178
|
12 |
|
->info('Set to true to get the client wrapped in a HttpMethodsClient which emulates provides functions for HTTP verbs.') |
179
|
12 |
|
->end() |
180
|
12 |
|
->arrayNode('plugins') |
181
|
12 |
|
->info('A list of service ids of plugins. The order is important.') |
182
|
|
|
->prototype('scalar')->end() |
183
|
12 |
|
->end() |
184
|
12 |
|
->variableNode('config')->defaultValue([])->end() |
185
|
12 |
|
->end() |
186
|
12 |
|
->end(); |
187
|
12 |
|
} |
188
|
12 |
|
|
189
|
12 |
|
/** |
190
|
12 |
|
* @param ArrayNodeDefinition $root |
191
|
12 |
|
*/ |
192
|
12 |
|
protected function configurePlugins(ArrayNodeDefinition $root) |
193
|
12 |
|
{ |
194
|
12 |
|
$root->children() |
195
|
12 |
|
->arrayNode('plugins') |
196
|
12 |
|
->addDefaultsIfNotSet() |
197
|
12 |
|
->children() |
198
|
12 |
|
->append($this->addAuthenticationPluiginNode()) |
199
|
12 |
|
|
200
|
12 |
|
->arrayNode('cache') |
201
|
12 |
|
->canBeEnabled() |
202
|
12 |
|
->addDefaultsIfNotSet() |
203
|
12 |
|
->children() |
204
|
12 |
|
->scalarNode('cache_pool') |
205
|
12 |
|
->info('This must be a service id to a service implementing Psr\Cache\CacheItemPoolInterface') |
206
|
|
|
->isRequired() |
207
|
12 |
|
->cannotBeEmpty() |
208
|
12 |
|
->end() |
209
|
12 |
|
->scalarNode('stream_factory') |
210
|
12 |
|
->info('This must be a service id to a service implementing Http\Message\StreamFactory') |
211
|
12 |
|
->defaultValue('httplug.stream_factory') |
212
|
12 |
|
->cannotBeEmpty() |
213
|
12 |
|
->end() |
214
|
12 |
|
->arrayNode('config') |
215
|
12 |
|
->addDefaultsIfNotSet() |
216
|
12 |
|
->children() |
217
|
|
|
->scalarNode('default_ttl')->defaultNull()->end() |
218
|
12 |
|
->scalarNode('respect_cache_headers')->defaultTrue()->end() |
219
|
12 |
|
->end() |
220
|
12 |
|
->end() |
221
|
12 |
|
->end() |
222
|
12 |
|
->end() // End cache plugin |
223
|
12 |
|
|
224
|
12 |
|
->arrayNode('cookie') |
225
|
|
|
->canBeEnabled() |
226
|
12 |
|
->children() |
227
|
12 |
|
->scalarNode('cookie_jar') |
228
|
12 |
|
->info('This must be a service id to a service implementing Http\Message\CookieJar') |
229
|
12 |
|
->isRequired() |
230
|
12 |
|
->cannotBeEmpty() |
231
|
12 |
|
->end() |
232
|
12 |
|
->end() |
233
|
12 |
|
->end() // End cookie plugin |
234
|
12 |
|
|
235
|
12 |
|
->arrayNode('decoder') |
236
|
|
|
->canBeDisabled() |
237
|
12 |
|
->addDefaultsIfNotSet() |
238
|
12 |
|
->children() |
239
|
12 |
|
->scalarNode('use_content_encoding')->defaultTrue()->end() |
240
|
12 |
|
->end() |
241
|
12 |
|
->end() // End decoder plugin |
242
|
12 |
|
|
243
|
12 |
|
->arrayNode('history') |
244
|
12 |
|
->canBeEnabled() |
245
|
12 |
|
->children() |
246
|
12 |
|
->scalarNode('journal') |
247
|
12 |
|
->info('This must be a service id to a service implementing Http\Client\Plugin\Journal') |
248
|
12 |
|
->isRequired() |
249
|
12 |
|
->cannotBeEmpty() |
250
|
12 |
|
->end() |
251
|
12 |
|
->end() |
252
|
|
|
->end() // End history plugin |
253
|
12 |
|
|
254
|
12 |
|
->arrayNode('logger') |
255
|
12 |
|
->canBeDisabled() |
256
|
12 |
|
->addDefaultsIfNotSet() |
257
|
12 |
|
->children() |
258
|
12 |
|
->scalarNode('logger') |
259
|
12 |
|
->info('This must be a service id to a service implementing Psr\Log\LoggerInterface') |
260
|
12 |
|
->defaultValue('logger') |
261
|
|
|
->cannotBeEmpty() |
262
|
12 |
|
->end() |
263
|
12 |
|
->scalarNode('formatter') |
264
|
12 |
|
->info('This must be a service id to a service implementing Http\Message\Formatter') |
265
|
12 |
|
->defaultNull() |
266
|
12 |
|
->end() |
267
|
12 |
|
->end() |
268
|
12 |
|
->end() // End logger plugin |
269
|
|
|
|
270
|
12 |
|
->arrayNode('redirect') |
271
|
12 |
|
->canBeDisabled() |
272
|
12 |
|
->addDefaultsIfNotSet() |
273
|
12 |
|
->children() |
274
|
12 |
|
->scalarNode('preserve_header')->defaultTrue()->end() |
275
|
12 |
|
->scalarNode('use_default_for_multiple')->defaultTrue()->end() |
276
|
12 |
|
->end() |
277
|
12 |
|
->end() // End redirect plugin |
278
|
12 |
|
|
279
|
12 |
|
->arrayNode('retry') |
280
|
12 |
|
->canBeDisabled() |
281
|
|
|
->addDefaultsIfNotSet() |
282
|
12 |
|
->children() |
283
|
12 |
|
->scalarNode('retry')->defaultValue(1)->end() |
284
|
12 |
|
->end() |
285
|
12 |
|
->end() // End retry plugin |
286
|
|
|
|
287
|
|
|
->arrayNode('stopwatch') |
288
|
|
|
->canBeDisabled() |
289
|
|
|
->addDefaultsIfNotSet() |
290
|
|
|
->children() |
291
|
|
|
->scalarNode('stopwatch') |
292
|
12 |
|
->info('This must be a service id to a service extending Symfony\Component\Stopwatch\Stopwatch') |
293
|
|
|
->defaultValue('debug.stopwatch') |
294
|
12 |
|
->cannotBeEmpty() |
295
|
12 |
|
->end() |
296
|
|
|
->end() |
297
|
12 |
|
->end() // End stopwatch plugin |
298
|
12 |
|
|
299
|
12 |
|
->end() |
300
|
12 |
|
->end() |
301
|
12 |
|
->end(); |
302
|
2 |
|
} |
303
|
2 |
|
|
304
|
1 |
|
/** |
305
|
1 |
|
* Add configuration for authentication plugin. |
306
|
2 |
|
* |
307
|
1 |
|
* @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
308
|
1 |
|
*/ |
309
|
2 |
|
private function addAuthenticationPluiginNode() |
310
|
2 |
|
{ |
311
|
1 |
|
$builder = new TreeBuilder(); |
312
|
1 |
|
$node = $builder->root('authentication'); |
313
|
1 |
|
$node |
314
|
1 |
|
->useAttributeAsKey('name') |
315
|
1 |
|
->prototype('array') |
316
|
|
|
->validate() |
317
|
1 |
|
->always() |
318
|
12 |
|
->then(function ($config) { |
319
|
12 |
|
switch ($config['type']) { |
320
|
12 |
|
case 'basic': |
321
|
12 |
|
$this->validateAuthenticationType(['username', 'password'], $config, 'basic'); |
322
|
12 |
|
break; |
323
|
12 |
|
case 'bearer': |
324
|
12 |
|
$this->validateAuthenticationType(['token'], $config, 'bearer'); |
325
|
12 |
|
break; |
326
|
12 |
|
case 'service': |
327
|
12 |
|
$this->validateAuthenticationType(['service'], $config, 'service'); |
328
|
12 |
|
break; |
329
|
12 |
|
case 'wsse': |
330
|
12 |
|
$this->validateAuthenticationType(['username', 'password'], $config, 'wsse'); |
331
|
12 |
|
break; |
332
|
12 |
|
} |
333
|
|
|
|
334
|
12 |
|
return $config; |
335
|
|
|
}) |
336
|
|
|
->end() |
337
|
|
|
->children() |
338
|
|
|
->enumNode('type') |
339
|
|
|
->values(['basic', 'bearer', 'wsse', 'service']) |
340
|
|
|
->isRequired() |
341
|
|
|
->cannotBeEmpty() |
342
|
|
|
->end() |
343
|
|
|
->scalarNode('username')->end() |
344
|
|
|
->scalarNode('password')->end() |
345
|
|
|
->scalarNode('token')->end() |
346
|
2 |
|
->scalarNode('service')->end() |
347
|
|
|
->end() |
348
|
2 |
|
->end() |
349
|
2 |
|
->end(); // End authentication plugin |
350
|
2 |
|
|
351
|
2 |
|
return $node; |
352
|
|
|
} |
353
|
2 |
|
|
354
|
1 |
|
/** |
355
|
|
|
* Validate that the configuration fragment has the specified keys and none other. |
356
|
|
|
* |
357
|
1 |
|
* @param array $expected Fields that must exist |
358
|
1 |
|
* @param array $actual Actual configuration hashmap |
359
|
1 |
|
* @param string $authName Name of authentication method for error messages |
360
|
1 |
|
* |
361
|
1 |
|
* @throws InvalidConfigurationException If $actual does not have exactly the keys specified in $expected (plus 'type') |
362
|
1 |
|
*/ |
363
|
|
|
private function validateAuthenticationType(array $expected, array $actual, $authName) |
364
|
|
|
{ |
365
|
|
|
unset($actual['type']); |
366
|
|
|
$actual = array_keys($actual); |
367
|
|
|
sort($actual); |
368
|
|
|
sort($expected); |
369
|
|
|
|
370
|
|
|
if ($expected === $actual) { |
371
|
|
|
return; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
throw new InvalidConfigurationException(sprintf( |
375
|
|
|
'Authentication "%s" requires %s but got %s', |
376
|
|
|
$authName, |
377
|
|
|
implode(', ', $expected), |
378
|
|
|
implode(', ', $actual) |
379
|
|
|
)); |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
|