Skip to content

Commit b7b4bf3

Browse files
committed
use xml to configure discovered clients
1 parent f507c1a commit b7b4bf3

File tree

3 files changed

+70
-108
lines changed

3 files changed

+70
-108
lines changed

DependencyInjection/HttplugExtension.php

Lines changed: 16 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
use Http\Client\Common\FlexibleHttpClient;
77
use Http\Client\Common\HttpMethodsClient;
88
use Http\Client\Common\Plugin\AuthenticationPlugin;
9-
use Http\Discovery\HttpAsyncClientDiscovery;
10-
use Http\Discovery\HttpClientDiscovery;
119
use Http\HttplugBundle\ClientFactory\DummyClient;
1210
use Http\HttplugBundle\ClientFactory\PluginClientFactory;
13-
use Http\HttplugBundle\Collector\ProfileClientFactory;
1411
use Http\HttplugBundle\Collector\ProfilePlugin;
1512
use Http\Message\Authentication\BasicAuth;
1613
use Http\Message\Authentication\Bearer;
@@ -359,94 +356,34 @@ private function createUri(ContainerBuilder $container, $serviceId, $uri)
359356
private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config)
360357
{
361358
$httpClient = $config['discovery']['client'];
362-
363-
if (!empty($httpClient)) {
364-
if ($httpClient === 'auto') {
365-
$httpClient = $this->registerAutoDiscoverableClient(
366-
$container,
367-
'auto_discovered_client',
368-
$this->configureAutoDiscoveryFactory(
369-
$container,
370-
HttpClientDiscovery::class,
371-
'auto_discovered_client',
372-
$config
373-
),
374-
$this->isConfigEnabled($container, $config['profiling'])
375-
);
359+
if ($httpClient !== 'auto') {
360+
$container->removeDefinition('httplug.auto_discovery.auto_discovered_client');
361+
$container->removeDefinition('httplug.collector.auto_discovered_client');
362+
$container->removeDefinition('httplug.auto_discovery.auto_discovered_client.plugin');
363+
364+
if (!empty($httpClient)) {
365+
$container->setAlias('httplug.auto_discovery.auto_discovered_client', $httpClient);
366+
$container->getAlias('httplug.auto_discovery.auto_discovered_client')->setPublic(false);
376367
}
377-
378-
$httpClient = new Reference($httpClient);
379368
}
380369

381370
$asyncHttpClient = $config['discovery']['async_client'];
382-
383-
if (!empty($asyncHttpClient)) {
384-
if ($asyncHttpClient === 'auto') {
385-
$asyncHttpClient = $this->registerAutoDiscoverableClient(
386-
$container,
387-
'auto_discovered_async',
388-
$this->configureAutoDiscoveryFactory(
389-
$container,
390-
HttpAsyncClientDiscovery::class,
391-
'auto_discovered_async',
392-
$config
393-
),
394-
$this->isConfigEnabled($container, $config['profiling'])
395-
);
371+
if ($asyncHttpClient !== 'auto') {
372+
$container->removeDefinition('httplug.auto_discovery.auto_discovered_async');
373+
$container->removeDefinition('httplug.collector.auto_discovered_async');
374+
$container->removeDefinition('httplug.auto_discovery.auto_discovered_async.plugin');
375+
376+
if (!empty($asyncHttpClient)) {
377+
$container->setAlias('httplug.auto_discovery.auto_discovered_async', $asyncHttpClient);
378+
$container->getAlias('httplug.auto_discovery.auto_discovered_async')->setPublic(false);
396379
}
397-
398-
$asyncHttpClient = new Reference($asyncHttpClient);
399380
}
400381

401382
if (null === $httpClient && null === $asyncHttpClient) {
402383
$container->removeDefinition('httplug.strategy');
403384

404385
return;
405386
}
406-
407-
$container
408-
->getDefinition('httplug.strategy')
409-
->addArgument($httpClient)
410-
->addArgument($asyncHttpClient)
411-
;
412-
}
413-
414-
/**
415-
* Find a client with auto discovery and return a service Reference to it.
416-
*
417-
* @param ContainerBuilder $container
418-
* @param string $name
419-
* @param Reference|callable $factory
420-
* @param bool $profiling
421-
*
422-
* @return string service id
423-
*/
424-
private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory, $profiling)
425-
{
426-
$serviceId = 'httplug.auto_discovery.'.$name;
427-
428-
$plugins = [];
429-
if ($profiling) {
430-
// To profile the requests, add a StackPlugin as first plugin in the chain.
431-
$plugins[] = $this->configureStackPlugin($container, $name, $serviceId);
432-
}
433-
434-
$container
435-
->register($serviceId, DummyClient::class)
436-
->setFactory([PluginClientFactory::class, 'createPluginClient'])
437-
->setArguments([
438-
array_map(
439-
function ($id) {
440-
return new Reference($id);
441-
},
442-
$plugins
443-
),
444-
$factory,
445-
[],
446-
])
447-
;
448-
449-
return $serviceId;
450387
}
451388

452389
/**
@@ -521,33 +458,4 @@ private function configureStackPlugin(ContainerBuilder $container, $clientName,
521458

522459
return $pluginServiceId;
523460
}
524-
525-
/**
526-
* Configure the discovery factory when profiling is enabled to get client decorated with a ProfileClient.
527-
*
528-
* @param ContainerBuilder $container
529-
* @param string $discovery
530-
* @param string $name
531-
* @param array $config
532-
*
533-
* @return callable|Reference
534-
*/
535-
private function configureAutoDiscoveryFactory(ContainerBuilder $container, $discovery, $name, array $config)
536-
{
537-
$factory = [$discovery, 'find'];
538-
if ($this->isConfigEnabled($container, $config['profiling'])) {
539-
$factoryServiceId = 'httplug.auto_discovery.'.$name.'.factory';
540-
$container->register($factoryServiceId, ProfileClientFactory::class)
541-
->setPublic(false)
542-
->setArguments([
543-
$factory,
544-
new Reference('httplug.collector.collector'),
545-
new Reference('httplug.collector.formatter'),
546-
new Reference('debug.stopwatch'),
547-
]);
548-
$factory = new Reference($factoryServiceId);
549-
}
550-
551-
return $factory;
552-
}
553461
}

Resources/config/data-collector.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,42 @@
2626
<tag name="twig.extension" />
2727
</service>
2828

29+
<!-- Discovered clients -->
30+
<service id="httplug.collector.auto_discovered_client" class="Http\HttplugBundle\Collector\ProfileClient" decorates="httplug.auto_discovery.auto_discovered_client" decoration-priority="2" public="false">
31+
<argument type="service" id="httplug.collector.auto_discovered_client.inner"/>
32+
<argument type="service" id="httplug.collector.collector"/>
33+
<argument type="service" id="httplug.collector.formatter"/>
34+
<argument type="service" id="debug.stopwatch"/>
35+
</service>
36+
37+
<service id="httplug.collector.auto_discovered_async" class="Http\HttplugBundle\Collector\ProfileClient" decorates="httplug.auto_discovery.auto_discovered_async" decoration-priority="2" public="false">
38+
<argument type="service" id="httplug.collector.auto_discovered_async.inner"/>
39+
<argument type="service" id="httplug.collector.collector"/>
40+
<argument type="service" id="httplug.collector.formatter"/>
41+
<argument type="service" id="debug.stopwatch"/>
42+
</service>
43+
44+
<service id="httplug.auto_discovery.auto_discovered_client.plugin" class="Http\Client\Common\PluginClient" decorates="httplug.auto_discovery.auto_discovered_client" public="false">
45+
<argument type="service" id="httplug.auto_discovery.auto_discovered_client.plugin.inner"/>
46+
<argument type="collection">
47+
<argument type="service">
48+
<service parent="httplug.plugin.stack">
49+
<argument>auto_discovered_client</argument>
50+
</service>
51+
</argument>
52+
</argument>
53+
</service>
54+
<service id="httplug.auto_discovery.auto_discovered_async.plugin" class="Http\Client\Common\PluginClient" decorates="httplug.auto_discovery.auto_discovered_async" public="false">
55+
<argument type="service" id="httplug.auto_discovery.auto_discovered_async.plugin.inner"/>
56+
<argument type="collection">
57+
<argument type="service">
58+
<service parent="httplug.plugin.stack">
59+
<argument>auto_discovered_async</argument>
60+
</service>
61+
</argument>
62+
</argument>
63+
</service>
64+
2965
<!-- ClientFactories -->
3066
<service id="httplug.collector.factory.auto" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.auto" public="false">
3167
<argument type="service" id="httplug.collector.factory.auto.inner"/>

Resources/config/services.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,27 @@
55

66
<services>
77
<service id="httplug.strategy" class="Http\HttplugBundle\Discovery\ConfiguredClientsStrategy">
8+
<argument type="service" id="httplug.auto_discovery.auto_discovered_client" on-invalid="null"/>
9+
<argument type="service" id="httplug.auto_discovery.auto_discovered_async" on-invalid="null"/>
810
<tag name="kernel.event_subscriber"/>
911
</service>
1012

13+
<service id="httplug.auto_discovery.auto_discovered_client" class="Http\Client\HttpClient">
14+
<factory class="Http\Discovery\HttpClientDiscovery" method="find" />
15+
</service>
16+
17+
<service id="httplug.auto_discovery.auto_discovered_async" class="Http\Client\HttpAsyncClient">
18+
<factory class="Http\Discovery\HttpAsyncClientDiscovery" method="find" />
19+
</service>
20+
21+
<!-- Decorate discovered clients with PluginClient -->
22+
<service id="httplug.auto_discovery.auto_discovered_client.plugin" class="Http\Client\Common\PluginClient" decorates="httplug.auto_discovery.auto_discovered_client" decoration-priority="1" public="false">
23+
<argument type="service" id="httplug.auto_discovery.auto_discovered_client.plugin.inner"/>
24+
</service>
25+
<service id="httplug.auto_discovery.auto_discovered_async.plugin" class="Http\Client\Common\PluginClient" decorates="httplug.auto_discovery.auto_discovered_async" decoration-priority="1" public="false">
26+
<argument type="service" id="httplug.auto_discovery.auto_discovered_async.plugin.inner"/>
27+
</service>
28+
1129
<!-- Discovery with autowiring support for Symfony 3.3+ -->
1230
<service id="httplug.message_factory.default" class="Http\Message\MessageFactory">
1331
<factory class="Http\Discovery\MessageFactoryDiscovery" method="find" />

0 commit comments

Comments
 (0)