Skip to content

Commit 6c98e84

Browse files
committed
Allow to configure plugins specifically for a client
1 parent c2493b5 commit 6c98e84

File tree

10 files changed

+350
-114
lines changed

10 files changed

+350
-114
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
### Added
77

8+
- Option to configure default host for each client. default_host to add host if missing, force_host to change all requests to a specific host.
89
- Support for BatchClient
9-
- The stopwatch plugin in included by default when using profiling.
10+
- The stopwatch plugin in included by default when using profiling.
1011

1112
### Changed
1213

DependencyInjection/Configuration.php

Lines changed: 181 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Symfony\Component\Config\Definition\ArrayNode;
66
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
7+
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
78
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
89
use Symfony\Component\Config\Definition\ConfigurationInterface;
910
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
@@ -92,6 +93,7 @@ public function getConfigTreeBuilder()
9293
return $v;
9394
})
9495
->end()
96+
->fixXmlConfig('client')
9597
->children()
9698
->arrayNode('main_alias')
9799
->addDefaultsIfNotSet()
@@ -150,7 +152,7 @@ public function getConfigTreeBuilder()
150152
return $treeBuilder;
151153
}
152154

153-
protected function configureClients(ArrayNodeDefinition $root)
155+
private function configureClients(ArrayNodeDefinition $root)
154156
{
155157
$root->children()
156158
->arrayNode('clients')
@@ -189,131 +191,215 @@ protected function configureClients(ArrayNodeDefinition $root)
189191
->prototype('scalar')->end()
190192
->end()
191193
->variableNode('config')->defaultValue([])->end()
194+
->append($this->createExtraPluginsNode())
192195
->end()
193196
->end();
194197
}
195198

196199
/**
197200
* @param ArrayNodeDefinition $root
198201
*/
199-
protected function configurePlugins(ArrayNodeDefinition $root)
202+
private function configurePlugins(ArrayNodeDefinition $root)
200203
{
201-
$root->children()
202-
->arrayNode('plugins')
204+
$pluginsNode = $root
205+
->children()
206+
->arrayNode('plugins')
203207
->addDefaultsIfNotSet()
204-
->children()
205-
->append($this->addAuthenticationPluiginNode())
208+
;
209+
$this->configureSharedPluginNodes($pluginsNode);
210+
}
206211

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')
208238
->canBeEnabled()
209239
->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()
228246
->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)
239250
->end()
240-
->end() // End cookie plugin
251+
->end()
252+
->end()
253+
->end()
254+
->end();
241255

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+
}
249258

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();
260266

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());
276268

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()
280285
->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()
283288
->end()
284-
->end() // End redirect plugin
289+
->end()
290+
->end()
291+
->end();
292+
// End cache plugin
285293

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
293305

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
305318

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()
306391
->end()
307392
->end()
308393
->end();
394+
// End stopwatch plugin
309395
}
310396

311397
/**
312-
* Add configuration for authentication plugin.
398+
* Create configuration for authentication plugin.
313399
*
314-
* @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition
400+
* @return NodeDefinition Definition for the authentication node in the plugins list.
315401
*/
316-
private function addAuthenticationPluiginNode()
402+
private function createAuthenticationPluginNode()
317403
{
318404
$builder = new TreeBuilder();
319405
$node = $builder->root('authentication');

0 commit comments

Comments
 (0)