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
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
7 |
|
public function getConfigTreeBuilder() |
26
|
|
|
{ |
27
|
7 |
|
$treeBuilder = new TreeBuilder(); |
28
|
7 |
|
$rootNode = $treeBuilder->root('httplug'); |
29
|
|
|
|
30
|
7 |
|
$this->configureClients($rootNode); |
31
|
7 |
|
$this->configurePlugins($rootNode); |
32
|
|
|
|
33
|
|
|
$rootNode |
34
|
7 |
|
->validate() |
35
|
|
|
->ifTrue(function ($v) { |
36
|
6 |
|
return !empty($v['classes']['client']) |
37
|
6 |
|
|| !empty($v['classes']['message_factory']) |
38
|
3 |
|
|| !empty($v['classes']['uri_factory']) |
39
|
6 |
|
|| !empty($v['classes']['stream_factory']); |
40
|
7 |
|
}) |
41
|
|
|
->then(function ($v) { |
42
|
3 |
|
foreach ($v['classes'] as $key => $class) { |
43
|
3 |
|
if (null !== $class && !class_exists($class)) { |
44
|
1 |
|
throw new InvalidConfigurationException(sprintf( |
45
|
1 |
|
'Class %s specified for httplug.classes.%s does not exist.', |
46
|
1 |
|
$class, |
47
|
|
|
$key |
48
|
1 |
|
)); |
49
|
|
|
} |
50
|
2 |
|
} |
51
|
|
|
|
52
|
2 |
|
return $v; |
53
|
7 |
|
}) |
54
|
7 |
|
->end() |
55
|
7 |
|
->children() |
56
|
7 |
|
->arrayNode('main_alias') |
57
|
7 |
|
->addDefaultsIfNotSet() |
58
|
7 |
|
->info('Configure which service the main alias point to.') |
59
|
7 |
|
->children() |
60
|
7 |
|
->scalarNode('client')->defaultValue('httplug.client.default')->end() |
61
|
7 |
|
->scalarNode('message_factory')->defaultValue('httplug.message_factory.default')->end() |
62
|
7 |
|
->scalarNode('uri_factory')->defaultValue('httplug.uri_factory.default')->end() |
63
|
7 |
|
->scalarNode('stream_factory')->defaultValue('httplug.stream_factory.default')->end() |
64
|
7 |
|
->end() |
65
|
7 |
|
->end() |
66
|
7 |
|
->arrayNode('classes') |
67
|
7 |
|
->addDefaultsIfNotSet() |
68
|
7 |
|
->info('Overwrite a service class instead of using the discovery mechanism.') |
69
|
7 |
|
->children() |
70
|
7 |
|
->scalarNode('client')->defaultNull()->end() |
71
|
7 |
|
->scalarNode('message_factory')->defaultNull()->end() |
72
|
7 |
|
->scalarNode('uri_factory')->defaultNull()->end() |
73
|
7 |
|
->scalarNode('stream_factory')->defaultNull()->end() |
74
|
7 |
|
->end() |
75
|
7 |
|
->end() |
76
|
7 |
|
->arrayNode('toolbar') |
77
|
7 |
|
->addDefaultsIfNotSet() |
78
|
7 |
|
->info('Extend the debug profiler with inforation about requests.') |
79
|
7 |
|
->children() |
80
|
7 |
|
->enumNode('enabled') |
81
|
7 |
|
->info('If "auto" (default), the toolbar is activated when kernel.debug is true. You can force the toolbar on and off by changing this option.') |
82
|
7 |
|
->values([true, false, 'auto']) |
83
|
7 |
|
->defaultValue('auto') |
84
|
7 |
|
->end() |
85
|
7 |
|
->scalarNode('formatter')->defaultNull()->end() |
86
|
7 |
|
->end() |
87
|
7 |
|
->end() |
88
|
7 |
|
->end(); |
89
|
|
|
|
90
|
7 |
|
return $treeBuilder; |
91
|
|
|
} |
92
|
|
|
|
93
|
7 |
|
protected function configureClients(ArrayNodeDefinition $root) |
94
|
|
|
{ |
95
|
7 |
|
$root->children() |
96
|
7 |
|
->arrayNode('clients') |
97
|
7 |
|
->validate() |
98
|
|
|
->ifTrue(function ($clients) { |
99
|
|
|
foreach ($clients as $name => $config) { |
100
|
|
|
return $config['flexible_client'] && $config['http_methods_client']; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return false; |
104
|
7 |
|
}) |
105
|
7 |
|
->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() |
106
|
7 |
|
->useAttributeAsKey('name') |
107
|
7 |
|
->prototype('array') |
108
|
7 |
|
->children() |
109
|
7 |
|
->scalarNode('factory') |
110
|
7 |
|
->isRequired() |
111
|
7 |
|
->cannotBeEmpty() |
112
|
7 |
|
->info('The service id of a factory to use when creating the adapter.') |
113
|
7 |
|
->end() |
114
|
7 |
|
->booleanNode('flexible_client') |
115
|
7 |
|
->defaultFalse() |
116
|
7 |
|
->info('Set to true to get the client wrapped in a FlexibleHttpClient which emulates async or sync behavior.') |
117
|
7 |
|
->end() |
118
|
7 |
|
->booleanNode('http_methods_client') |
119
|
7 |
|
->defaultFalse() |
120
|
7 |
|
->info('Set to true to get the client wrapped in a HttpMethodsClient which emulates provides functions for HTTP verbs.') |
121
|
7 |
|
->end() |
122
|
7 |
|
->arrayNode('plugins') |
123
|
7 |
|
->info('A list of service ids of plugins. The order is important.') |
124
|
7 |
|
->prototype('scalar')->end() |
125
|
7 |
|
->end() |
126
|
7 |
|
->variableNode('config')->defaultValue([])->end() |
127
|
7 |
|
->end() |
128
|
7 |
|
->end(); |
129
|
7 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param ArrayNodeDefinition $root |
133
|
|
|
*/ |
134
|
7 |
|
protected function configurePlugins(ArrayNodeDefinition $root) |
135
|
|
|
{ |
136
|
7 |
|
$root->children() |
137
|
7 |
|
->arrayNode('plugins') |
138
|
7 |
|
->addDefaultsIfNotSet() |
139
|
7 |
|
->children() |
140
|
7 |
|
->append($this->addAuthenticationPluiginNode()) |
141
|
|
|
|
142
|
7 |
|
->arrayNode('cache') |
143
|
7 |
|
->canBeEnabled() |
144
|
7 |
|
->addDefaultsIfNotSet() |
145
|
7 |
|
->children() |
146
|
7 |
|
->scalarNode('cache_pool') |
147
|
7 |
|
->info('This must be a service id to a service implementing Psr\Cache\CacheItemPoolInterface') |
148
|
7 |
|
->isRequired() |
149
|
7 |
|
->cannotBeEmpty() |
150
|
7 |
|
->end() |
151
|
7 |
|
->scalarNode('stream_factory') |
152
|
7 |
|
->info('This must be a service id to a service implementing Http\Message\StreamFactory') |
153
|
7 |
|
->defaultValue('httplug.stream_factory') |
154
|
7 |
|
->cannotBeEmpty() |
155
|
7 |
|
->end() |
156
|
7 |
|
->arrayNode('config') |
157
|
7 |
|
->addDefaultsIfNotSet() |
158
|
7 |
|
->children() |
159
|
7 |
|
->scalarNode('default_ttl')->defaultNull()->end() |
160
|
7 |
|
->scalarNode('respect_cache_headers')->defaultTrue()->end() |
161
|
7 |
|
->end() |
162
|
7 |
|
->end() |
163
|
7 |
|
->end() |
164
|
7 |
|
->end() // End cache plugin |
165
|
|
|
|
166
|
7 |
|
->arrayNode('cookie') |
167
|
7 |
|
->canBeEnabled() |
168
|
7 |
|
->children() |
169
|
7 |
|
->scalarNode('cookie_jar') |
170
|
7 |
|
->info('This must be a service id to a service implementing Http\Message\CookieJar') |
171
|
7 |
|
->isRequired() |
172
|
7 |
|
->cannotBeEmpty() |
173
|
7 |
|
->end() |
174
|
7 |
|
->end() |
175
|
7 |
|
->end() // End cookie plugin |
176
|
|
|
|
177
|
7 |
|
->arrayNode('decoder') |
178
|
7 |
|
->canBeDisabled() |
179
|
7 |
|
->addDefaultsIfNotSet() |
180
|
7 |
|
->children() |
181
|
7 |
|
->scalarNode('use_content_encoding')->defaultTrue()->end() |
182
|
7 |
|
->end() |
183
|
7 |
|
->end() // End decoder plugin |
184
|
|
|
|
185
|
7 |
|
->arrayNode('history') |
186
|
7 |
|
->canBeEnabled() |
187
|
7 |
|
->children() |
188
|
7 |
|
->scalarNode('journal') |
189
|
7 |
|
->info('This must be a service id to a service implementing Http\Client\Plugin\Journal') |
190
|
7 |
|
->isRequired() |
191
|
7 |
|
->cannotBeEmpty() |
192
|
7 |
|
->end() |
193
|
7 |
|
->end() |
194
|
7 |
|
->end() // End history plugin |
195
|
|
|
|
196
|
7 |
|
->arrayNode('logger') |
197
|
7 |
|
->canBeDisabled() |
198
|
7 |
|
->addDefaultsIfNotSet() |
199
|
7 |
|
->children() |
200
|
7 |
|
->scalarNode('logger') |
201
|
7 |
|
->info('This must be a service id to a service implementing Psr\Log\LoggerInterface') |
202
|
7 |
|
->defaultValue('logger') |
203
|
7 |
|
->cannotBeEmpty() |
204
|
7 |
|
->end() |
205
|
7 |
|
->scalarNode('formatter') |
206
|
7 |
|
->info('This must be a service id to a service implementing Http\Message\Formatter') |
207
|
7 |
|
->defaultNull() |
208
|
7 |
|
->end() |
209
|
7 |
|
->end() |
210
|
7 |
|
->end() // End logger plugin |
211
|
|
|
|
212
|
7 |
|
->arrayNode('redirect') |
213
|
7 |
|
->canBeDisabled() |
214
|
7 |
|
->addDefaultsIfNotSet() |
215
|
7 |
|
->children() |
216
|
7 |
|
->scalarNode('preserve_header')->defaultTrue()->end() |
217
|
7 |
|
->scalarNode('use_default_for_multiple')->defaultTrue()->end() |
218
|
7 |
|
->end() |
219
|
7 |
|
->end() // End redirect plugin |
220
|
|
|
|
221
|
7 |
|
->arrayNode('retry') |
222
|
7 |
|
->canBeDisabled() |
223
|
7 |
|
->addDefaultsIfNotSet() |
224
|
7 |
|
->children() |
225
|
7 |
|
->scalarNode('retry')->defaultValue(1)->end() |
226
|
7 |
|
->end() |
227
|
7 |
|
->end() // End retry plugin |
228
|
|
|
|
229
|
7 |
|
->arrayNode('stopwatch') |
230
|
7 |
|
->canBeDisabled() |
231
|
7 |
|
->addDefaultsIfNotSet() |
232
|
7 |
|
->children() |
233
|
7 |
|
->scalarNode('stopwatch') |
234
|
7 |
|
->info('This must be a service id to a service extending Symfony\Component\Stopwatch\Stopwatch') |
235
|
7 |
|
->defaultValue('debug.stopwatch') |
236
|
7 |
|
->cannotBeEmpty() |
237
|
7 |
|
->end() |
238
|
7 |
|
->end() |
239
|
7 |
|
->end() // End stopwatch plugin |
240
|
|
|
|
241
|
7 |
|
->end() |
242
|
7 |
|
->end() |
243
|
7 |
|
->end(); |
244
|
7 |
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Add configuration for authentication plugin. |
248
|
|
|
* |
249
|
|
|
* @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
250
|
|
|
*/ |
251
|
7 |
|
private function addAuthenticationPluiginNode() |
252
|
|
|
{ |
253
|
7 |
|
$builder = new TreeBuilder(); |
254
|
7 |
|
$node = $builder->root('authentication'); |
255
|
|
|
$node |
256
|
7 |
|
->useAttributeAsKey('name') |
257
|
7 |
|
->prototype('array') |
258
|
7 |
|
->validate() |
259
|
7 |
|
->always() |
260
|
7 |
|
->then(function ($config) { |
261
|
2 |
|
switch ($config['type']) { |
262
|
2 |
|
case 'basic': |
263
|
1 |
|
$this->validateAuthenticationType(['username', 'password'], $config, 'basic'); |
264
|
1 |
|
break; |
265
|
2 |
|
case 'bearer': |
266
|
1 |
|
$this->validateAuthenticationType(['token'], $config, 'bearer'); |
267
|
1 |
|
break; |
268
|
2 |
|
case 'service': |
269
|
2 |
|
$this->validateAuthenticationType(['service'], $config, 'service'); |
270
|
1 |
|
break; |
271
|
1 |
|
case 'wsse': |
272
|
1 |
|
$this->validateAuthenticationType(['username', 'password'], $config, 'wsse'); |
273
|
1 |
|
break; |
274
|
1 |
|
} |
275
|
|
|
|
276
|
1 |
|
return $config; |
277
|
7 |
|
}) |
278
|
7 |
|
->end() |
279
|
7 |
|
->children() |
280
|
7 |
|
->enumNode('type') |
281
|
7 |
|
->values(['basic', 'bearer', 'wsse', 'service']) |
282
|
7 |
|
->isRequired() |
283
|
7 |
|
->cannotBeEmpty() |
284
|
7 |
|
->end() |
285
|
7 |
|
->scalarNode('username')->end() |
286
|
7 |
|
->scalarNode('password')->end() |
287
|
7 |
|
->scalarNode('token')->end() |
288
|
7 |
|
->scalarNode('service')->end() |
289
|
7 |
|
->end() |
290
|
7 |
|
->end() |
291
|
7 |
|
->end(); // End authentication plugin |
292
|
|
|
|
293
|
7 |
|
return $node; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Validate that the configuration fragment has the specified keys and none other. |
298
|
|
|
* |
299
|
|
|
* @param array $expected Fields that must exist |
300
|
|
|
* @param array $actual Actual configuration hashmap |
301
|
|
|
* @param string $authName Name of authentication method for error messages |
302
|
|
|
* |
303
|
|
|
* @throws InvalidConfigurationException If $actual does not have exactly the keys specified in $expected (plus 'type') |
304
|
|
|
*/ |
305
|
2 |
|
private function validateAuthenticationType(array $expected, array $actual, $authName) |
306
|
|
|
{ |
307
|
2 |
|
unset($actual['type']); |
308
|
2 |
|
$actual = array_keys($actual); |
309
|
2 |
|
sort($actual); |
310
|
2 |
|
sort($expected); |
311
|
|
|
|
312
|
2 |
|
if ($expected === $actual) { |
313
|
1 |
|
return; |
314
|
|
|
} |
315
|
|
|
|
316
|
1 |
|
throw new InvalidConfigurationException(sprintf( |
317
|
1 |
|
'Authentication "%s" requires %s but got %s', |
318
|
1 |
|
$authName, |
319
|
1 |
|
implode(', ', $expected), |
320
|
1 |
|
implode(', ', $actual) |
321
|
1 |
|
)); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|