Skip to content

support autowire convenience clients #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 25 additions & 2 deletions DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
}
}
}
Expand Down