Skip to content

Commit e1710ba

Browse files
authored
Merge pull request #185 from fbourigault/service-locator
use service locator for configured client strategy
2 parents 1b00264 + c5ac6da commit e1710ba

File tree

5 files changed

+123
-22
lines changed

5 files changed

+123
-22
lines changed

DependencyInjection/HttplugExtension.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\DependencyInjection\DefinitionDecorator;
2121
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2222
use Symfony\Component\DependencyInjection\Reference;
23+
use Symfony\Component\DependencyInjection\ServiceLocator;
2324
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
2425

2526
/**
@@ -384,6 +385,14 @@ private function configureAutoDiscoveryClients(ContainerBuilder $container, arra
384385

385386
return;
386387
}
388+
389+
if (!class_exists(ServiceLocator::class)) {
390+
$container->getDefinition('httplug.strategy')->setArguments([
391+
new Reference('service_container'),
392+
in_array($httpClient, ['auto', null], true) ? 'httplug.auto_discovery.auto_discovered_client' : $httpClient,
393+
in_array($asyncHttpClient, ['auto', null], true) ? 'httplug.auto_discovery.auto_discovered_async' : $asyncHttpClient,
394+
]);
395+
}
387396
}
388397

389398
/**

Discovery/ConfiguredClientsStrategy.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Http\Client\HttpAsyncClient;
77
use Http\Discovery\HttpClientDiscovery;
88
use Http\Discovery\Strategy\DiscoveryStrategy;
9+
use Symfony\Component\DependencyInjection\ContainerInterface;
10+
use Symfony\Component\DependencyInjection\ServiceLocator;
911
use Symfony\Component\EventDispatcher\Event;
1012
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1113

@@ -18,39 +20,47 @@
1820
class ConfiguredClientsStrategy implements DiscoveryStrategy, EventSubscriberInterface
1921
{
2022
/**
21-
* @var HttpClient
23+
* @var ServiceLocator|ContainerInterface
24+
*/
25+
private static $locator;
26+
27+
/**
28+
* @var string
2229
*/
2330
private static $client;
2431

2532
/**
26-
* @var HttpAsyncClient
33+
* @var string
2734
*/
2835
private static $asyncClient;
2936

3037
/**
31-
* @param HttpClient $httpClient
32-
* @param HttpAsyncClient $asyncClient
38+
* @param ServiceLocator|ContainerInterface $locator
39+
* @param string $client
40+
* @param string $asyncClient
3341
*/
34-
public function __construct(HttpClient $httpClient = null, HttpAsyncClient $asyncClient = null)
42+
public function __construct($locator, $client, $asyncClient)
3543
{
36-
self::$client = $httpClient;
37-
self::$asyncClient = $asyncClient;
44+
static::$locator = $locator;
45+
static::$client = $client;
46+
static::$asyncClient = $asyncClient;
3847
}
3948

