Skip to content

Commit 64a3ebb

Browse files
committed
Allow to configure plugins specifically for a client
1 parent d56a403 commit 64a3ebb

File tree

10 files changed

+351
-114
lines changed

10 files changed

+351
-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+
- You can now configure plugins on each client in `extra_plugins`. Plugins that you previously had to define your own services can be configured on the client.
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: 182 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,216 @@ 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+
}
211+
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+
}
206231

207-
->arrayNode('cache')
232+
return $plugins;
233+
})
234+
;
235+
$this->configureSharedPluginNodes($node, true);
236+
$node
237+
->children()
238+
->arrayNode('add_host')
208239
->canBeEnabled()
209240
->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()
241+
->info('Configure the AddHostPlugin for this client.')
242+
->children()
243+
->scalarNode('host')
244+
->info('Host name including protocol and optionally the port number, e.g. https://api.local:8000')
245+
->isRequired()
246+
->cannotBeEmpty()
228247
->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()
248+
->scalarNode('replace')
249+
->info('Whether to replace the host if request already specifies it')
250+
->defaultValue(false)
239251
->end()
240-
->end() // End cookie plugin
252+
->end()
253+
->end()
254+
->end()
255+
->end();
241256

242-
->arrayNode('decoder')
243-
->canBeDisabled()
244-
->addDefaultsIfNotSet()
245-
->children()
246-
->scalarNode('use_content_encoding')->defaultTrue()->end()
247-
->end()
248-
->end() // End decoder plugin
257+
return $node;
258+
}
249259

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
260+
/**
261+
* @param ArrayNodeDefinition $pluginNode
262+
* @param bool $disableAll Some shared plugins are enabled by default. On the client, all are disabled by default.
263+
*/
264+
private function configureSharedPluginNodes(ArrayNodeDefinition $pluginNode, $disableAll = false)
265+
{
266+
$children = $pluginNode->children();
260267

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
268+
$children->append($this->createAuthenticationPluginNode());
276269

277-
->arrayNode('redirect')
278-
->canBeDisabled()
279-
->addDefaultsIfNotSet()
270+
$children->arrayNode('cache')
271+
->canBeEnabled()
272+
->addDefaultsIfNotSet()
273+
->children()
274+
->scalarNode('cache_pool')
275+
->info('This must be a service id to a service implementing Psr\Cache\CacheItemPoolInterface')
276+
->isRequired()
277+
->cannotBeEmpty()
278+
->end()
279+
->scalarNode('stream_factory')
280+
->info('This must be a service id to a service implementing Http\Message\StreamFactory')
281+
->defaultValue('httplug.stream_factory')
282+
->cannotBeEmpty()
283+
->end()
284+
->arrayNode('config')
285+
->addDefaultsIfNotSet()
280286
->children()
281-
->scalarNode('preserve_header')->defaultTrue()->end()
282-
->scalarNode('use_default_for_multiple')->defaultTrue()->end()
287+
->scalarNode('default_ttl')->defaultNull()->end()
288+
->scalarNode('respect_cache_headers')->defaultTrue()->end()
283289
->end()
284-
->end() // End redirect plugin
290+
->end()
291+
->end()
292+
->end();
293+
// End cache plugin
285294

286-
->arrayNode('retry')
287-
->canBeDisabled()
288-
->addDefaultsIfNotSet()
289-
->children()
290-
->scalarNode('retry')->defaultValue(1)->end()
291-
->end()
292-
->end() // End retry plugin
295+
$children->arrayNode('cookie')
296+
->canBeEnabled()
297+
->children()
298+
->scalarNode('cookie_jar')
299+
->info('This must be a service id to a service implementing Http\Message\CookieJar')
300+
->isRequired()
301+
->cannotBeEmpty()
302+
->end()
303+
->end()
304+
->end();
305+
// End cookie plugin
293306

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
307+
$decoder = $children->arrayNode('decoder');
308+
if ($disableAll) {
309+
$decoder->canBeEnabled();
310+
} else {
311+
$decoder->canBeDisabled();
312+
}
313+
$decoder->addDefaultsIfNotSet()
314+
->children()
315+
->scalarNode('use_content_encoding')->defaultTrue()->end()
316+
->end()
317+
->end();
318+
// End decoder plugin
319+
320+
$children->arrayNode('history')
321+
->canBeEnabled()
322+
->children()
323+
->scalarNode('journal')
324+
->info('This must be a service id to a service implementing Http\Client\Plugin\Journal')
325+
->isRequired()
326+
->cannotBeEmpty()
327+
->end()
328+
->end()
329+
->end();
330+
// End history plugin
331+
332+
$logger = $children->arrayNode('logger');
333+
if ($disableAll) {
334+
$logger->canBeEnabled();
335+
} else {
336+
$logger->canBeDisabled();
337+
}
338+
$logger->addDefaultsIfNotSet()
339+
->children()
340+
->scalarNode('logger')
341+
->info('This must be a service id to a service implementing Psr\Log\LoggerInterface')
342+
->defaultValue('logger')
343+
->cannotBeEmpty()
344+
->end()
345+
->scalarNode('formatter')
346+
->info('This must be a service id to a service implementing Http\Message\Formatter')
347+
->defaultNull()
348+
->end()
349+
->end()
350+
->end();
351+
// End logger plugin
352+
353+
$redirect = $children->arrayNode('redirect');
354+
if ($disableAll) {
355+
$redirect->canBeEnabled();
356+
} else {
357+
$redirect->canBeDisabled();
358+
}
359+
$redirect->addDefaultsIfNotSet()
360+
->children()
361+
->scalarNode('preserve_header')->defaultTrue()->end()
362+
->scalarNode('use_default_for_multiple')->defaultTrue()->end()
363+
->end()
364+
->end();
365+
// End redirect plugin
305366

367+
$retry = $children->arrayNode('retry');
368+
if ($disableAll) {
369+
$retry->canBeEnabled();
370+
} else {
371+
$retry->canBeDisabled();
372+
}
373+
$retry->addDefaultsIfNotSet()
374+
->children()
375+
->scalarNode('retry')->defaultValue(1)->end() // TODO: should be called retries for consistency with the class
376+
->end()
377+
->end();
378+
// End retry plugin
379+
380+
$stopwatch = $children->arrayNode('stopwatch');
381+
if ($disableAll) {
382+
$stopwatch->canBeEnabled();
383+
} else {
384+
$stopwatch->canBeDisabled();
385+
}
386+
$stopwatch->addDefaultsIfNotSet()
387+
->children()
388+
->scalarNode('stopwatch')
389+
->info('This must be a service id to a service extending Symfony\Component\Stopwatch\Stopwatch')
390+
->defaultValue('debug.stopwatch')
391+
->cannotBeEmpty()
306392
->end()
307393
->end()
308394
->end();
395+
// End stopwatch plugin
309396
}
310397

311398
/**
312-
* Add configuration for authentication plugin.
399+
* Create configuration for authentication plugin.
313400
*
314-
* @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition
401+
* @return NodeDefinition Definition for the authentication node in the plugins list.
315402
*/
316-
private function addAuthenticationPluiginNode()
403+
private function createAuthenticationPluginNode()
317404
{
318405
$builder = new TreeBuilder();
319406
$node = $builder->root('authentication');

0 commit comments

Comments
 (0)