From 9863d3a8897d138a87831d628c4eb6431340ee8e Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Thu, 24 Jan 2019 09:40:06 +0100 Subject: [PATCH] support autowire convenience clients this only happens for the "default" client: the first configured client or the one called default. --- CHANGELOG.md | 6 ++++++ DependencyInjection/HttplugExtension.php | 27 ++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3a202a2..0206a44d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee ## 1.15.0 [unreleased] +### Added + +- Autowiring support for FlexibleClient, HttpMethodsClientInterface and + BatchClientInterface if they are enabled on the default/first client. + (Only available with Httplug 2) + ### Changed - Removed `twig/twig` dependency diff --git a/DependencyInjection/HttplugExtension.php b/DependencyInjection/HttplugExtension.php index d3ce2bef..dd4b1c85 100644 --- a/DependencyInjection/HttplugExtension.php +++ b/DependencyInjection/HttplugExtension.php @@ -3,8 +3,10 @@ namespace Http\HttplugBundle\DependencyInjection; use Http\Client\Common\BatchClient; +use Http\Client\Common\BatchClientInterface; use Http\Client\Common\FlexibleHttpClient; use Http\Client\Common\HttpMethodsClient; +use Http\Client\Common\HttpMethodsClientInterface; use Http\Client\Common\Plugin\AuthenticationPlugin; use Http\Client\Common\PluginClient; use Http\Client\Common\PluginClientFactory; @@ -111,9 +113,30 @@ private function configureClients(ContainerBuilder $container, array $config) // If we have clients configured if (null !== $first) { // If we do not have a client named 'default' - if (!isset($config['clients']['default'])) { + if (!array_key_exists('default', $config['clients'])) { + $serviceId = 'httplug.client.'.$first; // Alias the first client to httplug.client.default - $container->setAlias('httplug.client.default', 'httplug.client.'.$first); + $container->setAlias('httplug.client.default', $serviceId); + $default = $first; + } else { + $default = 'default'; + } + + // Autowiring alias for special clients, if they are enabled on the default client + if ($config['clients'][$default]['flexible_client']) { + $container->setAlias(FlexibleHttpClient::class, $serviceId.'.flexible'); + } + if ($config['clients'][$default]['http_methods_client']) { + if (\interface_exists(HttpMethodsClientInterface::class)) { + // support for client-common 1.9 + $container->setAlias(HttpMethodsClientInterface::class, $serviceId.'.http_methods'); + } + } + if ($config['clients'][$default]['batch_client']) { + if (\interface_exists(BatchClientInterface::class)) { + // support for client-common 1.9 + $container->setAlias(BatchClientInterface::class, $serviceId.'.batch_client'); + } } } }