Skip to content

Commit 07c018a

Browse files
committed
Add a discovery strategy (#85)
Adds a new discovery strategy to have discovery find the configured client.
1 parent 4f15660 commit 07c018a

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
@@ -58,6 +58,7 @@ public function load(array $configs, ContainerBuilder $container)
5858
}
5959
}
6060

61+
// Set main aliases
6162
foreach ($config['main_alias'] as $type => $id) {
6263
$container->setAlias(sprintf('httplug.%s', $type), $id);
6364
}
@@ -74,9 +75,12 @@ public function load(array $configs, ContainerBuilder $container)
7475
*/
7576
private function configureClients(ContainerBuilder $container, array $config)
7677
{
78+
// If we have a client named 'default'
7779
$first = isset($config['clients']['default']) ? 'default' : null;
80+
7881
foreach ($config['clients'] as $name => $arguments) {
7982
if ($first === null) {
83+
// Save the name of the first configurated client.
8084
$first = $name;
8185
}
8286

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 <tobias.nyholm@gmail.com>
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
@@ -25,6 +25,7 @@
2525
"php-http/stopwatch-plugin": "^1.0",
2626
"php-http/message": "^1.3",
2727
"symfony/options-resolver": "^2.7|^3.0",
28+
"symfony/event-dispatcher": "^2.7|^3.0",
2829
"symfony/framework-bundle": "^2.7|^3.0",
2930
"php-http/discovery": "^0.9",
3031
"twig/twig": "^1.18|^2.0"

0 commit comments

Comments
 (0)