Skip to content

Use xml to configure discovered clients #183

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 1 commit into from
Jul 10, 2017
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
124 changes: 16 additions & 108 deletions DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
use Http\Client\Common\FlexibleHttpClient;
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\Plugin\AuthenticationPlugin;
use Http\Discovery\HttpAsyncClientDiscovery;
use Http\Discovery\HttpClientDiscovery;
use Http\HttplugBundle\ClientFactory\DummyClient;
use Http\HttplugBundle\ClientFactory\PluginClientFactory;
use Http\HttplugBundle\Collector\ProfileClientFactory;
use Http\HttplugBundle\Collector\ProfilePlugin;
use Http\Message\Authentication\BasicAuth;
use Http\Message\Authentication\Bearer;
Expand Down Expand Up @@ -359,94 +356,34 @@ private function createUri(ContainerBuilder $container, $serviceId, $uri)
private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config)
{
$httpClient = $config['discovery']['client'];

if (!empty($httpClient)) {
if ($httpClient === 'auto') {
$httpClient = $this->registerAutoDiscoverableClient(
$container,
'auto_discovered_client',
$this->configureAutoDiscoveryFactory(
$container,
HttpClientDiscovery::class,
'auto_discovered_client',
$config
),
$this->isConfigEnabled($container, $config['profiling'])
);
if ($httpClient !== 'auto') {
$container->removeDefinition('httplug.auto_discovery.auto_discovered_client');
$container->removeDefinition('httplug.collector.auto_discovered_client');
$container->removeDefinition('httplug.auto_discovery.auto_discovered_client.plugin');

if (!empty($httpClient)) {
$container->setAlias('httplug.auto_discovery.auto_discovered_client', $httpClient);
$container->getAlias('httplug.auto_discovery.auto_discovered_client')->setPublic(false);
}

$httpClient = new Reference($httpClient);
}

$asyncHttpClient = $config['discovery']['async_client'];

if (!empty($asyncHttpClient)) {
if ($asyncHttpClient === 'auto') {
$asyncHttpClient = $this->registerAutoDiscoverableClient(
$container,
'auto_discovered_async',
$this->configureAutoDiscoveryFactory(
$container,
HttpAsyncClientDiscovery::class,
'auto_discovered_async',
$config
),
$this->isConfigEnabled($container, $config['profiling'])
);
if ($asyncHttpClient !== 'auto') {
$container->removeDefinition('httplug.auto_discovery.auto_discovered_async');
$container->removeDefinition('httplug.collector.auto_discovered_async');
$container->removeDefinition('httplug.auto_discovery.auto_discovered_async.plugin');

if (!empty($asyncHttpClient)) {
$container->setAlias('httplug.auto_discovery.auto_discovered_async', $asyncHttpClient);
$container->getAlias('httplug.auto_discovery.auto_discovered_async')->setPublic(false);
}

$asyncHttpClient = new Reference($asyncHttpClient);
}

if (null === $httpClient && null === $asyncHttpClient) {
$container->removeDefinition('httplug.strategy');

return;
}

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

/**
* Find a client with auto discovery and return a service Reference to it.
*
* @param ContainerBuilder $container
* @param string $name
* @param Reference|callable $factory
* @param bool $profiling
*
* @return string service id
*/
private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory, $profiling)
{
$serviceId = 'httplug.auto_discovery.'.$name;

$plugins = [];
if ($profiling) {
// To profile the requests, add a StackPlugin as first plugin in the chain.
$plugins[] = $this->configureStackPlugin($container, $name, $serviceId);
}

$container
->register($serviceId, DummyClient::class)
->setFactory([PluginClientFactory::class, 'createPluginClient'])
->setArguments([
array_map(
function ($id) {
return new Reference($id);
},
$plugins
),
$factory,
[],
])
;

return $serviceId;
}

/**
Expand Down Expand Up @@ -521,33 +458,4 @@ private function configureStackPlugin(ContainerBuilder $container, $clientName,

return $pluginServiceId;
}

/**
* Configure the discovery factory when profiling is enabled to get client decorated with a ProfileClient.
*
* @param ContainerBuilder $container
* @param string $discovery
* @param string $name
* @param array $config
*
* @return callable|Reference
*/
private function configureAutoDiscoveryFactory(ContainerBuilder $container, $discovery, $name, array $config)
{
$factory = [$discovery, 'find'];
if ($this->isConfigEnabled($container, $config['profiling'])) {
$factoryServiceId = 'httplug.auto_discovery.'.$name.'.factory';
$container->register($factoryServiceId, ProfileClientFactory::class)
->setPublic(false)
->setArguments([
$factory,
new Reference('httplug.collector.collector'),
new Reference('httplug.collector.formatter'),
new Reference('debug.stopwatch'),
]);
$factory = new Reference($factoryServiceId);
}

return $factory;
}
}
36 changes: 36 additions & 0 deletions Resources/config/data-collector.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,42 @@
<tag name="twig.extension" />
</service>

<!-- Discovered clients -->
<service id="httplug.collector.auto_discovered_client" class="Http\HttplugBundle\Collector\ProfileClient" decorates="httplug.auto_discovery.auto_discovered_client" decoration-priority="2" public="false">
<argument type="service" id="httplug.collector.auto_discovered_client.inner"/>
<argument type="service" id="httplug.collector.collector"/>
<argument type="service" id="httplug.collector.formatter"/>
<argument type="service" id="debug.stopwatch"/>
</service>

<service id="httplug.collector.auto_discovered_async" class="Http\HttplugBundle\Collector\ProfileClient" decorates="httplug.auto_discovery.auto_discovered_async" decoration-priority="2" public="false">
<argument type="service" id="httplug.collector.auto_discovered_async.inner"/>
<argument type="service" id="httplug.collector.collector"/>
<argument type="service" id="httplug.collector.formatter"/>
<argument type="service" id="debug.stopwatch"/>
</service>

<service id="httplug.auto_discovery.auto_discovered_client.plugin" class="Http\Client\Common\PluginClient" decorates="httplug.auto_discovery.auto_discovered_client" public="false">
<argument type="service" id="httplug.auto_discovery.auto_discovered_client.plugin.inner"/>
<argument type="collection">
<argument type="service">
<service parent="httplug.plugin.stack">
<argument>auto_discovered_client</argument>
</service>
</argument>
</argument>
</service>
<service id="httplug.auto_discovery.auto_discovered_async.plugin" class="Http\Client\Common\PluginClient" decorates="httplug.auto_discovery.auto_discovered_async" public="false">
<argument type="service" id="httplug.auto_discovery.auto_discovered_async.plugin.inner"/>
<argument type="collection">
<argument type="service">
<service parent="httplug.plugin.stack">
<argument>auto_discovered_async</argument>
</service>
</argument>
</argument>
</service>

<!-- ClientFactories -->
<service id="httplug.collector.factory.auto" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.auto" public="false">
<argument type="service" id="httplug.collector.factory.auto.inner"/>
Expand Down
18 changes: 18 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,27 @@

<services>
<service id="httplug.strategy" class="Http\HttplugBundle\Discovery\ConfiguredClientsStrategy">
<argument type="service" id="httplug.auto_discovery.auto_discovered_client" on-invalid="null"/>
<argument type="service" id="httplug.auto_discovery.auto_discovered_async" on-invalid="null"/>
<tag name="kernel.event_subscriber"/>
</service>

<service id="httplug.auto_discovery.auto_discovered_client" class="Http\Client\HttpClient">
<factory class="Http\Discovery\HttpClientDiscovery" method="find" />
</service>

<service id="httplug.auto_discovery.auto_discovered_async" class="Http\Client\HttpAsyncClient">
<factory class="Http\Discovery\HttpAsyncClientDiscovery" method="find" />
</service>

<!-- Decorate discovered clients with PluginClient -->
<service id="httplug.auto_discovery.auto_discovered_client.plugin" class="Http\Client\Common\PluginClient" decorates="httplug.auto_discovery.auto_discovered_client" decoration-priority="1" public="false">
<argument type="service" id="httplug.auto_discovery.auto_discovered_client.plugin.inner"/>
</service>
<service id="httplug.auto_discovery.auto_discovered_async.plugin" class="Http\Client\Common\PluginClient" decorates="httplug.auto_discovery.auto_discovered_async" decoration-priority="1" public="false">
<argument type="service" id="httplug.auto_discovery.auto_discovered_async.plugin.inner"/>
</service>

<!-- Discovery with autowiring support for Symfony 3.3+ -->
<service id="httplug.message_factory.default" class="Http\Message\MessageFactory">
<factory class="Http\Discovery\MessageFactoryDiscovery" method="find" />
Expand Down