Skip to content

Commit 91f885c

Browse files
Nyholmdbu
authored andcommitted
Add a discovery strategy (#85)
Adds a new discovery strategy to have discovery find the configured client.
1 parent be6f9f5 commit 91f885c

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

DependencyInjection/HttplugExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function load(array $configs, ContainerBuilder $container)
5353
}
5454
}
5555

56+
// Set main aliases
5657
foreach ($config['main_alias'] as $type => $id) {
5758
$container->setAlias(sprintf('httplug.%s', $type), $id);
5859
}
@@ -69,9 +70,12 @@ public function load(array $configs, ContainerBuilder $container)
6970
*/
7071
private function configureClients(ContainerBuilder $container, array $config)
7172
{
73+
// If we have a client named 'default'
7274
$first = isset($config['clients']['default']) ? 'default' : null;
75+
7376
foreach ($config['clients'] as $name => $arguments) {
7477
if ($first === null) {
78+
// Save the name of the first configurated client.
7579
$first = $name;
7680
}
7781

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\Discovery;
4+
5+
use Http\Client\HttpClient;
6+
use Http\Discovery\HttpClientDiscovery;
7+
use Http\Discovery\Strategy\DiscoveryStrategy;
8+
use Symfony\Component\Console\ConsoleEvents;
9+
use Symfony\Component\EventDispatcher\Event;
10+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11+
use Symfony\Component\HttpKernel\KernelEvents;
12+
13+
/**
14+
* A strategy that provide clients configured with HTTPlug bundle. With help from this strategy
15+
* we can use the web debug toolbar for clients found with the discovery.
16+
*
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
19+
class ConfiguredClientsStrategy implements DiscoveryStrategy, EventSubscriberInterface
20+
{
21+
/**
22+
* @var HttpClient
23+
*/
24+
private static $client;
25+
26+
/**
27+
* @param HttpClient $httpClient
28+
*/
29+
public function __construct(HttpClient $httpClient)
30+
{
31+
static::$client = $httpClient;
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public static function getCandidates($type)
38+
{
39+
if (static::$client !== null && $type == HttpClient::class) {
40+
return [['class' => function () {
41+
return static::$client;
42+
}]];
43+
}
44+
45+
return [];
46+
}
47+
48+
/**
49+
* Make sure to use our custom strategy.
50+
*
51+
* @param Event $e
52+
*/
53+
public function onEvent(Event $e)
54+
{
55+
HttpClientDiscovery::prependStrategy(self::class);
56+
}
57+
58+
/**
59+
* Whenever these events occur we make sure to add our strategy to the discovery.
60+
*
61+
* {@inheritdoc}
62+
*/
63+
public static function getSubscribedEvents()
64+
{
65+
return [
66+
KernelEvents::REQUEST => ['onEvent', 1024],
67+
ConsoleEvents::COMMAND => ['onEvent', 1024],
68+
];
69+
}
70+
}

Resources/config/services.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
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" class="Http\HttplugBundle\Discovery\ConfiguredClientsStrategy" public="true">
8+
<argument type="service" id="httplug.client"/>
9+
<tag name="kernel.event_subscriber"/>
10+
</service>
711

812
<!-- ClientFactories -->
913
<service id="httplug.factory.buzz" class="Http\HttplugBundle\ClientFactory\BuzzFactory" public="false">

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"php-http/logger-plugin": "^1.0",
2525
"php-http/stopwatch-plugin": "^1.0",
2626
"symfony/options-resolver": "^2.7|^3.0",
27+
"symfony/event-dispatcher": "^2.7|^3.0",
2728
"symfony/framework-bundle": "^2.7|^3.0",
2829
"php-http/discovery": "^0.9"
2930
},

0 commit comments

Comments
 (0)