Skip to content

Commit 1e806aa

Browse files
Nyholmsagikazarmark
authored andcommitted
Discovery bugfixes and more flexible. (#87)
* Let user configure what do be found in discovery and if profiling should be used. * Use strict comparison. * fixed tests * Updated config * Bugfix * Removed code not used * Minor fix * evalute class type first * Removed `public="true"`
1 parent c516a4e commit 1e806aa

File tree

6 files changed

+96
-6
lines changed

6 files changed

+96
-6
lines changed

Collector/PluginJournal.php

+5-1
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

+14
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ public function getConfigTreeBuilder()
9090
->end()
9191
->end()
9292
->end()
93+
->arrayNode('discovery')
94+
->addDefaultsIfNotSet()
95+
->info('Control what clients should be found by the discovery.')
96+
->children()
97+
->scalarNode('client')
98+
->defaultValue('auto')
99+
->info('Set to "auto" to see auto discovered client in the web profiler. If provided a service id for a client then this client will be found by auto discovery.')
100+
->end()
101+
->scalarNode('async_client')
102+
->defaultNull()
103+
->info('Set to "auto" to see auto discovered client in the web profiler. If provided a service id for a client then this client will be found by auto discovery.')
104+
->end()
105+
->end()
106+
->end()
93107
->end();
94108

95109
return $treeBuilder;

DependencyInjection/HttplugExtension.php

+51
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,53 @@ 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 = $config['discovery']['client'];
293+
if ($httpClient === 'auto') {
294+
$httpClient = $this->registerAutoDiscoverableClientWithDebugPlugin($container, 'client');
295+
} elseif ($httpClient) {
296+
$httpClient = new Reference($httpClient);
297+
}
298+
299+
$asyncHttpClient = $config['discovery']['async_client'];
300+
if ($asyncHttpClient === 'auto') {
301+
$asyncHttpClient = $this->registerAutoDiscoverableClientWithDebugPlugin($container, 'async_client');
302+
} elseif ($asyncHttpClient) {
303+
$asyncHttpClient = new Reference($httpClient);
304+
}
305+
306+
$container->getDefinition('httplug.strategy')
307+
->addArgument($httpClient)
308+
->addArgument($asyncHttpClient);
309+
}
310+
311+
/**
312+
* @param ContainerBuilder $container
313+
* @param $name
314+
*
315+
* @return Reference
316+
*/
317+
private function registerAutoDiscoverableClientWithDebugPlugin(ContainerBuilder $container, $name)
318+
{
319+
$definition = $container->register('httplug.auto_discovery_'.$name.'.pure', DummyClient::class);
320+
$definition->setPublic(false);
321+
$definition->setFactory([HttpAsyncClientDiscovery::class, 'find']);
322+
323+
$serviceIdDebugPlugin = $this->registerDebugPlugin($container, 'auto_discovery_'.$name);
324+
$container->register('httplug.auto_discovery_'.$name.'.plugin', PluginClient::class)
325+
->setPublic(false)
326+
->addArgument(new Reference('httplug.auto_discovery_'.$name.'.pure'))
327+
->addArgument([])
328+
->addArgument(['debug_plugins' => [new Reference($serviceIdDebugPlugin)]]);
329+
330+
return new Reference('httplug.auto_discovery_'.$name.'.plugin');
331+
}
281332
}

Discovery/ConfiguredClientsStrategy.php

+17-3
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,24 +25,37 @@ 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
/**
3543
* {@inheritdoc}
3644
*/
3745
public static function getCandidates($type)
3846
{
39-
if (static::$client !== null && $type == HttpClient::class) {
47+
if ($type === HttpClient::class && static::$client !== null) {
4048
return [['class' => function () {
4149
return static::$client;
4250
}]];
4351
}
4452

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

Resources/config/services.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
55

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

Tests/Unit/DependencyInjection/ConfigurationTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public function testEmptyConfiguration()
8181
'stopwatch' => 'debug.stopwatch',
8282
],
8383
],
84+
'discovery' => [
85+
'client' => 'auto',
86+
'async_client' => null,
87+
],
8488
];
8589

8690
$formats = array_map(function ($path) {
@@ -178,6 +182,10 @@ public function testSupportsAllConfigFormats()
178182
'stopwatch' => 'debug.stopwatch',
179183
],
180184
],
185+
'discovery' => [
186+
'client' => 'auto',
187+
'async_client' => null,
188+
],
181189
];
182190

183191
$formats = array_map(function ($path) {

0 commit comments

Comments
 (0)