Skip to content

Commit

Permalink
Merge pull request #151 from knpuniversity/issue_145
Browse files Browse the repository at this point in the history
added ability to configure default http_client
  • Loading branch information
weaverryan authored Dec 31, 2018
2 parents 8afacf5 + 21b5232 commit 37311de
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 21 deletions.
41 changes: 29 additions & 12 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
18 changes: 11 additions & 7 deletions src/DependencyInjection/KnpUOAuth2ClientExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,15 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('services.xml');

$httpClient = $config['http_client'];
$httpClientOptions = $config['http_client_options'];
$clientConfigurations = $config['clients'];

$clientServiceKeys = [];
foreach ($clientConfigurations as $key => $clientConfig) {
// 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
));
}
Expand All @@ -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(
Expand All @@ -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'],
Expand Down
4 changes: 2 additions & 2 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function provideConfigurationTests()

$tests[] = [
[],
['http_client' => null, 'clients' => []],
['http_client' => null, 'clients' => [], 'http_client_options' => []],
];

$fbConfig = [
Expand All @@ -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[] = [
Expand Down
18 changes: 18 additions & 0 deletions tests/app/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 37311de

Please sign in to comment.