4049
/**
4150
* {@inheritdoc}
4251
*/
4352
public static function getCandidates($type)
4453
{
45-
if ($type === HttpClient::class && self::$client !== null) {
46-
return [['class' => function () {
47-
return self::$client;
54+
$locator = static::$locator;
55+
if ($type === HttpClient::class && $locator->has(static::$client)) {
56+
return [['class' => function () use ($locator) {
57+
return $locator->get(static::$client);
4858
}]];
4959
}
5060

51-
if ($type === HttpAsyncClient::class && self::$asyncClient !== null) {
52-
return [['class' => function () {
53-
return self::$asyncClient;
61+
if ($type === HttpAsyncClient::class && $locator->has(static::$asyncClient)) {
62+
return [['class' => function () use ($locator) {
63+
return $locator->get(static::$asyncClient);
5464
}]];
5565
}
5666

Resources/config/services.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@
44
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
55

66
<services>
7+
<service id="httplug.strategy.locator" class="Symfony\Component\DependencyInjection\ServiceLocator" public="false">
8+
<argument type="collection">
9+
<argument key="httplug.auto_discovery.auto_discovered_client" type="service" id="httplug.auto_discovery.auto_discovered_client" />
10+
<argument key="httplug.auto_discovery.auto_discovered_async" type="service" id="httplug.auto_discovery.auto_discovered_async" />
11+
</argument>
12+
<tag name="container.service_locator" />
13+
</service>
714
<service id="httplug.strategy" class="Http\HttplugBundle\Discovery\ConfiguredClientsStrategy">
8-
<argument type="service" id="httplug.auto_discovery.auto_discovered_client" on-invalid="null"/>
9-
<argument type="service" id="httplug.auto_discovery.auto_discovered_async" on-invalid="null"/>
15+
<argument type="service" id="httplug.strategy.locator" />
16+
<argument>httplug.auto_discovery.auto_discovered_client</argument>
17+
<argument>httplug.auto_discovery.auto_discovered_async</argument>
18+
1019
<tag name="kernel.event_subscriber"/>
1120
</service>
1221

Tests/Functional/DiscoveredClientsTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Http\Client\HttpAsyncClient;
77
use Http\Client\HttpClient;
88
use Http\HttplugBundle\Collector\StackPlugin;
9+
use Http\HttplugBundle\Discovery\ConfiguredClientsStrategy;
910
use Nyholm\NSA;
1011
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1112

@@ -88,10 +89,13 @@ public function testForcedDiscovery()
8889
$this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_async'));
8990
$this->assertTrue($container->has('httplug.strategy'));
9091

91-
$strategy = $container->get('httplug.strategy');
92+
$container->get('httplug.strategy');
9293

93-
$this->assertEquals($container->get('httplug.client.acme'), NSA::getProperty($strategy, 'client'));
94-
$this->assertEquals($container->get('httplug.client.acme'), NSA::getProperty($strategy, 'asyncClient'));
94+
$httpClientCandidate = ConfiguredClientsStrategy::getCandidates(HttpClient::class)[0]['class'];
95+
$httpAsyncClientCandidate = ConfiguredClientsStrategy::getCandidates(HttpAsyncClient::class)[0]['class'];
96+
97+
$this->assertEquals($container->get('httplug.client.acme'), $httpClientCandidate());
98+
$this->assertEquals($container->get('httplug.client.acme'), $httpAsyncClientCandidate());
9599
}
96100

97101
private function getContainer($debug, $environment = 'test')

Tests/Unit/Discovery/ConfiguredClientsStrategyTest.php

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,28 @@
55
use Http\Client\HttpAsyncClient;
66
use Http\Client\HttpClient;
77
use Http\HttplugBundle\Discovery\ConfiguredClientsStrategy;
8+
use Symfony\Component\DependencyInjection\ContainerInterface;
9+
use Symfony\Component\DependencyInjection\ServiceLocator;
810

911
class ConfiguredClientsStrategyTest extends \PHPUnit_Framework_TestCase
1012
{
1113
public function testGetCandidates()
1214
{
1315
$httpClient = $this->getMockBuilder(HttpClient::class)->getMock();
1416
$httpAsyncClient = $this->getMockBuilder(HttpAsyncClient::class)->getMock();
15-
$strategy = new ConfiguredClientsStrategy($httpClient, $httpAsyncClient);
17+
$locator = $this->getLocatorMock();
18+
$locator
19+
->expects($this->exactly(2))
20+
->method('has')
21+
->willReturn(true)
22+
;
23+
$locator
24+
->expects($this->exactly(2))
25+
->method('get')
26+
->will($this->onConsecutiveCalls($httpClient, $httpAsyncClient))
27+
;
28+
29+
$strategy = new ConfiguredClientsStrategy($locator, 'httplug.auto_discovery.auto_discovered_client', 'httplug.auto_discovery.auto_discovered_async');
1630

1731
$candidates = $strategy::getCandidates(HttpClient::class);
1832
$candidate = array_shift($candidates);
@@ -25,7 +39,18 @@ public function testGetCandidates()
2539

2640
public function testGetCandidatesEmpty()
2741
{
28-
$strategy = new ConfiguredClientsStrategy(null, null);
42+
$locator = $this->getLocatorMock();
43+
$locator
44+
->expects($this->exactly(2))
45+
->method('has')
46+
->willReturn(false)
47+
;
48+
$locator
49+
->expects($this->never())
50+
->method('get')
51+
;
52+
53+
$strategy = new ConfiguredClientsStrategy($locator, 'httplug.auto_discovery.auto_discovered_client', 'httplug.auto_discovery.auto_discovered_async');
2954

3055
$candidates = $strategy::getCandidates(HttpClient::class);
3156
$this->assertEquals([], $candidates);
@@ -37,7 +62,23 @@ public function testGetCandidatesEmpty()
3762
public function testGetCandidatesEmptyAsync()
3863
{
3964
$httpClient = $this->getMockBuilder(HttpClient::class)->getMock();
40-
$strategy = new ConfiguredClientsStrategy($httpClient, null);
65+
66+
$locator = $this->getLocatorMock();
67+
$locator
68+
->expects($this->exactly(2))
69+
->method('has')
70+
->willReturnMap([
71+
['httplug.auto_discovery.auto_discovered_client', true],
72+
['httplug.auto_discovery.auto_discovered_async', false],
73+
])
74+
;
75+
$locator
76+
->expects($this->once())
77+
->method('get')
78+
->willReturn($httpClient)
79+
;
80+
81+
$strategy = new ConfiguredClientsStrategy($locator, 'httplug.auto_discovery.auto_discovered_client', 'httplug.auto_discovery.auto_discovered_async');
4182

4283
$candidates = $strategy::getCandidates(HttpClient::class);
4384
$candidate = array_shift($candidates);
@@ -50,7 +91,23 @@ public function testGetCandidatesEmptyAsync()
5091
public function testGetCandidatesEmptySync()
5192
{
5293
$httpAsyncClient = $this->getMockBuilder(HttpAsyncClient::class)->getMock();
53-
$strategy = new ConfiguredClientsStrategy(null, $httpAsyncClient);
94+
95+
$locator = $this->getLocatorMock();
96+
$locator
97+
->expects($this->exactly(2))
98+
->method('has')
99+
->willReturnMap([
100+
['httplug.auto_discovery.auto_discovered_client', false],
101+
['httplug.auto_discovery.auto_discovered_async', true],
102+
])
103+
;
104+
$locator
105+
->expects($this->once())
106+
->method('get')
107+
->willReturn($httpAsyncClient)
108+
;
109+
110+
$strategy = new ConfiguredClientsStrategy($locator, 'httplug.auto_discovery.auto_discovered_client', 'httplug.auto_discovery.auto_discovered_async');
54111

55112
$candidates = $strategy::getCandidates(HttpClient::class);
56113
$this->assertEquals([], $candidates);
@@ -59,4 +116,16 @@ public function testGetCandidatesEmptySync()
59116
$candidate = array_shift($candidates);
60117
$this->assertEquals($httpAsyncClient, $candidate['class']());
61118
}
119+
120+
/**
121+
* @return ContainerInterface|ServiceLocator
122+
*/
123+
private function getLocatorMock()
124+
{
125+
if (class_exists(ServiceLocator::class)) {
126+
return $this->getMockBuilder(ServiceLocator::class)->disableOriginalConstructor()->getMock();
127+
}
128+
129+
return $this->getMockBuilder(ContainerInterface::class)->getMock();
130+
}
62131
}

0 commit comments

Comments
 (0)