Skip to content

Commit f56531b

Browse files
Nyholmdbu
authored andcommitted
Add config for enable FlexibleHttpClient and HttpMethodsClient (#83)
* Add config for enable FlexibleHttpClient and HttpMethodsClient
1 parent b178535 commit f56531b

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed

DependencyInjection/Configuration.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ protected function configureClients(ArrayNodeDefinition $root)
9494
{
9595
$root->children()
9696
->arrayNode('clients')
97+
->validate()
98+
->ifTrue(function ($clients) {
99+
foreach ($clients as $name => $config) {
100+
return $config['flexible_client'] && $config['http_methods_client'];
101+
}
102+
103+
return false;
104+
})
105+
->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()
97106
->useAttributeAsKey('name')
98107
->prototype('array')
99108
->children()
@@ -102,6 +111,14 @@ protected function configureClients(ArrayNodeDefinition $root)
102111
->cannotBeEmpty()
103112
->info('The service id of a factory to use when creating the adapter.')
104113
->end()
114+
->booleanNode('flexible_client')
115+
->defaultFalse()
116+
->info('Set to true to get the client wrapped in a FlexibleHttpClient which emulates async or sync behavior.')
117+
->end()
118+
->booleanNode('http_methods_client')
119+
->defaultFalse()
120+
->info('Set to true to get the client wrapped in a HttpMethodsClient which emulates provides functions for HTTP verbs.')
121+
->end()
105122
->arrayNode('plugins')
106123
->info('A list of service ids of plugins. The order is important.')
107124
->prototype('scalar')->end()

DependencyInjection/HttplugExtension.php

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Http\HttplugBundle\DependencyInjection;
44

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

80-
$def = $container->register('httplug.client.'.$name, DummyClient::class);
81-
82-
if (empty($arguments['plugins'])) {
83-
$def->setFactory([new Reference($arguments['factory']), 'createClient'])
84-
->addArgument($arguments['config']);
85-
} else {
86-
$def->setFactory('Http\HttplugBundle\ClientFactory\PluginClientFactory::createPluginClient')
87-
->addArgument(array_map(function ($id) {
88-
return new Reference($id);
89-
}, $arguments['plugins']))
90-
->addArgument(new Reference($arguments['factory']))
91-
->addArgument($arguments['config']);
92-
}
82+
$this->configureClient($container, $name, $arguments);
9383
}
9484

9585
// If we have clients configured
@@ -206,4 +196,51 @@ private function configureAuthentication(ContainerBuilder $container, array $con
206196
->addArgument(new Reference($authServiceKey));
207197
}
208198
}
199+
200+
/**
201+
* @param ContainerBuilder $container
202+
* @param string $name
203+
* @param array $arguments
204+
*/
205+
private function configureClient(ContainerBuilder $container, $name, array $arguments)
206+
{
207+
$serviceId = 'httplug.client.'.$name;
208+
$def = $container->register($serviceId, DummyClient::class);
209+
210+
if (empty($arguments['plugins'])) {
211+
$def->setFactory([new Reference($arguments['factory']), 'createClient'])
212+
->addArgument($arguments['config']);
213+
} else {
214+
$def->setFactory('Http\HttplugBundle\ClientFactory\PluginClientFactory::createPluginClient')
215+
->addArgument(
216+
array_map(
217+
function ($id) {
218+
return new Reference($id);
219+
},
220+
$arguments['plugins']
221+
)
222+
)
223+
->addArgument(new Reference($arguments['factory']))
224+
->addArgument($arguments['config']);
225+
}
226+
227+
228+
/*
229+
* Decorate the client with clients from client-common
230+
*/
231+
232+
if ($arguments['flexible_client']) {
233+
$container->register($serviceId.'.flexible', FlexibleHttpClient::class)
234+
->addArgument(new Reference($serviceId.'.flexible.inner'))
235+
->setPublic(false)
236+
->setDecoratedService($serviceId);
237+
}
238+
239+
if ($arguments['http_methods_client']) {
240+
$container->register($serviceId.'.http_methods', HttpMethodsClient::class)
241+
->setArguments([new Reference($serviceId.'.http_methods.inner'), new Reference('httplug.message_factory')])
242+
->setPublic(false)
243+
->setDecoratedService($serviceId);
244+
}
245+
}
209246
}

0 commit comments

Comments
 (0)