diff --git a/CHANGELOG.md b/CHANGELOG.md index 09192a07..aeec856b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,12 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release. -## 1.10.0 (unreleased) - 2018-03-09 +## 1.10.0 - 2018-03-27 ### Added - Allow to configure the `AddPathPlugin` per client, under the `add_path` configuration key. +- Allow to configure clients with a `service` instead of a factory. ## 1.9.0 - 2018-03-06 diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index cb43df30..85f21621 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -179,12 +179,22 @@ private function configureClients(ArrayNodeDefinition $root) }) ->thenInvalid('If you want to use the "config" key you must also specify a valid "factory".') ->end() + ->validate() + ->ifTrue(function ($config) { + return !empty($config['service']) && ('httplug.factory.auto' !== $config['factory'] || !empty($config['config'])); + }) + ->thenInvalid('If you want to use the "service" key you cannot specify "factory" or "config".') + ->end() ->children() ->scalarNode('factory') ->defaultValue('httplug.factory.auto') ->cannotBeEmpty() ->info('The service id of a factory to use when creating the adapter.') ->end() + ->scalarNode('service') + ->defaultNull() + ->info('The service id of the client to use.') + ->end() ->booleanNode('flexible_client') ->defaultFalse() ->info('Set to true to get the client wrapped in a FlexibleHttpClient which emulates async or sync behavior.') diff --git a/DependencyInjection/HttplugExtension.php b/DependencyInjection/HttplugExtension.php index 0186f794..261afefb 100644 --- a/DependencyInjection/HttplugExtension.php +++ b/DependencyInjection/HttplugExtension.php @@ -306,12 +306,16 @@ private function configureClient(ContainerBuilder $container, $clientName, array } } - $container - ->register($serviceId.'.client', HttpClient::class) - ->setFactory([new Reference($arguments['factory']), 'createClient']) - ->addArgument($arguments['config']) - ->setPublic(false) - ; + if (empty($arguments['service'])) { + $container + ->register($serviceId.'.client', HttpClient::class) + ->setFactory([new Reference($arguments['factory']), 'createClient']) + ->addArgument($arguments['config']) + ->setPublic(false); + } else { + $container + ->setAlias($serviceId.'.client', new Alias($arguments['service'], false)); + } $container ->register($serviceId, PluginClient::class) diff --git a/Tests/Unit/DependencyInjection/ConfigurationTest.php b/Tests/Unit/DependencyInjection/ConfigurationTest.php index 52ca3c74..1d7e443f 100644 --- a/Tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/Tests/Unit/DependencyInjection/ConfigurationTest.php @@ -118,6 +118,7 @@ public function testSupportsAllConfigFormats() 'test' => [ 'factory' => 'httplug.factory.guzzle6', 'http_methods_client' => true, + 'service' => null, 'flexible_client' => false, 'batch_client' => false, 'plugins' => [ diff --git a/Tests/Unit/DependencyInjection/HttplugExtensionTest.php b/Tests/Unit/DependencyInjection/HttplugExtensionTest.php index a9f19e32..4d32121d 100644 --- a/Tests/Unit/DependencyInjection/HttplugExtensionTest.php +++ b/Tests/Unit/DependencyInjection/HttplugExtensionTest.php @@ -240,6 +240,21 @@ public function testCachePluginConfigCacheKeyGeneratorReference() $this->assertSame('header_cache_key_generator', (string) $config['cache_key_generator']); } + public function testUsingServiceKeyForClients() + { + $this->load([ + 'clients' => [ + 'acme' => [ + 'service' => 'my_custom_client', + ], + ], + ]); + + $client = $this->container->getAlias('httplug.client.acme.client'); + $this->assertEquals('my_custom_client', (string) $client); + $this->assertFalse($client->isPublic()); + } + private function verifyProfilingDisabled() { $def = $this->container->findDefinition('httplug.client');