Skip to content

Add configuration option to disable registration of default client #314

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
Mar 21, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
BatchClientInterface if they are enabled on the default/first client.
(Only available with Httplug 2)
- Configuration for the content_type plugin
- Configuration option default_client_autowiring that you can set to false
to prevent autowiring the HttpClient and HttpAsyncClient

### Changed

Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public function getConfigTreeBuilder()
->end()
->fixXmlConfig('client')
->children()
->booleanNode('default_client_autowiring')
->defaultTrue()
->info('Set to false to not autowire HttpClient and HttpAsyncClient.')
->end()
->arrayNode('main_alias')
->addDefaultsIfNotSet()
->info('Configure which service the main alias point to.')
Expand Down
6 changes: 6 additions & 0 deletions src/DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Http\Client\Common\Plugin\AuthenticationPlugin;
use Http\Client\Common\PluginClient;
use Http\Client\Common\PluginClientFactory;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Message\Authentication\BasicAuth;
use Http\Message\Authentication\Bearer;
Expand Down Expand Up @@ -88,6 +89,11 @@ public function load(array $configs, ContainerBuilder $container)
$this->configureClients($container, $config);
$this->configurePlugins($container, $config['plugins']); // must be after clients, as clients.X.plugins might use plugins as templates that will be removed
$this->configureAutoDiscoveryClients($container, $config);

if (!$config['default_client_autowiring']) {
$container->removeAlias(HttpAsyncClient::class);
$container->removeAlias(HttpClient::class);
}
}

/**
Expand Down
37 changes: 37 additions & 0 deletions tests/Functional/DiscoveryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Http\Message\StreamFactory;
use Http\Message\UriFactory;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\ContainerBuilderHasAliasConstraint;
use PHPUnit\Framework\Constraint\LogicalNot;
use Symfony\Component\DependencyInjection\Definition;

/**
Expand Down Expand Up @@ -71,4 +73,39 @@ public function testNoDiscoveryFallbacks()
$clientDefinition = $this->container->getDefinition('httplug.client.default');
$this->assertEquals([HttpClientDiscovery::class, 'find'], $clientDefinition->getFactory());
}

public function testEnableAutowiring()
{
$this->load([
'default_client_autowiring' => true,
]);

$this->assertContainerBuilderHasService('httplug.client.default');
$this->assertContainerBuilderHasService('httplug.async_client.default');
$this->assertContainerBuilderHasAlias(HttpClient::class);
$this->assertContainerBuilderHasAlias(HttpAsyncClient::class);
}

public function testDisableAutowiring()
{
if (PHP_VERSION_ID <= 70000) {
$this->markTestSkipped();
}

$this->load([
'default_client_autowiring' => false,
]);

$this->assertContainerBuilderHasService('httplug.client.default');
$this->assertContainerBuilderHasService('httplug.async_client.default');

self::assertThat(
$this->container,
new LogicalNot(new ContainerBuilderHasAliasConstraint(HttpClient::class))
);
self::assertThat(
$this->container,
new LogicalNot(new ContainerBuilderHasAliasConstraint(HttpAsyncClient::class))
);
}
}
1 change: 1 addition & 0 deletions tests/Resources/Fixtures/config/full.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

$container->loadFromExtension('httplug', [
'default_client_autowiring' => false,
'main_alias' => [
'client' => 'my_client',
'message_factory' => 'my_message_factory',
Expand Down
1 change: 1 addition & 0 deletions tests/Resources/Fixtures/config/full.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<container xmlns="http://symfony.com/schema/dic/services">

<config xmlns="http://example.org/schema/dic/httplug">
<default-client-autowiring>false</default-client-autowiring>
<main-alias>
<client>my_client</client>
<message-factory>my_message_factory</message-factory>
Expand Down
1 change: 1 addition & 0 deletions tests/Resources/Fixtures/config/full.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
httplug:
default_client_autowiring: false
main_alias:
client: my_client
message_factory: my_message_factory
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class ConfigurationTest extends AbstractExtensionConfigurationTestCase
{
private $emptyConfig = [
'default_client_autowiring' => true,
'main_alias' => [
'client' => 'httplug.client.default',
'message_factory' => 'httplug.message_factory.default',
Expand Down Expand Up @@ -102,6 +103,7 @@ public function testEmptyConfiguration()
public function testSupportsAllConfigFormats()
{
$expectedConfiguration = [
'default_client_autowiring' => false,
'main_alias' => [
'client' => 'my_client',
'message_factory' => 'my_message_factory',
Expand Down