|
6 | 6 | use Http\Client\Common\FlexibleHttpClient; |
7 | 7 | use Http\Client\Common\HttpMethodsClient; |
8 | 8 | use Http\Client\Common\Plugin\AuthenticationPlugin; |
9 | | -use Http\Discovery\HttpAsyncClientDiscovery; |
10 | | -use Http\Discovery\HttpClientDiscovery; |
11 | 9 | use Http\HttplugBundle\ClientFactory\DummyClient; |
12 | 10 | use Http\HttplugBundle\ClientFactory\PluginClientFactory; |
13 | | -use Http\HttplugBundle\Collector\ProfileClientFactory; |
14 | 11 | use Http\HttplugBundle\Collector\ProfilePlugin; |
15 | 12 | use Http\Message\Authentication\BasicAuth; |
16 | 13 | use Http\Message\Authentication\Bearer; |
@@ -359,94 +356,34 @@ private function createUri(ContainerBuilder $container, $serviceId, $uri) |
359 | 356 | private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config) |
360 | 357 | { |
361 | 358 | $httpClient = $config['discovery']['client']; |
362 | | - |
363 | | - if (!empty($httpClient)) { |
364 | | - if ($httpClient === 'auto') { |
365 | | - $httpClient = $this->registerAutoDiscoverableClient( |
366 | | - $container, |
367 | | - 'auto_discovered_client', |
368 | | - $this->configureAutoDiscoveryFactory( |
369 | | - $container, |
370 | | - HttpClientDiscovery::class, |
371 | | - 'auto_discovered_client', |
372 | | - $config |
373 | | - ), |
374 | | - $this->isConfigEnabled($container, $config['profiling']) |
375 | | - ); |
| 359 | + if ($httpClient !== 'auto') { |
| 360 | + $container->removeDefinition('httplug.auto_discovery.auto_discovered_client'); |
| 361 | + $container->removeDefinition('httplug.collector.auto_discovered_client'); |
| 362 | + $container->removeDefinition('httplug.auto_discovery.auto_discovered_client.plugin'); |
| 363 | + |
| 364 | + if (!empty($httpClient)) { |
| 365 | + $container->setAlias('httplug.auto_discovery.auto_discovered_client', $httpClient); |
| 366 | + $container->getAlias('httplug.auto_discovery.auto_discovered_client')->setPublic(false); |
376 | 367 | } |
377 | | - |
378 | | - $httpClient = new Reference($httpClient); |
379 | 368 | } |
380 | 369 |
|
381 | 370 | $asyncHttpClient = $config['discovery']['async_client']; |
382 | | - |
383 | | - if (!empty($asyncHttpClient)) { |
384 | | - if ($asyncHttpClient === 'auto') { |
385 | | - $asyncHttpClient = $this->registerAutoDiscoverableClient( |
386 | | - $container, |
387 | | - 'auto_discovered_async', |
388 | | - $this->configureAutoDiscoveryFactory( |
389 | | - $container, |
390 | | - HttpAsyncClientDiscovery::class, |
391 | | - 'auto_discovered_async', |
392 | | - $config |
393 | | - ), |
394 | | - $this->isConfigEnabled($container, $config['profiling']) |
395 | | - ); |
| 371 | + if ($asyncHttpClient !== 'auto') { |
| 372 | + $container->removeDefinition('httplug.auto_discovery.auto_discovered_async'); |
| 373 | + $container->removeDefinition('httplug.collector.auto_discovered_async'); |
| 374 | + $container->removeDefinition('httplug.auto_discovery.auto_discovered_async.plugin'); |
| 375 | + |
| 376 | + if (!empty($asyncHttpClient)) { |
| 377 | + $container->setAlias('httplug.auto_discovery.auto_discovered_async', $asyncHttpClient); |
| 378 | + $container->getAlias('httplug.auto_discovery.auto_discovered_async')->setPublic(false); |
396 | 379 | } |
397 | | - |
398 | | - $asyncHttpClient = new Reference($asyncHttpClient); |
399 | 380 | } |
400 | 381 |
|
401 | 382 | if (null === $httpClient && null === $asyncHttpClient) { |
402 | 383 | $container->removeDefinition('httplug.strategy'); |
403 | 384 |
|
404 | 385 | return; |
405 | 386 | } |
406 | | - |
407 | | - $container |
408 | | - ->getDefinition('httplug.strategy') |
409 | | - ->addArgument($httpClient) |
410 | | - ->addArgument($asyncHttpClient) |
411 | | - ; |
412 | | - } |
413 | | - |
414 | | - /** |
415 | | - * Find a client with auto discovery and return a service Reference to it. |
416 | | - * |
417 | | - * @param ContainerBuilder $container |
418 | | - * @param string $name |
419 | | - * @param Reference|callable $factory |
420 | | - * @param bool $profiling |
421 | | - * |
422 | | - * @return string service id |
423 | | - */ |
424 | | - private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory, $profiling) |
425 | | - { |
426 | | - $serviceId = 'httplug.auto_discovery.'.$name; |
427 | | - |
428 | | - $plugins = []; |
429 | | - if ($profiling) { |
430 | | - // To profile the requests, add a StackPlugin as first plugin in the chain. |
431 | | - $plugins[] = $this->configureStackPlugin($container, $name, $serviceId); |
432 | | - } |
433 | | - |
434 | | - $container |
435 | | - ->register($serviceId, DummyClient::class) |
436 | | - ->setFactory([PluginClientFactory::class, 'createPluginClient']) |
437 | | - ->setArguments([ |
438 | | - array_map( |
439 | | - function ($id) { |
440 | | - return new Reference($id); |
441 | | - }, |
442 | | - $plugins |
443 | | - ), |
444 | | - $factory, |
445 | | - [], |
446 | | - ]) |
447 | | - ; |
448 | | - |
449 | | - return $serviceId; |
450 | 387 | } |
451 | 388 |
|
452 | 389 | /** |
@@ -521,33 +458,4 @@ private function configureStackPlugin(ContainerBuilder $container, $clientName, |
521 | 458 |
|
522 | 459 | return $pluginServiceId; |
523 | 460 | } |
524 | | - |
525 | | - /** |
526 | | - * Configure the discovery factory when profiling is enabled to get client decorated with a ProfileClient. |
527 | | - * |
528 | | - * @param ContainerBuilder $container |
529 | | - * @param string $discovery |
530 | | - * @param string $name |
531 | | - * @param array $config |
532 | | - * |
533 | | - * @return callable|Reference |
534 | | - */ |
535 | | - private function configureAutoDiscoveryFactory(ContainerBuilder $container, $discovery, $name, array $config) |
536 | | - { |
537 | | - $factory = [$discovery, 'find']; |
538 | | - if ($this->isConfigEnabled($container, $config['profiling'])) { |
539 | | - $factoryServiceId = 'httplug.auto_discovery.'.$name.'.factory'; |
540 | | - $container->register($factoryServiceId, ProfileClientFactory::class) |
541 | | - ->setPublic(false) |
542 | | - ->setArguments([ |
543 | | - $factory, |
544 | | - new Reference('httplug.collector.collector'), |
545 | | - new Reference('httplug.collector.formatter'), |
546 | | - new Reference('debug.stopwatch'), |
547 | | - ]); |
548 | | - $factory = new Reference($factoryServiceId); |
549 | | - } |
550 | | - |
551 | | - return $factory; |
552 | | - } |
553 | 461 | } |
0 commit comments