Skip to content

Commit 2fcedcc

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

File tree

5 files changed

+141
-4
lines changed

5 files changed

+141
-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' => null,
122123
'flexible_client' => false,
123124
'batch_client' => false,
124125
'plugins' => [

Tests/Unit/DependencyInjection/HttplugExtensionTest.php

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

0 commit comments

Comments
 (0)