Skip to content

Commit d5e9bcd

Browse files
committed
Better discovery strategy
1 parent 07c018a commit d5e9bcd

File tree

5 files changed

+95
-3
lines changed

5 files changed

+95
-3
lines changed

Collector/PluginJournal.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ class PluginJournal
1919
*/
2020
public function getPlugins($clientName)
2121
{
22-
return $this->data[$clientName];
22+
if (isset($this->data[$clientName])) {
23+
return $this->data[$clientName];
24+
}
25+
26+
return [];
2327
}
2428

2529
/**

DependencyInjection/Configuration.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ protected function configureClients(ArrayNodeDefinition $root)
124124
->defaultFalse()
125125
->info('Set to true to get the client wrapped in a HttpMethodsClient which emulates provides functions for HTTP verbs.')
126126
->end()
127+
->enumNode('use_with_discovery')
128+
->values(['async_client', 'http_client', false])
129+
->defaultFalse()
130+
->info('If set to "http_client" this will be the client return when using dicovery to find a HTTP client.')
131+
->end()
127132
->arrayNode('plugins')
128133
->info('A list of service ids of plugins. The order is important.')
129134
->prototype('scalar')->end()

DependencyInjection/HttplugExtension.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
use Http\Client\Common\HttpMethodsClient;
77
use Http\Client\Common\Plugin\AuthenticationPlugin;
88
use Http\Client\Common\PluginClient;
9+
use Http\Client\HttpAsyncClient;
10+
use Http\Client\HttpClient;
11+
use Http\Discovery\HttpAsyncClientDiscovery;
12+
use Http\Discovery\HttpClientDiscovery;
913
use Http\HttplugBundle\ClientFactory\DummyClient;
1014
use Http\HttplugBundle\Collector\DebugPlugin;
1115
use Http\Message\Authentication\BasicAuth;
@@ -65,6 +69,7 @@ public function load(array $configs, ContainerBuilder $container)
6569

6670
$this->configurePlugins($container, $config['plugins']);
6771
$this->configureClients($container, $config);
72+
$this->configureAutoDiscoveryClients($container, $config);
6873
}
6974

7075
/**
@@ -278,4 +283,68 @@ private function registerDebugPlugin(ContainerBuilder $container, $name)
278283

279284
return $serviceIdDebugPlugin;
280285
}
286+
287+
/**
288+
* Make sure we inject the debug plugin for clients found by auto discovery.
289+
*
290+
* @param ContainerBuilder $container
291+
* @param array $config
292+
*/
293+
private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config)
294+
{
295+
$httpClient = null;
296+
$asyncHttpClient = null;
297+
298+
// Verify if any clients were specifucally set to function as auto discoverable.
299+
foreach ($config['clients'] as $name => $arguments) {
300+
if ($arguments['use_with_discovery'] === 'http_client') {
301+
if ($httpClient !== null) {
302+
throw new \LogicException('Only one client can configured with "use_with_discovery: http_client".');
303+
}
304+
$httpClient = new Reference('http.client.'.$name);
305+
} elseif ($arguments['use_with_discovery'] === 'async_client') {
306+
if ($asyncHttpClient !== null) {
307+
throw new \LogicException('Only one client can be configured with "use_with_discovery: async_client".');
308+
}
309+
$asyncHttpClient = new Reference('http.client.'.$name);
310+
}
311+
}
312+
313+
if ($httpClient === null) {
314+
// Use auto discovery
315+
$asyncHttpClient = $this->registerAutoDiscoverableClientWithDebugPlugin($container, 'client');
316+
}
317+
318+
if ($asyncHttpClient === null) {
319+
// Use auto discovery
320+
$asyncHttpClient = $this->registerAutoDiscoverableClientWithDebugPlugin($container, 'async_client');
321+
}
322+
323+
324+
$container->getDefinition('httplug.strategy')
325+
->addArgument($httpClient)
326+
->addArgument($asyncHttpClient);
327+
}
328+
329+
/**
330+
* @param ContainerBuilder $container
331+
* @param $name
332+
*
333+
* @return Reference
334+
*/
335+
private function registerAutoDiscoverableClientWithDebugPlugin(ContainerBuilder $container, $name)
336+
{
337+
$definition = $container->register('httplug.auto_discovery_'.$name.'.pure', DummyClient::class);
338+
$definition->setPublic(false);
339+
$definition->setFactory([HttpAsyncClientDiscovery::class, 'find']);
340+
341+
$serviceIdDebugPlugin = $this->registerDebugPlugin($container, 'auto_discovery_'.$name);
342+
$container->register('httplug.auto_discovery_'.$name.'.plugin', PluginClient::class)
343+
->setPublic(false)
344+
->addArgument(new Reference('httplug.auto_discovery_'.$name.'.pure'))
345+
->addArgument([])
346+
->addArgument(['debug_plugins' => [new Reference($serviceIdDebugPlugin)]]);
347+
348+
return new Reference('httplug.auto_discovery_'.$name.'.plugin');
349+
}
281350
}

Discovery/ConfiguredClientsStrategy.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Http\HttplugBundle\Discovery;
44

55
use Http\Client\HttpClient;
6+
use Http\Client\HttpAsyncClient;
67
use Http\Discovery\HttpClientDiscovery;
78
use Http\Discovery\Strategy\DiscoveryStrategy;
89
use Symfony\Component\Console\ConsoleEvents;
@@ -24,11 +25,19 @@ class ConfiguredClientsStrategy implements DiscoveryStrategy, EventSubscriberInt
2425
private static $client;
2526

2627
/**
28+
* @var HttpAsyncClient
29+
*/
30+
private static $asyncClient;
31+
32+
/**
33+
*
2734
* @param HttpClient $httpClient
35+
* @param HttpAsyncClient $asyncClient
2836
*/
29-
public function __construct(HttpClient $httpClient)
37+
public function __construct(HttpClient $httpClient = null, HttpAsyncClient $asyncClient = null)
3038
{
3139
static::$client = $httpClient;
40+
static::$asyncClient = $asyncClient;
3241
}
3342

3443
/**
@@ -42,6 +51,12 @@ public static function getCandidates($type)
4251
}]];
4352
}
4453

54+
if (static::$asyncClient !== null && $type == HttpAsyncClient::class) {
55+
return [['class' => function () {
56+
return static::$asyncClient;
57+
}]];
58+
}
59+
4560
return [];
4661
}
4762

Resources/config/services.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
<services>
77
<service id="httplug.strategy" class="Http\HttplugBundle\Discovery\ConfiguredClientsStrategy" public="true">
8-
<argument type="service" id="httplug.client"/>
98
<tag name="kernel.event_subscriber"/>
109
</service>
1110

0 commit comments

Comments
 (0)