Skip to content

Commit d49f7d6

Browse files
committed
Let user configure what do be found in discovery and if profiling should be used.
1 parent 68ccd1e commit d49f7d6

File tree

5 files changed

+98
-4
lines changed

5 files changed

+98
-4
lines changed

Collector/PluginJournal.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ final class PluginJournal
2121
*/
2222
public function getPlugins($clientName)
2323
{
24-
return $this->data[$clientName];
24+
if (isset($this->data[$clientName])) {
25+
return $this->data[$clientName];
26+
}
27+
28+
return [];
2529
}
2630

2731
/**

DependencyInjection/Configuration.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public function getConfigTreeBuilder()
8383
->defaultValue('auto')
8484
->end()
8585
->scalarNode('formatter')->defaultNull()->end()
86+
->booleanNode('profile_discovered_client')->defaultTrue()->end()
87+
->booleanNode('profile_discovered_async_client')->defaultFalse()->end()
8688
->scalarNode('captured_body_length')
8789
->defaultValue(0)
8890
->canNotBeEmpty()
@@ -124,6 +126,11 @@ protected function configureClients(ArrayNodeDefinition $root)
124126
->defaultFalse()
125127
->info('Set to true to get the client wrapped in a HttpMethodsClient which emulates provides functions for HTTP verbs.')
126128
->end()
129+
->enumNode('use_with_discovery')
130+
->values(['async_client', 'http_client', false])
131+
->defaultFalse()
132+
->info('If set to "http_client" this will be the client return when using dicovery to find a HTTP client.')
133+
->end()
127134
->arrayNode('plugins')
128135
->info('A list of service ids of plugins. The order is important.')
129136
->prototype('scalar')->end()

DependencyInjection/HttplugExtension.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Http\Client\Common\HttpMethodsClient;
77
use Http\Client\Common\Plugin\AuthenticationPlugin;
88
use Http\Client\Common\PluginClient;
9+
use Http\Discovery\HttpAsyncClientDiscovery;
910
use Http\HttplugBundle\ClientFactory\DummyClient;
1011
use Http\HttplugBundle\Collector\DebugPlugin;
1112
use Http\Message\Authentication\BasicAuth;
@@ -65,6 +66,7 @@ public function load(array $configs, ContainerBuilder $container)
6566

6667
$this->configurePlugins($container, $config['plugins']);
6768
$this->configureClients($container, $config);
69+
$this->configureAutoDiscoveryClients($container, $config);
6870
}
6971

7072
/**
@@ -278,4 +280,72 @@ private function registerDebugPlugin(ContainerBuilder $container, $name)
278280

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

Discovery/ConfiguredClientsStrategy.php

Lines changed: 16 additions & 2 deletions
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,18 @@ class ConfiguredClientsStrategy implements DiscoveryStrategy, EventSubscriberInt
2425
private static $client;
2526

2627
/**
27-
* @param HttpClient $httpClient
28+
* @var HttpAsyncClient
2829
*/
29-
public function __construct(HttpClient $httpClient)
30+
private static $asyncClient;
31+
32+
/**
33+
* @param HttpClient $httpClient
34+
* @param HttpAsyncClient $asyncClient
35+
*/
36+
public function __construct(HttpClient $httpClient = null, HttpAsyncClient $asyncClient = null)
3037
{
3138
static::$client = $httpClient;
39+
static::$asyncClient = $asyncClient;
3240
}
3341

3442
/**
@@ -42,6 +50,12 @@ public static function getCandidates($type)
4250
}]];
4351
}
4452

53+
if (static::$asyncClient !== null && $type == HttpAsyncClient::class) {
54+
return [['class' => function () {
55+
return static::$asyncClient;
56+
}]];
57+
}
58+
4559
return [];
4660
}
4761

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)