diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index c4b793bc..856fa26d 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -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() @@ -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() diff --git a/DependencyInjection/HttplugExtension.php b/DependencyInjection/HttplugExtension.php index df43e3c8..cfa717b0 100644 --- a/DependencyInjection/HttplugExtension.php +++ b/DependencyInjection/HttplugExtension.php @@ -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; @@ -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 @@ -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); + } + } }