Skip to content

Discovery bugfixes and more flexible. #87

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 9 commits into from
Jul 18, 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
6 changes: 5 additions & 1 deletion Collector/PluginJournal.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ final class PluginJournal
*/
public function getPlugins($clientName)
{
return $this->data[$clientName];
if (isset($this->data[$clientName])) {
return $this->data[$clientName];
}

return [];
}

/**
Expand Down
14 changes: 14 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('discovery')
->addDefaultsIfNotSet()
->info('Control what clients should be found by the discovery.')
->children()
->scalarNode('client')
->defaultValue('auto')
->info('Set to "auto" to see auto discovered client in the web profiler. If provided a service id for a client then this client will be found by auto discovery.')
->end()
->scalarNode('async_client')
->defaultNull()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not auto as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because if no AsyncClient is found we get an exception. I do not think I can catch that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay. but then lets change the info to mention that you should set this to auto if you know that you have an async client available.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or should I put that in the docs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

->info('Set to "auto" to see auto discovered client in the web profiler. If provided a service id for a client then this client will be found by auto discovery.')
->end()
->end()
->end()
->end();

return $treeBuilder;
Expand Down
51 changes: 51 additions & 0 deletions DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\Plugin\AuthenticationPlugin;
use Http\Client\Common\PluginClient;
use Http\Discovery\HttpAsyncClientDiscovery;
use Http\HttplugBundle\ClientFactory\DummyClient;
use Http\HttplugBundle\Collector\DebugPlugin;
use Http\Message\Authentication\BasicAuth;
Expand Down Expand Up @@ -65,6 +66,7 @@ public function load(array $configs, ContainerBuilder $container)

$this->configurePlugins($container, $config['plugins']);
$this->configureClients($container, $config);
$this->configureAutoDiscoveryClients($container, $config);
}

/**
Expand Down Expand Up @@ -278,4 +280,53 @@ private function registerDebugPlugin(ContainerBuilder $container, $name)

return $serviceIdDebugPlugin;
}

/**
* Make sure we inject the debug plugin for clients found by auto discovery.
*
* @param ContainerBuilder $container
* @param array $config
*/
private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config)
{
$httpClient = $config['discovery']['client'];
if ($httpClient === 'auto') {
$httpClient = $this->registerAutoDiscoverableClientWithDebugPlugin($container, 'client');
} elseif ($httpClient) {
$httpClient = new Reference($httpClient);
}

$asyncHttpClient = $config['discovery']['async_client'];
if ($asyncHttpClient === 'auto') {
$asyncHttpClient = $this->registerAutoDiscoverableClientWithDebugPlugin($container, 'async_client');
} elseif ($asyncHttpClient) {
$asyncHttpClient = new Reference($httpClient);
}

$container->getDefinition('httplug.strategy')
->addArgument($httpClient)
->addArgument($asyncHttpClient);
}

/**
* @param ContainerBuilder $container
* @param $name
*
* @return Reference
*/
private function registerAutoDiscoverableClientWithDebugPlugin(ContainerBuilder $container, $name)
{
$definition = $container->register('httplug.auto_discovery_'.$name.'.pure', DummyClient::class);
$definition->setPublic(false);
$definition->setFactory([HttpAsyncClientDiscovery::class, 'find']);

$serviceIdDebugPlugin = $this->registerDebugPlugin($container, 'auto_discovery_'.$name);
$container->register('httplug.auto_discovery_'.$name.'.plugin', PluginClient::class)
->setPublic(false)
->addArgument(new Reference('httplug.auto_discovery_'.$name.'.pure'))
->addArgument([])
->addArgument(['debug_plugins' => [new Reference($serviceIdDebugPlugin)]]);

return new Reference('httplug.auto_discovery_'.$name.'.plugin');
}
}
20 changes: 17 additions & 3 deletions Discovery/ConfiguredClientsStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Http\HttplugBundle\Discovery;

use Http\Client\HttpClient;
use Http\Client\HttpAsyncClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\Strategy\DiscoveryStrategy;
use Symfony\Component\Console\ConsoleEvents;
Expand All @@ -24,24 +25,37 @@ class ConfiguredClientsStrategy implements DiscoveryStrategy, EventSubscriberInt
private static $client;

/**
* @param HttpClient $httpClient
* @var HttpAsyncClient
*/
public function __construct(HttpClient $httpClient)
private static $asyncClient;

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

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

if ($type === HttpAsyncClient::class && static::$asyncClient !== null) {
return [['class' => function () {
return static::$asyncClient;
}]];
}

return [];
}

Expand Down
3 changes: 1 addition & 2 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
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"/>
<service id="httplug.strategy" class="Http\HttplugBundle\Discovery\ConfiguredClientsStrategy">
<tag name="kernel.event_subscriber"/>
</service>

Expand Down
8 changes: 8 additions & 0 deletions Tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public function testEmptyConfiguration()
'stopwatch' => 'debug.stopwatch',
],
],
'discovery' => [
'client' => 'auto',
'async_client' => null,
],
];

$formats = array_map(function ($path) {
Expand Down Expand Up @@ -178,6 +182,10 @@ public function testSupportsAllConfigFormats()
'stopwatch' => 'debug.stopwatch',
],
],
'discovery' => [
'client' => 'auto',
'async_client' => null,
],
];

$formats = array_map(function ($path) {
Expand Down