Skip to content

Commit d12e1f0

Browse files
committed
Make it possible to create public httplug clients
1 parent 853c4f6 commit d12e1f0

File tree

5 files changed

+140
-4
lines changed

5 files changed

+140
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

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

5+
## 1.12.0 - 2018-10-24
6+
7+
### Added
8+
9+
- Add configuration option to create public clients
10+
511
## 1.11.0 - 2018-07-07
612

713
### Added

DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ private function configureClients(ArrayNodeDefinition $root)
195195
->defaultNull()
196196
->info('The service id of the client to use.')
197197
->end()
198+
->booleanNode('public')
199+
->defaultNull()
200+
->info('Set to true if you really cannot use dependency injection and need to make the client service public.')
201+
->end()
198202
->booleanNode('flexible_client')
199203
->defaultFalse()
200204
->info('Set to true to get the client wrapped in a FlexibleHttpClient which emulates async or sync behavior.')

DependencyInjection/HttplugExtension.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ private function configureClient(ContainerBuilder $container, $clientName, array
323323
->setAlias($serviceId.'.client', new Alias($arguments['service'], false));
324324
}
325325

326-
$container
326+
$definition = $container
327327
->register($serviceId, PluginClient::class)
328328
->setFactory([new Reference(PluginClientFactory::class), 'createClient'])
329329
->addArgument(new Reference($serviceId.'.client'))
@@ -340,14 +340,18 @@ function ($id) {
340340
])
341341
;
342342

343+
if (is_bool($arguments['public'])) {
344+
$definition->setPublic($arguments['public']);
345+
}
346+
343347
/*
344348
* Decorate the client with clients from client-common
345349
*/
346350
if ($arguments['flexible_client']) {
347351
$container
348352
->register($serviceId.'.flexible', FlexibleHttpClient::class)
349353
->addArgument(new Reference($serviceId.'.flexible.inner'))
350-
->setPublic(false)
354+
->setPublic($arguments['public'] ? true : false)
351355
->setDecoratedService($serviceId)
352356
;
353357
}
@@ -356,7 +360,7 @@ function ($id) {
356360
$container
357361
->register($serviceId.'.http_methods', HttpMethodsClient::class)
358362
->setArguments([new Reference($serviceId.'.http_methods.inner'), new Reference('httplug.message_factory')])
359-
->setPublic(false)
363+
->setPublic($arguments['public'] ? true : false)
360364
->setDecoratedService($serviceId)
361365
;
362366
}
@@ -365,7 +369,7 @@ function ($id) {
365369
$container
366370
->register($serviceId.'.batch_client', BatchClient::class)
367371
->setArguments([new Reference($serviceId.'.batch_client.inner')])
368-
->setPublic(false)
372+
->setPublic($arguments['public'] ? true : false)
369373
->setDecoratedService($serviceId)
370374
;
371375
}

Tests/Unit/DependencyInjection/ConfigurationTest.php

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

Tests/Unit/DependencyInjection/HttplugExtensionTest.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Http\HttplugBundle\DependencyInjection\HttplugExtension;
77
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
88
use Symfony\Component\DependencyInjection\Reference;
9+
use Symfony\Component\HttpKernel\Kernel;
910

1011
/**
1112
* @author David Buchmann <mail@davidbu.ch>
@@ -267,4 +268,124 @@ private function verifyProfilingDisabled()
267268
);
268269
}
269270
}
271+
272+
public function testClientShouldBePrivateByDefault()
273+
{
274+
$this->load([
275+
'clients' => [
276+
'acme' => [],
277+
],
278+
]);
279+
280+
$defaultVisibilityForSymfonyServices = true;
281+
282+
// Symfony made services private by default starting from 3.4
283+
if (version_compare(Kernel::VERSION, '3.4', '>=')) {
284+
$defaultVisibilityForSymfonyServices = false;
285+
}
286+
287+
$this->assertContainerBuilderHasService('httplug.client.acme');
288+
$this->assertSame($defaultVisibilityForSymfonyServices, $this->container->getDefinition('httplug.client.acme')->isPublic());
289+
}
290+
291+
public function testFlexibleClientShouldBePrivateByDefault()
292+
{
293+
$this->load([
294+
'clients' => [
295+
'acme' => [
296+
'flexible_client' => true,
297+
],
298+
],
299+
]);
300+
301+
$this->assertContainerBuilderHasService('httplug.client.acme');
302+
$this->assertFalse($this->container->getDefinition('httplug.client.acme.flexible')->isPublic());
303+
}
304+
305+
public function testHttpMethodsClientShouldBePrivateByDefault()
306+
{
307+
$this->load([
308+
'clients' => [
309+
'acme' => [
310+
'http_methods_client' => true,
311+
],
312+
],
313+
]);
314+
315+
$this->assertContainerBuilderHasService('httplug.client.acme');
316+
$this->assertFalse($this->container->getDefinition('httplug.client.acme.http_methods')->isPublic());
317+
}
318+
319+
public function testBatchClientShouldBePrivateByDefault()
320+
{
321+
$this->load([
322+
'clients' => [
323+
'acme' => [
324+
'batch_client' => true,
325+
],
326+
],
327+
]);
328+
329+
$this->assertContainerBuilderHasService('httplug.client.acme');
330+
$this->assertFalse($this->container->getDefinition('httplug.client.acme.batch_client')->isPublic());
331+
}
332+
333+
public function testClientCanBePublic()
334+
{
335+
$this->load([
336+
'clients' => [
337+
'acme' => [
338+
'public' => true,
339+
],
340+
],
341+
]);
342+
343+
$this->assertContainerBuilderHasService('httplug.client.acme');
344+
$this->assertTrue($this->container->getDefinition('httplug.client.acme')->isPublic());
345+
}
346+
347+
public function testFlexibleClientCanBePublic()
348+
{
349+
$this->load([
350+
'clients' => [
351+
'acme' => [
352+
'public' => true,
353+
'flexible_client' => true,
354+
],
355+
],
356+
]);
357+
358+
$this->assertContainerBuilderHasService('httplug.client.acme');
359+
$this->assertTrue($this->container->getDefinition('httplug.client.acme.flexible')->isPublic());
360+
}
361+
362+
public function testHttpMethodsClientCanBePublic()
363+
{
364+
$this->load([
365+
'clients' => [
366+
'acme' => [
367+
'public' => true,
368+
'http_methods_client' => true,
369+
],
370+
],
371+
]);
372+
373+
$this->assertContainerBuilderHasService('httplug.client.acme');
374+
$this->assertTrue($this->container->getDefinition('httplug.client.acme.http_methods')->isPublic());
375+
}
376+
377+
public function testBatchClientCanBePublic()
378+
{
379+
$this->load([
380+
'clients' => [
381+
'acme' => [
382+
'public' => true,
383+
'batch_client' => true,
384+
],
385+
],
386+
]);
387+
388+
$this->assertContainerBuilderHasService('httplug.client.acme');
389+
$this->assertTrue($this->container->getDefinition('httplug.client.acme.batch_client')->isPublic());
390+
}
270391
}

0 commit comments

Comments
 (0)