-
Notifications
You must be signed in to change notification settings - Fork 50
Configure plugins #37
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
+302
−4
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
* sections are normalized, and merged. | ||
* | ||
* @author David Buchmann <[email protected]> | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class Configuration implements ConfigurationInterface | ||
{ | ||
|
@@ -27,6 +28,7 @@ public function getConfigTreeBuilder() | |
$rootNode = $treeBuilder->root('httplug'); | ||
|
||
$this->configureClients($rootNode); | ||
$this->configurePlugins($rootNode); | ||
|
||
$rootNode | ||
->validate() | ||
|
@@ -108,4 +110,129 @@ protected function configureClients(ArrayNodeDefinition $root) | |
->end() | ||
->end(); | ||
} | ||
|
||
/** | ||
* @param ArrayNodeDefinition $root | ||
*/ | ||
protected function configurePlugins(ArrayNodeDefinition $root) | ||
{ | ||
$root->children() | ||
->arrayNode('plugins') | ||
->addDefaultsIfNotSet() | ||
->children() | ||
|
||
->arrayNode('authentication') | ||
->canBeEnabled() | ||
->children() | ||
->scalarNode('authentication') | ||
->info('This must be a service id to a service implementing Http\Message\Authentication') | ||
->isRequired() | ||
->cannotBeEmpty() | ||
->end() | ||
->end() | ||
->end() // End authentication plugin | ||
|
||
->arrayNode('cache') | ||
->canBeEnabled() | ||
->addDefaultsIfNotSet() | ||
->children() | ||
->scalarNode('cache_pool') | ||
->info('This must be a service id to a service implementing Psr\Cache\CacheItemPoolInterface') | ||
->isRequired() | ||
->cannotBeEmpty() | ||
->end() | ||
->scalarNode('stream_factory') | ||
->info('This must be a service id to a service implementing Http\Message\StreamFactory') | ||
->defaultValue('httplug.stream_factory') | ||
->cannotBeEmpty() | ||
->end() | ||
->arrayNode('config') | ||
->addDefaultsIfNotSet() | ||
->children() | ||
->scalarNode('default_ttl')->defaultNull()->end() | ||
->scalarNode('respect_cache_headers')->defaultTrue()->end() | ||
->end() | ||
->end() | ||
->end() | ||
->end() // End cache plugin | ||
|
||
->arrayNode('cookie') | ||
->canBeEnabled() | ||
->children() | ||
->scalarNode('cookie_jar') | ||
->info('This must be a service id to a service implementing Http\Message\CookieJar') | ||
->isRequired() | ||
->cannotBeEmpty() | ||
->end() | ||
->end() | ||
->end() // End cookie plugin | ||
|
||
->arrayNode('decoder') | ||
->canBeDisabled() | ||
->addDefaultsIfNotSet() | ||
->children() | ||
->scalarNode('use_content_encoding')->defaultTrue()->end() | ||
->end() | ||
->end() // End decoder plugin | ||
|
||
->arrayNode('history') | ||
->canBeEnabled() | ||
->children() | ||
->scalarNode('journal') | ||
->info('This must be a service id to a service implementing Http\Client\Plugin\Journal') | ||
->isRequired() | ||
->cannotBeEmpty() | ||
->end() | ||
->end() | ||
->end() // End history plugin | ||
|
||
->arrayNode('logger') | ||
->canBeDisabled() | ||
->addDefaultsIfNotSet() | ||
->children() | ||
->scalarNode('logger') | ||
->info('This must be a service id to a service implementing Psr\Log\LoggerInterface') | ||
->defaultValue('logger') | ||
->cannotBeEmpty() | ||
->end() | ||
->scalarNode('formatter') | ||
->info('This must be a service id to a service implementing Http\Message\Formatter') | ||
->defaultNull() | ||
->end() | ||
->end() | ||
->end() // End logger plugin | ||
|
||
->arrayNode('redirect') | ||
->canBeDisabled() | ||
->addDefaultsIfNotSet() | ||
->children() | ||
->scalarNode('preserve_header')->defaultTrue()->end() | ||
->scalarNode('use_default_for_multiple')->defaultTrue()->end() | ||
->end() | ||
->end() // End redirect plugin | ||
|
||
->arrayNode('retry') | ||
->canBeDisabled() | ||
->addDefaultsIfNotSet() | ||
->children() | ||
->scalarNode('retry')->defaultValue(1)->end() | ||
->end() | ||
->end() // End retry plugin | ||
|
||
->arrayNode('stopwatch') | ||
->canBeDisabled() | ||
->addDefaultsIfNotSet() | ||
->children() | ||
->scalarNode('stopwatch') | ||
->info('This must be a service id to a service extending Symfony\Component\Stopwatch\Stopwatch') | ||
->defaultValue('debug.stopwatch') | ||
->cannotBeEmpty() | ||
->end() | ||
->end() | ||
->end() // End stopwatch plugin | ||
|
||
->end() | ||
->end() | ||
->end(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,14 @@ | |
use Http\HttplugBundle\ClientFactory\DummyClient; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
|
||
/** | ||
* @author David Buchmann <[email protected]> | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class HttplugExtension extends Extension | ||
{ | ||
|
@@ -50,6 +52,7 @@ public function load(array $configs, ContainerBuilder $container) | |
foreach ($config['main_alias'] as $type => $id) { | ||
$container->setAlias(sprintf('httplug.%s', $type), $id); | ||
} | ||
$this->configurePlugins($container, $config['plugins']); | ||
$this->configureClients($container, $config); | ||
} | ||
|
||
|
@@ -59,7 +62,7 @@ public function load(array $configs, ContainerBuilder $container) | |
* @param ContainerBuilder $container | ||
* @param array $config | ||
*/ | ||
protected function configureClients(ContainerBuilder $container, array $config) | ||
private function configureClients(ContainerBuilder $container, array $config) | ||
{ | ||
$first = isset($config['clients']['default']) ? 'default' : null; | ||
foreach ($config['clients'] as $name => $arguments) { | ||
|
@@ -96,4 +99,67 @@ protected function configureClients(ContainerBuilder $container, array $config) | |
->addArgument([new Reference('httplug.collector.history_plugin')]); | ||
} | ||
} | ||
|
||
/** | ||
* @param ContainerBuilder $container | ||
* @param array $config | ||
*/ | ||
private function configurePlugins(ContainerBuilder $container, array $config) | ||
{ | ||
foreach ($config as $name => $pluginConfig) { | ||
$pluginId = 'httplug.plugin.'.$name; | ||
if ($pluginConfig['enabled']) { | ||
$def = $container->getDefinition($pluginId); | ||
$this->configurePluginByName($name, $def, $pluginConfig); | ||
} else { | ||
$container->removeDefinition($pluginId); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param Definition $definition | ||
* @param array $config | ||
*/ | ||
private function configurePluginByName($name, Definition $definition, array $config) | ||
{ | ||
switch ($name) { | ||
case 'authentication': | ||
$definition->replaceArgument(0, new Reference($config['authentication'])); | ||
break; | ||
case 'cache': | ||
$definition | ||
->replaceArgument(0, new Reference($config['cache_pool'])) | ||
->replaceArgument(1, new Reference($config['stream_factory'])) | ||
->replaceArgument(2, $config['config']); | ||
break; | ||
case 'cookie': | ||
$definition->replaceArgument(0, new Reference($config['cookie_jar'])); | ||
break; | ||
case 'decoder': | ||
$definition->addArgument($config['use_content_encoding']); | ||
break; | ||
case 'history': | ||
$definition->replaceArgument(0, new Reference($config['journal'])); | ||
break; | ||
case 'logger': | ||
$definition->replaceArgument(0, new Reference($config['logger'])); | ||
if (!empty($config['formatter'])) { | ||
$definition->replaceArgument(1, new Reference($config['formatter'])); | ||
} | ||
break; | ||
case 'redirect': | ||
$definition | ||
->addArgument($config['preserve_header']) | ||
->addArgument($config['use_default_for_multiple']); | ||
break; | ||
case 'retry': | ||
$definition->addArgument($config['retry']); | ||
break; | ||
case 'stopwatch': | ||
$definition->replaceArgument(0, new Reference($config['stopwatch'])); | ||
break; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
/** | ||
* @author David Buchmann <[email protected]> | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class HttplugBundle extends Bundle | ||
{ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or configurePlugin$name() and split the code below into a method per case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was considering that approach. Switch statements are code smell but dynamic functions like that is worse. Also, you cant type hint dynamic functions in PHP7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay. lets keep this then. its very internal either way so no big deal.