diff --git a/Collector/PluginJournal.php b/Collector/PluginJournal.php index cb53d37d..cfc40e4d 100644 --- a/Collector/PluginJournal.php +++ b/Collector/PluginJournal.php @@ -21,7 +21,11 @@ final class PluginJournal */ public function getPlugins($clientName) { - return $this->data[$clientName]; + if (isset($this->data[$clientName])) { + return $this->data[$clientName]; + } + + return []; } /** diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index cf0ba826..90653dcd 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -90,6 +90,20 @@ public function getConfigTreeBuilder() ->end() ->end() ->end() + ->arrayNode('discovery') + ->addDefaultsIfNotSet() + ->info('Control what clients should be found by the discovery.') + ->children() + ->scalarNode('client') + ->defaultValue('auto') + ->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.') + ->end() + ->scalarNode('async_client') + ->defaultNull() + ->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.') + ->end() + ->end() + ->end() ->end(); return $treeBuilder; diff --git a/DependencyInjection/HttplugExtension.php b/DependencyInjection/HttplugExtension.php index 09b6c6e4..8cc63877 100644 --- a/DependencyInjection/HttplugExtension.php +++ b/DependencyInjection/HttplugExtension.php @@ -6,6 +6,7 @@ use Http\Client\Common\HttpMethodsClient; use Http\Client\Common\Plugin\AuthenticationPlugin; use Http\Client\Common\PluginClient; +use Http\Discovery\HttpAsyncClientDiscovery; use Http\HttplugBundle\ClientFactory\DummyClient; use Http\HttplugBundle\Collector\DebugPlugin; use Http\Message\Authentication\BasicAuth; @@ -65,6 +66,7 @@ public function load(array $configs, ContainerBuilder $container) $this->configurePlugins($container, $config['plugins']); $this->configureClients($container, $config); + $this->configureAutoDiscoveryClients($container, $config); } /** @@ -278,4 +280,53 @@ private function registerDebugPlugin(ContainerBuilder $container, $name) return $serviceIdDebugPlugin; } + + /** + * Make sure we inject the debug plugin for clients found by auto discovery. + * + * @param ContainerBuilder $container + * @param array $config + */ + private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config) + { + $httpClient = $config['discovery']['client']; + if ($httpClient === 'auto') { + $httpClient = $this->registerAutoDiscoverableClientWithDebugPlugin($container, 'client'); + } elseif ($httpClient) { + $httpClient = new Reference($httpClient); + } + + $asyncHttpClient = $config['discovery']['async_client']; + if ($asyncHttpClient === 'auto') { + $asyncHttpClient = $this->registerAutoDiscoverableClientWithDebugPlugin($container, 'async_client'); + } elseif ($asyncHttpClient) { + $asyncHttpClient = new Reference($httpClient); + } + + $container->getDefinition('httplug.strategy') + ->addArgument($httpClient) + ->addArgument($asyncHttpClient); + } + + /** + * @param ContainerBuilder $container + * @param $name + * + * @return Reference + */ + private function registerAutoDiscoverableClientWithDebugPlugin(ContainerBuilder $container, $name) + { + $definition = $container->register('httplug.auto_discovery_'.$name.'.pure', DummyClient::class); + $definition->setPublic(false); + $definition->setFactory([HttpAsyncClientDiscovery::class, 'find']); + + $serviceIdDebugPlugin = $this->registerDebugPlugin($container, 'auto_discovery_'.$name); + $container->register('httplug.auto_discovery_'.$name.'.plugin', PluginClient::class) + ->setPublic(false) + ->addArgument(new Reference('httplug.auto_discovery_'.$name.'.pure')) + ->addArgument([]) + ->addArgument(['debug_plugins' => [new Reference($serviceIdDebugPlugin)]]); + + return new Reference('httplug.auto_discovery_'.$name.'.plugin'); + } } diff --git a/Discovery/ConfiguredClientsStrategy.php b/Discovery/ConfiguredClientsStrategy.php index d3a71eb1..b5b88ca6 100644 --- a/Discovery/ConfiguredClientsStrategy.php +++ b/Discovery/ConfiguredClientsStrategy.php @@ -3,6 +3,7 @@ namespace Http\HttplugBundle\Discovery; use Http\Client\HttpClient; +use Http\Client\HttpAsyncClient; use Http\Discovery\HttpClientDiscovery; use Http\Discovery\Strategy\DiscoveryStrategy; use Symfony\Component\Console\ConsoleEvents; @@ -24,11 +25,18 @@ class ConfiguredClientsStrategy implements DiscoveryStrategy, EventSubscriberInt private static $client; /** - * @param HttpClient $httpClient + * @var HttpAsyncClient */ - public function __construct(HttpClient $httpClient) + private static $asyncClient; + + /** + * @param HttpClient $httpClient + * @param HttpAsyncClient $asyncClient + */ + public function __construct(HttpClient $httpClient = null, HttpAsyncClient $asyncClient = null) { static::$client = $httpClient; + static::$asyncClient = $asyncClient; } /** @@ -36,12 +44,18 @@ public function __construct(HttpClient $httpClient) */ public static function getCandidates($type) { - if (static::$client !== null && $type == HttpClient::class) { + if ($type === HttpClient::class && static::$client !== null) { return [['class' => function () { return static::$client; }]]; } + if ($type === HttpAsyncClient::class && static::$asyncClient !== null) { + return [['class' => function () { + return static::$asyncClient; + }]]; + } + return []; } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 78648a20..b5ad3efc 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -4,8 +4,7 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - - + diff --git a/Tests/Unit/DependencyInjection/ConfigurationTest.php b/Tests/Unit/DependencyInjection/ConfigurationTest.php index 1a571c73..15764cbe 100644 --- a/Tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/Tests/Unit/DependencyInjection/ConfigurationTest.php @@ -81,6 +81,10 @@ public function testEmptyConfiguration() 'stopwatch' => 'debug.stopwatch', ], ], + 'discovery' => [ + 'client' => 'auto', + 'async_client' => null, + ], ]; $formats = array_map(function ($path) { @@ -178,6 +182,10 @@ public function testSupportsAllConfigFormats() 'stopwatch' => 'debug.stopwatch', ], ], + 'discovery' => [ + 'client' => 'auto', + 'async_client' => null, + ], ]; $formats = array_map(function ($path) {