Skip to content

Add config for enable FlexibleHttpClient and HttpMethodsClient #83

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 4 commits into from
Jun 30, 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
17 changes: 17 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ protected function configureClients(ArrayNodeDefinition $root)
{
$root->children()
->arrayNode('clients')
->validate()
->ifTrue(function ($clients) {
foreach ($clients as $name => $config) {
return $config['flexible_client'] && $config['http_methods_client'];
}

return false;
})
->thenInvalid('A http client can\'t be decorated with both FlexibleHttpClient and HttpMethodsClient. Only one of the following options can be true. ("flexible_client", "http_methods_client")')->end()
->useAttributeAsKey('name')
->prototype('array')
->children()
Expand All @@ -102,6 +111,14 @@ protected function configureClients(ArrayNodeDefinition $root)
->cannotBeEmpty()
->info('The service id of a factory to use when creating the adapter.')
->end()
->booleanNode('flexible_client')
->defaultFalse()
->info('Set to true to get the client wrapped in a FlexibleHttpClient which emulates async or sync behavior.')
->end()
->booleanNode('http_methods_client')
->defaultFalse()
->info('Set to true to get the client wrapped in a HttpMethodsClient which emulates provides functions for HTTP verbs.')
->end()
->arrayNode('plugins')
->info('A list of service ids of plugins. The order is important.')
->prototype('scalar')->end()
Expand Down
63 changes: 50 additions & 13 deletions DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Http\HttplugBundle\DependencyInjection;

use Http\Client\Common\FlexibleHttpClient;
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\Plugin\AuthenticationPlugin;
use Http\Client\Common\PluginClient;
use Http\HttplugBundle\ClientFactory\DummyClient;
Expand Down Expand Up @@ -77,19 +79,7 @@ private function configureClients(ContainerBuilder $container, array $config)
array_unshift($arguments['plugins'], 'httplug.collector.history_plugin');
}

$def = $container->register('httplug.client.'.$name, DummyClient::class);

if (empty($arguments['plugins'])) {
$def->setFactory([new Reference($arguments['factory']), 'createClient'])
->addArgument($arguments['config']);
} else {
$def->setFactory('Http\HttplugBundle\ClientFactory\PluginClientFactory::createPluginClient')
->addArgument(array_map(function ($id) {
return new Reference($id);
}, $arguments['plugins']))
->addArgument(new Reference($arguments['factory']))
->addArgument($arguments['config']);
}
$this->configureClient($container, $name, $arguments);
}

// If we have clients configured
Expand Down Expand Up @@ -206,4 +196,51 @@ private function configureAuthentication(ContainerBuilder $container, array $con
->addArgument(new Reference($authServiceKey));
}
}

/**
* @param ContainerBuilder $container
* @param string $name
* @param array $arguments
*/
private function configureClient(ContainerBuilder $container, $name, array $arguments)
{
$serviceId = 'httplug.client.'.$name;
$def = $container->register($serviceId, DummyClient::class);

if (empty($arguments['plugins'])) {
$def->setFactory([new Reference($arguments['factory']), 'createClient'])
->addArgument($arguments['config']);
} else {
$def->setFactory('Http\HttplugBundle\ClientFactory\PluginClientFactory::createPluginClient')
->addArgument(
array_map(
function ($id) {
return new Reference($id);
},
$arguments['plugins']
)
)
->addArgument(new Reference($arguments['factory']))
->addArgument($arguments['config']);
}


/*
* Decorate the client with clients from client-common
*/

if ($arguments['flexible_client']) {
$container->register($serviceId.'.flexible', FlexibleHttpClient::class)
->addArgument(new Reference($serviceId.'.flexible.inner'))
->setPublic(false)
->setDecoratedService($serviceId);
}

if ($arguments['http_methods_client']) {
$container->register($serviceId.'.http_methods', HttpMethodsClient::class)
->setArguments([new Reference($serviceId.'.http_methods.inner'), new Reference('httplug.message_factory')])
->setPublic(false)
->setDecoratedService($serviceId);
}
}
}