Skip to content

Commit 8b7cb00

Browse files
dbuNyholm
authored andcommitted
support autowire convenience clients (#309)
this only happens for the "default" client: the first configured client or the one called default.
1 parent f718e56 commit 8b7cb00

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
44

55
## 1.15.0 [unreleased]
66

7+
### Added
8+
9+
- Autowiring support for FlexibleClient, HttpMethodsClientInterface and
10+
BatchClientInterface if they are enabled on the default/first client.
11+
(Only available with Httplug 2)
12+
713
### Changed
814

915
- Removed `twig/twig` dependency

DependencyInjection/HttplugExtension.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace Http\HttplugBundle\DependencyInjection;
44

55
use Http\Client\Common\BatchClient;
6+
use Http\Client\Common\BatchClientInterface;
67
use Http\Client\Common\FlexibleHttpClient;
78
use Http\Client\Common\HttpMethodsClient;
9+
use Http\Client\Common\HttpMethodsClientInterface;
810
use Http\Client\Common\Plugin\AuthenticationPlugin;
911
use Http\Client\Common\PluginClient;
1012
use Http\Client\Common\PluginClientFactory;
@@ -111,9 +113,30 @@ private function configureClients(ContainerBuilder $container, array $config)
111113
// If we have clients configured
112114
if (null !== $first) {
113115
// If we do not have a client named 'default'
114-
if (!isset($config['clients']['default'])) {
116+
if (!array_key_exists('default', $config['clients'])) {
117+
$serviceId = 'httplug.client.'.$first;
115118
// Alias the first client to httplug.client.default
116-
$container->setAlias('httplug.client.default', 'httplug.client.'.$first);
119+
$container->setAlias('httplug.client.default', $serviceId);
120+
$default = $first;
121+
} else {
122+
$default = 'default';
123+
}
124+
125+
// Autowiring alias for special clients, if they are enabled on the default client
126+
if ($config['clients'][$default]['flexible_client']) {
127+
$container->setAlias(FlexibleHttpClient::class, $serviceId.'.flexible');
128+
}
129+
if ($config['clients'][$default]['http_methods_client']) {
130+
if (\interface_exists(HttpMethodsClientInterface::class)) {
131+
// support for client-common 1.9
132+
$container->setAlias(HttpMethodsClientInterface::class, $serviceId.'.http_methods');
133+
}
134+
}
135+
if ($config['clients'][$default]['batch_client']) {
136+
if (\interface_exists(BatchClientInterface::class)) {
137+
// support for client-common 1.9
138+
$container->setAlias(BatchClientInterface::class, $serviceId.'.batch_client');
139+
}
117140
}
118141
}
119142
}

0 commit comments

Comments
 (0)