Skip to content

Add a discovery strategy #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function load(array $configs, ContainerBuilder $container)
}
}

// Set main aliases
foreach ($config['main_alias'] as $type => $id) {
$container->setAlias(sprintf('httplug.%s', $type), $id);
}
Expand All @@ -69,9 +70,12 @@ public function load(array $configs, ContainerBuilder $container)
*/
private function configureClients(ContainerBuilder $container, array $config)
{
// If we have a client named 'default'
$first = isset($config['clients']['default']) ? 'default' : null;

foreach ($config['clients'] as $name => $arguments) {
if ($first === null) {
// Save the name of the first configurated client.
$first = $name;
}

Expand Down
70 changes: 70 additions & 0 deletions Discovery/ConfiguredClientsStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Http\HttplugBundle\Discovery;

use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\Strategy\DiscoveryStrategy;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* A strategy that provide clients configured with HTTPlug bundle. With help from this strategy
* we can use the web debug toolbar for clients found with the discovery.
*
* @author Tobias Nyholm <[email protected]>
*/
class ConfiguredClientsStrategy implements DiscoveryStrategy, EventSubscriberInterface
{
/**
* @var HttpClient
*/
private static $client;

/**
* @param HttpClient $httpClient
*/
public function __construct(HttpClient $httpClient)
{
static::$client = $httpClient;
}

/**
* {@inheritdoc}
*/
public static function getCandidates($type)
{
if (static::$client !== null && $type == HttpClient::class) {
return [['class' => function () {
return static::$client;
}]];
}

return [];
}

/**
* Make sure to use our custom strategy.
*
* @param Event $e
*/
public function onEvent(Event $e)
{
HttpClientDiscovery::prependStrategy(self::class);
}

/**
* Whenever these events occur we make sure to add our strategy to the discovery.
*
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => ['onEvent', 1024],
ConsoleEvents::COMMAND => ['onEvent', 1024],
];
}
}
4 changes: 4 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="httplug.strategy" class="Http\HttplugBundle\Discovery\ConfiguredClientsStrategy" public="true">
<argument type="service" id="httplug.client"/>
<tag name="kernel.event_subscriber"/>
</service>

<!-- ClientFactories -->
<service id="httplug.factory.buzz" class="Http\HttplugBundle\ClientFactory\BuzzFactory" public="false">
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"php-http/logger-plugin": "^1.0",
"php-http/stopwatch-plugin": "^1.0",
"symfony/options-resolver": "^2.7|^3.0",
"symfony/event-dispatcher": "^2.7|^3.0",
"symfony/framework-bundle": "^2.7|^3.0",
"php-http/discovery": "^0.9"
},
Expand Down