Skip to content

Commit c91eef5

Browse files
authored
Allow you to configure a client without a factory (#257)
* Allow you to configure a client without a factory * cs * Fixed tests * Update HttplugExtension.php * Update HttplugExtensionTest.php * FIxed typo * Added changelog * Removed date
1 parent 5c4db94 commit c91eef5

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
44

5-
## 1.10.0 (unreleased) - 2018-03-09
5+
## 1.10.0 - 2018-03-27
66

77
### Added
88

99
- Allow to configure the `AddPathPlugin` per client, under the `add_path` configuration key.
10+
- Allow to configure clients with a `service` instead of a factory.
1011

1112
## 1.9.0 - 2018-03-06
1213

DependencyInjection/Configuration.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,22 @@ private function configureClients(ArrayNodeDefinition $root)
179179
})
180180
->thenInvalid('If you want to use the "config" key you must also specify a valid "factory".')
181181
->end()
182+
->validate()
183+
->ifTrue(function ($config) {
184+
return !empty($config['service']) && ('httplug.factory.auto' !== $config['factory'] || !empty($config['config']));
185+
})
186+
->thenInvalid('If you want to use the "service" key you cannot specify "factory" or "config".')
187+
->end()
182188
->children()
183189
->scalarNode('factory')
184190
->defaultValue('httplug.factory.auto')
185191
->cannotBeEmpty()
186192
->info('The service id of a factory to use when creating the adapter.')
187193
->end()
194+
->scalarNode('service')
195+
->defaultNull()
196+
->info('The service id of the client to use.')
197+
->end()
188198
->booleanNode('flexible_client')
189199
->defaultFalse()
190200
->info('Set to true to get the client wrapped in a FlexibleHttpClient which emulates async or sync behavior.')

DependencyInjection/HttplugExtension.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,16 @@ private function configureClient(ContainerBuilder $container, $clientName, array
306306
}
307307
}
308308

309-
$container
310-
->register($serviceId.'.client', HttpClient::class)
311-
->setFactory([new Reference($arguments['factory']), 'createClient'])
312-
->addArgument($arguments['config'])
313-
->setPublic(false)
314-
;
309+
if (empty($arguments['service'])) {
310+
$container
311+
->register($serviceId.'.client', HttpClient::class)
312+
->setFactory([new Reference($arguments['factory']), 'createClient'])
313+
->addArgument($arguments['config'])
314+
->setPublic(false);
315+
} else {
316+
$container
317+
->setAlias($serviceId.'.client', new Alias($arguments['service'], false));
318+
}
315319

316320
$container
317321
->register($serviceId, PluginClient::class)

Tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public function testSupportsAllConfigFormats()
118118
'test' => [
119119
'factory' => 'httplug.factory.guzzle6',
120120
'http_methods_client' => true,
121+
'service' => null,
121122
'flexible_client' => false,
122123
'batch_client' => false,
123124
'plugins' => [

Tests/Unit/DependencyInjection/HttplugExtensionTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,21 @@ public function testCachePluginConfigCacheKeyGeneratorReference()
240240
$this->assertSame('header_cache_key_generator', (string) $config['cache_key_generator']);
241241
}
242242

243+
public function testUsingServiceKeyForClients()
244+
{
245+
$this->load([
246+
'clients' => [
247+
'acme' => [
248+
'service' => 'my_custom_client',
249+
],
250+
],
251+
]);
252+
253+
$client = $this->container->getAlias('httplug.client.acme.client');
254+
$this->assertEquals('my_custom_client', (string) $client);
255+
$this->assertFalse($client->isPublic());
256+
}
257+
243258
private function verifyProfilingDisabled()
244259
{
245260
$def = $this->container->findDefinition('httplug.client');

0 commit comments

Comments
 (0)