|
4 | 4 |
|
5 | 5 | use Symfony\Component\Config\Definition\ArrayNode;
|
6 | 6 | use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
|
| 7 | +use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
7 | 8 | use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
8 | 9 | use Symfony\Component\Config\Definition\ConfigurationInterface;
|
9 | 10 | use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
|
@@ -92,6 +93,7 @@ public function getConfigTreeBuilder()
|
92 | 93 | return $v;
|
93 | 94 | })
|
94 | 95 | ->end()
|
| 96 | + ->fixXmlConfig('client') |
95 | 97 | ->children()
|
96 | 98 | ->arrayNode('main_alias')
|
97 | 99 | ->addDefaultsIfNotSet()
|
@@ -150,7 +152,7 @@ public function getConfigTreeBuilder()
|
150 | 152 | return $treeBuilder;
|
151 | 153 | }
|
152 | 154 |
|
153 |
| - protected function configureClients(ArrayNodeDefinition $root) |
| 155 | + private function configureClients(ArrayNodeDefinition $root) |
154 | 156 | {
|
155 | 157 | $root->children()
|
156 | 158 | ->arrayNode('clients')
|
@@ -189,131 +191,215 @@ protected function configureClients(ArrayNodeDefinition $root)
|
189 | 191 | ->prototype('scalar')->end()
|
190 | 192 | ->end()
|
191 | 193 | ->variableNode('config')->defaultValue([])->end()
|
| 194 | + ->append($this->createExtraPluginsNode()) |
192 | 195 | ->end()
|
193 | 196 | ->end();
|
194 | 197 | }
|
195 | 198 |
|
196 | 199 | /**
|
197 | 200 | * @param ArrayNodeDefinition $root
|
198 | 201 | */
|
199 |
| - protected function configurePlugins(ArrayNodeDefinition $root) |
| 202 | + private function configurePlugins(ArrayNodeDefinition $root) |
200 | 203 | {
|
201 |
| - $root->children() |
202 |
| - ->arrayNode('plugins') |
| 204 | + $pluginsNode = $root |
| 205 | + ->children() |
| 206 | + ->arrayNode('plugins') |
203 | 207 | ->addDefaultsIfNotSet()
|
204 |
| - ->children() |
205 |
| - ->append($this->addAuthenticationPluiginNode()) |
| 208 | + ; |
| 209 | + $this->configureSharedPluginNodes($pluginsNode); |
| 210 | + } |
206 | 211 |
|
207 |
| - ->arrayNode('cache') |
| 212 | + /** |
| 213 | + * Create configuration for the extra_plugins node inside the client. |
| 214 | + * |
| 215 | + * @return NodeDefinition Definition of the extra_plugins node in the client. |
| 216 | + */ |
| 217 | + private function createExtraPluginsNode() |
| 218 | + { |
| 219 | + $builder = new TreeBuilder(); |
| 220 | + $node = $builder->root('extra_plugins'); |
| 221 | + $node->validate() |
| 222 | + ->always(function ($plugins) { |
| 223 | + if (!count($plugins['authentication'])) { |
| 224 | + unset($plugins['authentication']); |
| 225 | + } |
| 226 | + foreach ($plugins as $name => $definition) { |
| 227 | + if (!$definition['enabled']) { |
| 228 | + unset($plugins[$name]); |
| 229 | + } |
| 230 | + } |
| 231 | + return $plugins; |
| 232 | + }) |
| 233 | + ; |
| 234 | + $this->configureSharedPluginNodes($node, true); |
| 235 | + $node |
| 236 | + ->children() |
| 237 | + ->arrayNode('add_host') |
208 | 238 | ->canBeEnabled()
|
209 | 239 | ->addDefaultsIfNotSet()
|
210 |
| - ->children() |
211 |
| - ->scalarNode('cache_pool') |
212 |
| - ->info('This must be a service id to a service implementing Psr\Cache\CacheItemPoolInterface') |
213 |
| - ->isRequired() |
214 |
| - ->cannotBeEmpty() |
215 |
| - ->end() |
216 |
| - ->scalarNode('stream_factory') |
217 |
| - ->info('This must be a service id to a service implementing Http\Message\StreamFactory') |
218 |
| - ->defaultValue('httplug.stream_factory') |
219 |
| - ->cannotBeEmpty() |
220 |
| - ->end() |
221 |
| - ->arrayNode('config') |
222 |
| - ->addDefaultsIfNotSet() |
223 |
| - ->children() |
224 |
| - ->scalarNode('default_ttl')->defaultNull()->end() |
225 |
| - ->scalarNode('respect_cache_headers')->defaultTrue()->end() |
226 |
| - ->end() |
227 |
| - ->end() |
| 240 | + ->info('Configure the AddHostPlugin for this client.') |
| 241 | + ->children() |
| 242 | + ->scalarNode('host') |
| 243 | + ->info('Host name including protocol and optionally the port number, e.g. https://api.local:8000') |
| 244 | + ->isRequired() |
| 245 | + ->cannotBeEmpty() |
228 | 246 | ->end()
|
229 |
| - ->end() // End cache plugin |
230 |
| - |
231 |
| - ->arrayNode('cookie') |
232 |
| - ->canBeEnabled() |
233 |
| - ->children() |
234 |
| - ->scalarNode('cookie_jar') |
235 |
| - ->info('This must be a service id to a service implementing Http\Message\CookieJar') |
236 |
| - ->isRequired() |
237 |
| - ->cannotBeEmpty() |
238 |
| - ->end() |
| 247 | + ->scalarNode('replace') |
| 248 | + ->info('Whether to replace the host if request already specifies it') |
| 249 | + ->defaultValue(false) |
239 | 250 | ->end()
|
240 |
| - ->end() // End cookie plugin |
| 251 | + ->end() |
| 252 | + ->end() |
| 253 | + ->end() |
| 254 | + ->end(); |
241 | 255 |
|
242 |
| - ->arrayNode('decoder') |
243 |
| - ->canBeDisabled() |
244 |
| - ->addDefaultsIfNotSet() |
245 |
| - ->children() |
246 |
| - ->scalarNode('use_content_encoding')->defaultTrue()->end() |
247 |
| - ->end() |
248 |
| - ->end() // End decoder plugin |
| 256 | + return $node; |
| 257 | + } |
249 | 258 |
|
250 |
| - ->arrayNode('history') |
251 |
| - ->canBeEnabled() |
252 |
| - ->children() |
253 |
| - ->scalarNode('journal') |
254 |
| - ->info('This must be a service id to a service implementing Http\Client\Plugin\Journal') |
255 |
| - ->isRequired() |
256 |
| - ->cannotBeEmpty() |
257 |
| - ->end() |
258 |
| - ->end() |
259 |
| - ->end() // End history plugin |
| 259 | + /** |
| 260 | + * @param ArrayNodeDefinition $pluginNode |
| 261 | + * @param bool $disableAll Some shared plugins are enabled by default. On the client, all are disabled by default. |
| 262 | + */ |
| 263 | + private function configureSharedPluginNodes(ArrayNodeDefinition $pluginNode, $disableAll = false) |
| 264 | + { |
| 265 | + $children = $pluginNode->children(); |
260 | 266 |
|
261 |
| - ->arrayNode('logger') |
262 |
| - ->canBeDisabled() |
263 |
| - ->addDefaultsIfNotSet() |
264 |
| - ->children() |
265 |
| - ->scalarNode('logger') |
266 |
| - ->info('This must be a service id to a service implementing Psr\Log\LoggerInterface') |
267 |
| - ->defaultValue('logger') |
268 |
| - ->cannotBeEmpty() |
269 |
| - ->end() |
270 |
| - ->scalarNode('formatter') |
271 |
| - ->info('This must be a service id to a service implementing Http\Message\Formatter') |
272 |
| - ->defaultNull() |
273 |
| - ->end() |
274 |
| - ->end() |
275 |
| - ->end() // End logger plugin |
| 267 | + $children->append($this->createAuthenticationPluginNode()); |
276 | 268 |
|
277 |
| - ->arrayNode('redirect') |
278 |
| - ->canBeDisabled() |
279 |
| - ->addDefaultsIfNotSet() |
| 269 | + $children->arrayNode('cache') |
| 270 | + ->canBeEnabled() |
| 271 | + ->addDefaultsIfNotSet() |
| 272 | + ->children() |
| 273 | + ->scalarNode('cache_pool') |
| 274 | + ->info('This must be a service id to a service implementing Psr\Cache\CacheItemPoolInterface') |
| 275 | + ->isRequired() |
| 276 | + ->cannotBeEmpty() |
| 277 | + ->end() |
| 278 | + ->scalarNode('stream_factory') |
| 279 | + ->info('This must be a service id to a service implementing Http\Message\StreamFactory') |
| 280 | + ->defaultValue('httplug.stream_factory') |
| 281 | + ->cannotBeEmpty() |
| 282 | + ->end() |
| 283 | + ->arrayNode('config') |
| 284 | + ->addDefaultsIfNotSet() |
280 | 285 | ->children()
|
281 |
| - ->scalarNode('preserve_header')->defaultTrue()->end() |
282 |
| - ->scalarNode('use_default_for_multiple')->defaultTrue()->end() |
| 286 | + ->scalarNode('default_ttl')->defaultNull()->end() |
| 287 | + ->scalarNode('respect_cache_headers')->defaultTrue()->end() |
283 | 288 | ->end()
|
284 |
| - ->end() // End redirect plugin |
| 289 | + ->end() |
| 290 | + ->end() |
| 291 | + ->end(); |
| 292 | + // End cache plugin |
285 | 293 |
|
286 |
| - ->arrayNode('retry') |
287 |
| - ->canBeDisabled() |
288 |
| - ->addDefaultsIfNotSet() |
289 |
| - ->children() |
290 |
| - ->scalarNode('retry')->defaultValue(1)->end() |
291 |
| - ->end() |
292 |
| - ->end() // End retry plugin |
| 294 | + $children->arrayNode('cookie') |
| 295 | + ->canBeEnabled() |
| 296 | + ->children() |
| 297 | + ->scalarNode('cookie_jar') |
| 298 | + ->info('This must be a service id to a service implementing Http\Message\CookieJar') |
| 299 | + ->isRequired() |
| 300 | + ->cannotBeEmpty() |
| 301 | + ->end() |
| 302 | + ->end() |
| 303 | + ->end(); |
| 304 | + // End cookie plugin |
293 | 305 |
|
294 |
| - ->arrayNode('stopwatch') |
295 |
| - ->canBeDisabled() |
296 |
| - ->addDefaultsIfNotSet() |
297 |
| - ->children() |
298 |
| - ->scalarNode('stopwatch') |
299 |
| - ->info('This must be a service id to a service extending Symfony\Component\Stopwatch\Stopwatch') |
300 |
| - ->defaultValue('debug.stopwatch') |
301 |
| - ->cannotBeEmpty() |
302 |
| - ->end() |
303 |
| - ->end() |
304 |
| - ->end() // End stopwatch plugin |
| 306 | + $decoder = $children->arrayNode('decoder'); |
| 307 | + if ($disableAll) { |
| 308 | + $decoder->canBeEnabled(); |
| 309 | + } else { |
| 310 | + $decoder->canBeDisabled(); |
| 311 | + } |
| 312 | + $decoder->addDefaultsIfNotSet() |
| 313 | + ->children() |
| 314 | + ->scalarNode('use_content_encoding')->defaultTrue()->end() |
| 315 | + ->end() |
| 316 | + ->end(); |
| 317 | + // End decoder plugin |
305 | 318 |
|
| 319 | + $children->arrayNode('history') |
| 320 | + ->canBeEnabled() |
| 321 | + ->children() |
| 322 | + ->scalarNode('journal') |
| 323 | + ->info('This must be a service id to a service implementing Http\Client\Plugin\Journal') |
| 324 | + ->isRequired() |
| 325 | + ->cannotBeEmpty() |
| 326 | + ->end() |
| 327 | + ->end() |
| 328 | + ->end(); |
| 329 | + // End history plugin |
| 330 | + |
| 331 | + $logger = $children->arrayNode('logger'); |
| 332 | + if ($disableAll) { |
| 333 | + $logger->canBeEnabled(); |
| 334 | + } else { |
| 335 | + $logger->canBeDisabled(); |
| 336 | + } |
| 337 | + $logger->addDefaultsIfNotSet() |
| 338 | + ->children() |
| 339 | + ->scalarNode('logger') |
| 340 | + ->info('This must be a service id to a service implementing Psr\Log\LoggerInterface') |
| 341 | + ->defaultValue('logger') |
| 342 | + ->cannotBeEmpty() |
| 343 | + ->end() |
| 344 | + ->scalarNode('formatter') |
| 345 | + ->info('This must be a service id to a service implementing Http\Message\Formatter') |
| 346 | + ->defaultNull() |
| 347 | + ->end() |
| 348 | + ->end() |
| 349 | + ->end(); |
| 350 | + // End logger plugin |
| 351 | + |
| 352 | + $redirect = $children->arrayNode('redirect'); |
| 353 | + if ($disableAll) { |
| 354 | + $redirect->canBeEnabled(); |
| 355 | + } else { |
| 356 | + $redirect->canBeDisabled(); |
| 357 | + } |
| 358 | + $redirect->addDefaultsIfNotSet() |
| 359 | + ->children() |
| 360 | + ->scalarNode('preserve_header')->defaultTrue()->end() |
| 361 | + ->scalarNode('use_default_for_multiple')->defaultTrue()->end() |
| 362 | + ->end() |
| 363 | + ->end(); |
| 364 | + // End redirect plugin |
| 365 | + |
| 366 | + $retry = $children->arrayNode('retry'); |
| 367 | + if ($disableAll) { |
| 368 | + $retry->canBeEnabled(); |
| 369 | + } else { |
| 370 | + $retry->canBeDisabled(); |
| 371 | + } |
| 372 | + $retry->addDefaultsIfNotSet() |
| 373 | + ->children() |
| 374 | + ->scalarNode('retry')->defaultValue(1)->end() // TODO: should be called retries for consistency with the class |
| 375 | + ->end() |
| 376 | + ->end(); |
| 377 | + // End retry plugin |
| 378 | + |
| 379 | + $stopwatch = $children->arrayNode('stopwatch'); |
| 380 | + if ($disableAll) { |
| 381 | + $stopwatch->canBeEnabled(); |
| 382 | + } else { |
| 383 | + $stopwatch->canBeDisabled(); |
| 384 | + } |
| 385 | + $stopwatch->addDefaultsIfNotSet() |
| 386 | + ->children() |
| 387 | + ->scalarNode('stopwatch') |
| 388 | + ->info('This must be a service id to a service extending Symfony\Component\Stopwatch\Stopwatch') |
| 389 | + ->defaultValue('debug.stopwatch') |
| 390 | + ->cannotBeEmpty() |
306 | 391 | ->end()
|
307 | 392 | ->end()
|
308 | 393 | ->end();
|
| 394 | + // End stopwatch plugin |
309 | 395 | }
|
310 | 396 |
|
311 | 397 | /**
|
312 |
| - * Add configuration for authentication plugin. |
| 398 | + * Create configuration for authentication plugin. |
313 | 399 | *
|
314 |
| - * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
| 400 | + * @return NodeDefinition Definition for the authentication node in the plugins list. |
315 | 401 | */
|
316 |
| - private function addAuthenticationPluiginNode() |
| 402 | + private function createAuthenticationPluginNode() |
317 | 403 | {
|
318 | 404 | $builder = new TreeBuilder();
|
319 | 405 | $node = $builder->root('authentication');
|
|
0 commit comments