diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 2394a0f4..6207d362 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -18,22 +18,39 @@ class Configuration implements ConfigurationInterface public function getConfigTreeBuilder() { $treeBuilder = new TreeBuilder('knpu_oauth2_client'); - if (method_exists($treeBuilder, 'getRootNode')) { - $rootNode = $treeBuilder->getRootNode(); - } else { - $rootNode = $treeBuilder->root('knpu_oauth2_client'); - } + $rootNode = method_exists($treeBuilder, 'getRootNode') + ? $treeBuilder->getRootNode() + : $treeBuilder->root('knpu_oauth2_client'); + $rootNode ->children() - ->scalarNode('http_client')->defaultNull()->info('Service id of HTTP client to use (must implement GuzzleHttp\ClientInterface)')->end() - ->arrayNode('clients') - ->normalizeKeys(false) - ->useAttributeAsKey('variable') - ->prototype('array') - ->prototype('variable')->end() + ->scalarNode('http_client') + ->defaultNull() + ->info('Service id of HTTP client to use (must implement GuzzleHttp\ClientInterface)') + ->end() + ->arrayNode('http_client_options') + ->addDefaultsIfNotSet() + ->children() + ->integerNode('timeout')->min(0)->end() + ->scalarNode('proxy')->end() + ->booleanNode('verify')->info('Use only with proxy option set')->end() + ->end() ->end() + ->arrayNode('clients') + ->normalizeKeys(false) + ->useAttributeAsKey('variable') + ->prototype('array') + ->prototype('variable')->end() + ->end() + ->end() + ->end() + ->validate() + ->ifTrue(function ($v) { + return isset($v['http_client_options'], $v['http_client']) && !empty($v['http_client_options']); + }) + ->thenInvalid('You cannot use both "http_client_options" and "http_client" at the same time under "knpu_oauth2_client".') ->end() - ->end(); + ; return $treeBuilder; } diff --git a/src/DependencyInjection/KnpUOAuth2ClientExtension.php b/src/DependencyInjection/KnpUOAuth2ClientExtension.php index c13a0777..36f8c83c 100644 --- a/src/DependencyInjection/KnpUOAuth2ClientExtension.php +++ b/src/DependencyInjection/KnpUOAuth2ClientExtension.php @@ -157,6 +157,7 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('services.xml'); $httpClient = $config['http_client']; + $httpClientOptions = $config['http_client_options']; $clientConfigurations = $config['clients']; $clientServiceKeys = []; @@ -164,7 +165,7 @@ public function load(array $configs, ContainerBuilder $container) // manually make sure "type" is there if (!isset($clientConfig['type'])) { throw new InvalidConfigurationException(sprintf( - 'Your "knpu_oauth2_client.clients." config entry is missing the "type" key.', + 'Your "knpu_oauth2_client.clients.%s" config entry is missing the "type" key.', $key )); } @@ -181,20 +182,23 @@ public function load(array $configs, ContainerBuilder $container) // process the configuration $tree = new TreeBuilder('knpu_oauth2_client/clients/' . $key); - if (method_exists($tree, 'getRootNode')) { - $node = $tree->getRootNode(); - } else { - $node = $tree->root('knpu_oauth2_client/clients/' . $key); - } + $node = method_exists($tree, 'getRootNode') + ? $tree->getRootNode() + : $tree->root('knpu_oauth2_client/clients/' . $key); + $this->buildConfigurationForType($node, $type); $processor = new Processor(); $config = $processor->process($tree->buildTree(), [$clientConfig]); $configurator = $this->getConfigurator($type); + $providerOptions = $configurator->getProviderOptions($config); + $collaborators = []; if ($httpClient) { $collaborators['httpClient'] = new Reference($httpClient); + } else { + $providerOptions = array_merge($providerOptions, $httpClientOptions); } // hey, we should add the provider/client service! $clientServiceKey = $this->configureProviderAndClient( @@ -204,7 +208,7 @@ public function load(array $configs, ContainerBuilder $container) $configurator->getProviderClass($config), $configurator->getClientClass($config), $configurator->getPackagistName(), - $configurator->getProviderOptions($config), + $providerOptions, $config['redirect_route'], $config['redirect_params'], $config['use_state'], diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index d72952fe..975f15c7 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -51,7 +51,7 @@ public function provideConfigurationTests() $tests[] = [ [], - ['http_client' => null, 'clients' => []], + ['http_client' => null, 'clients' => [], 'http_client_options' => []], ]; $fbConfig = [ @@ -64,7 +64,7 @@ public function provideConfigurationTests() ]; $tests[] = [ ['http_client' => null, 'clients' => ['facebook1' => $fbConfig]], - ['http_client' => null, 'clients' => ['facebook1' => $fbConfig]], + ['http_client' => null, 'clients' => ['facebook1' => $fbConfig], 'http_client_options' => []], ]; $tests[] = [ diff --git a/tests/app/TestKernel.php b/tests/app/TestKernel.php index d819f1ce..24aef89c 100644 --- a/tests/app/TestKernel.php +++ b/tests/app/TestKernel.php @@ -61,4 +61,22 @@ public function registerContainerConfiguration(LoaderInterface $loader) ]); }); } + + public function getCacheDir() + { + if (method_exists($this, 'getProjectDir')) { + return $this->getProjectDir() . '/tests/app/cache/' . $this->getEnvironment(); + } + + return parent::getCacheDir(); + } + + public function getLogDir() + { + if (method_exists($this, 'getProjectDir')) { + return $this->getProjectDir() . '/tests/app/cache/' . $this->getEnvironment(); + } + + return parent::getLogDir(); + } }