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 |
|
->children() |
73
|
12 |
|
->arrayNode('main_alias') |
74
|
12 |
|
->addDefaultsIfNotSet() |
75
|
12 |
|
->info('Configure which service the main alias point to.') |
76
|
12 |
|
->children() |
77
|
12 |
|
->scalarNode('client')->defaultValue('httplug.client.default')->end() |
78
|
12 |
|
->scalarNode('message_factory')->defaultValue('httplug.message_factory.default')->end() |
79
|
12 |
|
->scalarNode('uri_factory')->defaultValue('httplug.uri_factory.default')->end() |
80
|
12 |
|
->scalarNode('stream_factory')->defaultValue('httplug.stream_factory.default')->end() |
81
|
12 |
|
->end() |
82
|
12 |
|
->end() |
83
|
12 |
|
->arrayNode('classes') |
84
|
12 |
|
->addDefaultsIfNotSet() |
85
|
12 |
|
->info('Overwrite a service class instead of using the discovery mechanism.') |
86
|
12 |
|
->children() |
87
|
12 |
|
->scalarNode('client')->defaultNull()->end() |
88
|
12 |
|
->scalarNode('message_factory')->defaultNull()->end() |
89
|
12 |
|
->scalarNode('uri_factory')->defaultNull()->end() |
90
|
12 |
|
->scalarNode('stream_factory')->defaultNull()->end() |
91
|
12 |
|
->end() |
92
|
12 |
|
->end() |
93
|
12 |
|
->arrayNode('toolbar') |
94
|
12 |
|
->addDefaultsIfNotSet() |
95
|
12 |
|
->info('Extend the debug profiler with information about requests.') |
96
|
12 |
|
->children() |
97
|
12 |
|
->booleanNode('enabled') // @deprecated value auto in 1.3.0 |
98
|
12 |
|
->beforeNormalization() |
99
|
12 |
|
->ifString() |
100
|
|
|
->then(function ($v) { |
101
|
1 |
|
return 'auto' === $v ? $this->debug : $v; |
102
|
12 |
|
}) |
103
|
12 |
|
->end() |
104
|
12 |
|
->info('Turn the toolbar on or off. Defaults to kernel debug mode.') |
105
|
12 |
|
->defaultValue($this->debug) |
106
|
12 |
|
->end() |
107
|
12 |
|
->scalarNode('formatter')->defaultNull()->end() |
108
|
12 |
|
->scalarNode('captured_body_length') |
109
|
12 |
|
->defaultValue(0) |
110
|
12 |
|
->canNotBeEmpty() |
111
|
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).') |
112
|
12 |
|
->end() |
113
|
12 |
|
->end() |
114
|
12 |
|
->end() |
115
|
12 |
|
->arrayNode('discovery') |
116
|
12 |
|
->addDefaultsIfNotSet() |
117
|
12 |
|
->info('Control what clients should be found by the discovery.') |
118
|
12 |
|
->children() |
119
|
12 |
|
->scalarNode('client') |
120
|
12 |
|
->defaultValue('auto') |
121
|
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.') |
122
|
12 |
|
->end() |
123
|
12 |
|
->scalarNode('async_client') |
124
|
12 |
|
->defaultNull() |
125
|
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.') |
126
|
12 |
|
->end() |
127
|
12 |
|
->end() |
128
|
12 |
|
->end() |
129
|
12 |
|
->end(); |
130
|
|
|
|
131
|
12 |
|
return $treeBuilder; |
132
|
|
|
} |
133
|
|
|
|
134
|
12 |
|
protected function configureClients(ArrayNodeDefinition $root) |
135
|
|
|
{ |
136
|
12 |
|
$root->children() |
137
|
12 |
|
->arrayNode('clients') |
138
|
12 |
|
->validate() |
139
|
|
|
->ifTrue(function ($clients) { |
140
|
3 |
|
foreach ($clients as $name => $config) { |
141
|
3 |
|
return $config['flexible_client'] && $config['http_methods_client']; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return false; |
145
|
12 |
|
}) |
146
|
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() |
147
|
12 |
|
->useAttributeAsKey('name') |
148
|
12 |
|
->prototype('array') |
149
|
12 |
|
->children() |
150
|
12 |
|
->scalarNode('factory') |
151
|
12 |
|
->isRequired() |
152
|
12 |
|
->cannotBeEmpty() |
153
|
12 |
|
->info('The service id of a factory to use when creating the adapter.') |
154
|
12 |
|
->end() |
155
|
12 |
|
->booleanNode('flexible_client') |
156
|
12 |
|
->defaultFalse() |
157
|
12 |
|
->info('Set to true to get the client wrapped in a FlexibleHttpClient which emulates async or sync behavior.') |
158
|
12 |
|
->end() |
159
|
12 |
|
->booleanNode('http_methods_client') |
160
|
12 |
|
->defaultFalse() |
161
|
12 |
|
->info('Set to true to get the client wrapped in a HttpMethodsClient which emulates provides functions for HTTP verbs.') |
162
|
12 |
|
->end() |
163
|
12 |
|
->arrayNode('plugins') |
164
|
12 |
|
->info('A list of service ids of plugins. The order is important.') |
165
|
12 |
|
->prototype('scalar')->end() |
166
|
12 |
|
->end() |
167
|
12 |
|
->variableNode('config')->defaultValue([])->end() |
168
|
12 |
|
->end() |
169
|
12 |
|
->end(); |
170
|
12 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param ArrayNodeDefinition $root |
174
|
|
|
*/ |
175
|
12 |
|
protected function configurePlugins(ArrayNodeDefinition $root) |
176
|
|
|
{ |
177
|
12 |
|
$root->children() |
178
|
12 |
|
->arrayNode('plugins') |
179
|
12 |
|
->addDefaultsIfNotSet() |
180
|
12 |
|
->children() |
181
|
12 |
|
->append($this->addAuthenticationPluiginNode()) |
182
|
|
|
|
183
|
12 |
|
->arrayNode('cache') |
184
|
12 |
|
->canBeEnabled() |
185
|
12 |
|
->addDefaultsIfNotSet() |
186
|
12 |
|
->children() |
187
|
12 |
|
->scalarNode('cache_pool') |
188
|
12 |
|
->info('This must be a service id to a service implementing Psr\Cache\CacheItemPoolInterface') |
189
|
12 |
|
->isRequired() |
190
|
12 |
|
->cannotBeEmpty() |
191
|
12 |
|
->end() |
192
|
12 |
|
->scalarNode('stream_factory') |
193
|
12 |
|
->info('This must be a service id to a service implementing Http\Message\StreamFactory') |
194
|
12 |
|
->defaultValue('httplug.stream_factory') |
195
|
12 |
|
->cannotBeEmpty() |
196
|
12 |
|
->end() |
197
|
12 |
|
->arrayNode('config') |
198
|
12 |
|
->addDefaultsIfNotSet() |
199
|
12 |
|
->children() |
200
|
12 |
|
->scalarNode('default_ttl')->defaultNull()->end() |
201
|
12 |
|
->scalarNode('respect_cache_headers')->defaultTrue()->end() |
202
|
12 |
|
->end() |
203
|
12 |
|
->end() |
204
|
12 |
|
->end() |
205
|
12 |
|
->end() // End cache plugin |
206
|
|
|
|
207
|
12 |
|
->arrayNode('cookie') |
208
|
12 |
|
->canBeEnabled() |
209
|
12 |
|
->children() |
210
|
12 |
|
->scalarNode('cookie_jar') |
211
|
12 |
|
->info('This must be a service id to a service implementing Http\Message\CookieJar') |
212
|
12 |
|
->isRequired() |
213
|
12 |
|
->cannotBeEmpty() |
214
|
12 |
|
->end() |
215
|
12 |
|
->end() |
216
|
12 |
|
->end() // End cookie plugin |
217
|
|
|
|
218
|
12 |
|
->arrayNode('decoder') |
219
|
12 |
|
->canBeDisabled() |
220
|
12 |
|
->addDefaultsIfNotSet() |
221
|
12 |
|
->children() |
222
|
12 |
|
->scalarNode('use_content_encoding')->defaultTrue()->end() |
223
|
12 |
|
->end() |
224
|
12 |
|
->end() // End decoder plugin |
225
|
|
|
|
226
|
12 |
|
->arrayNode('history') |
227
|
12 |
|
->canBeEnabled() |
228
|
12 |
|
->children() |
229
|
12 |
|
->scalarNode('journal') |
230
|
12 |
|
->info('This must be a service id to a service implementing Http\Client\Plugin\Journal') |
231
|
12 |
|
->isRequired() |
232
|
12 |
|
->cannotBeEmpty() |
233
|
12 |
|
->end() |
234
|
12 |
|
->end() |
235
|
12 |
|
->end() // End history plugin |
236
|
|
|
|
237
|
12 |
|
->arrayNode('logger') |
238
|
12 |
|
->canBeDisabled() |
239
|
12 |
|
->addDefaultsIfNotSet() |
240
|
12 |
|
->children() |
241
|
12 |
|
->scalarNode('logger') |
242
|
12 |
|
->info('This must be a service id to a service implementing Psr\Log\LoggerInterface') |
243
|
12 |
|
->defaultValue('logger') |
244
|
12 |
|
->cannotBeEmpty() |
245
|
12 |
|
->end() |
246
|
12 |
|
->scalarNode('formatter') |
247
|
12 |
|
->info('This must be a service id to a service implementing Http\Message\Formatter') |
248
|
12 |
|
->defaultNull() |
249
|
12 |
|
->end() |
250
|
12 |
|
->end() |
251
|
12 |
|
->end() // End logger plugin |
252
|
|
|
|
253
|
12 |
|
->arrayNode('redirect') |
254
|
12 |
|
->canBeDisabled() |
255
|
12 |
|
->addDefaultsIfNotSet() |
256
|
12 |
|
->children() |
257
|
12 |
|
->scalarNode('preserve_header')->defaultTrue()->end() |
258
|
12 |
|
->scalarNode('use_default_for_multiple')->defaultTrue()->end() |
259
|
12 |
|
->end() |
260
|
12 |
|
->end() // End redirect plugin |
261
|
|
|
|
262
|
12 |
|
->arrayNode('retry') |
263
|
12 |
|
->canBeDisabled() |
264
|
12 |
|
->addDefaultsIfNotSet() |
265
|
12 |
|
->children() |
266
|
12 |
|
->scalarNode('retry')->defaultValue(1)->end() |
267
|
12 |
|
->end() |
268
|
12 |
|
->end() // End retry plugin |
269
|
|
|
|
270
|
12 |
|
->arrayNode('stopwatch') |
271
|
12 |
|
->canBeDisabled() |
272
|
12 |
|
->addDefaultsIfNotSet() |
273
|
12 |
|
->children() |
274
|
12 |
|
->scalarNode('stopwatch') |
275
|
12 |
|
->info('This must be a service id to a service extending Symfony\Component\Stopwatch\Stopwatch') |
276
|
12 |
|
->defaultValue('debug.stopwatch') |
277
|
12 |
|
->cannotBeEmpty() |
278
|
12 |
|
->end() |
279
|
12 |
|
->end() |
280
|
12 |
|
->end() // End stopwatch plugin |
281
|
|
|
|
282
|
12 |
|
->end() |
283
|
12 |
|
->end() |
284
|
12 |
|
->end(); |
285
|
12 |
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Add configuration for authentication plugin. |
289
|
|
|
* |
290
|
|
|
* @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
291
|
|
|
*/ |
292
|
12 |
|
private function addAuthenticationPluiginNode() |
293
|
|
|
{ |
294
|
12 |
|
$builder = new TreeBuilder(); |
295
|
12 |
|
$node = $builder->root('authentication'); |
296
|
|
|
$node |
297
|
12 |
|
->useAttributeAsKey('name') |
298
|
12 |
|
->prototype('array') |
299
|
12 |
|
->validate() |
300
|
12 |
|
->always() |
301
|
12 |
|
->then(function ($config) { |
302
|
2 |
|
switch ($config['type']) { |
303
|
2 |
|
case 'basic': |
304
|
1 |
|
$this->validateAuthenticationType(['username', 'password'], $config, 'basic'); |
305
|
1 |
|
break; |
306
|
2 |
|
case 'bearer': |
307
|
1 |
|
$this->validateAuthenticationType(['token'], $config, 'bearer'); |
308
|
1 |
|
break; |
309
|
2 |
|
case 'service': |
310
|
2 |
|
$this->validateAuthenticationType(['service'], $config, 'service'); |
311
|
1 |
|
break; |
312
|
1 |
|
case 'wsse': |
313
|
1 |
|
$this->validateAuthenticationType(['username', 'password'], $config, 'wsse'); |
314
|
1 |
|
break; |
315
|
1 |
|
} |
316
|
|
|
|
317
|
1 |
|
return $config; |
318
|
12 |
|
}) |
319
|
12 |
|
->end() |
320
|
12 |
|
->children() |
321
|
12 |
|
->enumNode('type') |
322
|
12 |
|
->values(['basic', 'bearer', 'wsse', 'service']) |
323
|
12 |
|
->isRequired() |
324
|
12 |
|
->cannotBeEmpty() |
325
|
12 |
|
->end() |
326
|
12 |
|
->scalarNode('username')->end() |
327
|
12 |
|
->scalarNode('password')->end() |
328
|
12 |
|
->scalarNode('token')->end() |
329
|
12 |
|
->scalarNode('service')->end() |
330
|
12 |
|
->end() |
331
|
12 |
|
->end() |
332
|
12 |
|
->end(); // End authentication plugin |
333
|
|
|
|
334
|
12 |
|
return $node; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Validate that the configuration fragment has the specified keys and none other. |
339
|
|
|
* |
340
|
|
|
* @param array $expected Fields that must exist |
341
|
|
|
* @param array $actual Actual configuration hashmap |
342
|
|
|
* @param string $authName Name of authentication method for error messages |
343
|
|
|
* |
344
|
|
|
* @throws InvalidConfigurationException If $actual does not have exactly the keys specified in $expected (plus 'type') |
345
|
|
|
*/ |
346
|
2 |
|
private function validateAuthenticationType(array $expected, array $actual, $authName) |
347
|
|
|
{ |
348
|
2 |
|
unset($actual['type']); |
349
|
2 |
|
$actual = array_keys($actual); |
350
|
2 |
|
sort($actual); |
351
|
2 |
|
sort($expected); |
352
|
|
|
|
353
|
2 |
|
if ($expected === $actual) { |
354
|
1 |
|
return; |
355
|
|
|
} |
356
|
|
|
|
357
|
1 |
|
throw new InvalidConfigurationException(sprintf( |
358
|
1 |
|
'Authentication "%s" requires %s but got %s', |
359
|
1 |
|
$authName, |
360
|
1 |
|
implode(', ', $expected), |
361
|
1 |
|
implode(', ', $actual) |
362
|
1 |
|
)); |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|