|  | 
| 6 | 6 | use Http\Client\Common\HttpMethodsClient; | 
| 7 | 7 | use Http\Client\Common\Plugin\AuthenticationPlugin; | 
| 8 | 8 | use Http\Client\Common\PluginClient; | 
|  | 9 | +use Http\Discovery\HttpAsyncClientDiscovery; | 
| 9 | 10 | use Http\HttplugBundle\ClientFactory\DummyClient; | 
| 10 | 11 | use Http\HttplugBundle\Collector\DebugPlugin; | 
| 11 | 12 | use Http\Message\Authentication\BasicAuth; | 
| @@ -65,6 +66,7 @@ public function load(array $configs, ContainerBuilder $container) | 
| 65 | 66 | 
 | 
| 66 | 67 |         $this->configurePlugins($container, $config['plugins']); | 
| 67 | 68 |         $this->configureClients($container, $config); | 
|  | 69 | +        $this->configureAutoDiscoveryClients($container, $config); | 
| 68 | 70 |     } | 
| 69 | 71 | 
 | 
| 70 | 72 |     /** | 
| @@ -278,4 +280,53 @@ private function registerDebugPlugin(ContainerBuilder $container, $name) | 
| 278 | 280 | 
 | 
| 279 | 281 |         return $serviceIdDebugPlugin; | 
| 280 | 282 |     } | 
|  | 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 | +    } | 
| 281 | 332 | } | 
0 commit comments