Skip to content

Commit ad61c35

Browse files
committed
Added config for the default plugins
1 parent c258776 commit ad61c35

File tree

4 files changed

+182
-3
lines changed

4 files changed

+182
-3
lines changed

DependencyInjection/Configuration.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function getConfigTreeBuilder()
2727
$rootNode = $treeBuilder->root('httplug');
2828

2929
$this->configureClients($rootNode);
30+
$this->configurePlugins($rootNode);
3031

3132
$rootNode
3233
->validate()
@@ -108,4 +109,96 @@ protected function configureClients(ArrayNodeDefinition $root)
108109
->end()
109110
->end();
110111
}
112+
113+
/**
114+
* @param ArrayNodeDefinition $root
115+
*/
116+
protected function configurePlugins(ArrayNodeDefinition $root)
117+
{
118+
$root->children()
119+
->arrayNode('plugins')
120+
->addDefaultsIfNotSet()
121+
->children()
122+
123+
->arrayNode('authentication')
124+
->canBeEnabled()
125+
->children()
126+
->scalarNode('authentication')->isRequired()->cannotBeEmpty()->end()
127+
->end()
128+
->end() // End authentication plugin
129+
130+
->arrayNode('cache')
131+
->canBeEnabled()
132+
->addDefaultsIfNotSet()
133+
->children()
134+
->scalarNode('cache_pool')->isRequired()->cannotBeEmpty()->end()
135+
->scalarNode('stream_factory')->cannotBeEmpty()->defaultValue('httplug.stream_factory')->end()
136+
->arrayNode('config')
137+
->children()
138+
->scalarNode('default_ttl')->defaultNull()->end()
139+
->scalarNode('respect_cache_headers')->defaultTrue()->end()
140+
->end()
141+
->end()
142+
->end()
143+
->end() // End cache plugin
144+
145+
->arrayNode('cookie')
146+
->canBeEnabled()
147+
->children()
148+
->scalarNode('cookie_jar')->isRequired()->cannotBeEmpty()->end()
149+
->end()
150+
->end() // End cookie plugin
151+
152+
->arrayNode('decoder')
153+
->canBeDisabled()
154+
->addDefaultsIfNotSet()
155+
->children()
156+
->scalarNode('use_content_encoding')->defaultTrue()->end()
157+
->end()
158+
->end() // End decoder plugin
159+
160+
->arrayNode('history')
161+
->canBeEnabled()
162+
->children()
163+
->scalarNode('journal')->isRequired()->cannotBeEmpty()->end()
164+
->end()
165+
->end() // End history plugin
166+
167+
->arrayNode('logger')
168+
->canBeDisabled()
169+
->addDefaultsIfNotSet()
170+
->children()
171+
->scalarNode('logger')->isRequired()->cannotBeEmpty()->defaultValue('logger')->end()
172+
->scalarNode('formatter')->defaultNull()->end()
173+
->end()
174+
->end() // End logger plugin
175+
176+
->arrayNode('redirect')
177+
->canBeDisabled()
178+
->addDefaultsIfNotSet()
179+
->children()
180+
->scalarNode('preserve_header')->defaultTrue()->end()
181+
->scalarNode('use_default_for_multiple')->defaultTrue()->end()
182+
->end()
183+
->end() // End redirect plugin
184+
185+
->arrayNode('retry')
186+
->canBeDisabled()
187+
->addDefaultsIfNotSet()
188+
->children()
189+
->scalarNode('retry')->defaultValue(1)->end()
190+
->end()
191+
->end() // End retry plugin
192+
193+
->arrayNode('stopwatch')
194+
->canBeEnabled()
195+
->children()
196+
->scalarNode('stopwatch')->isRequired()->cannotBeEmpty()->end()
197+
->end()
198+
->end() // End stopwatch plugin
199+
200+
->end()
201+
->end()
202+
->end();
203+
}
111204
}

DependencyInjection/HttplugExtension.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Http\HttplugBundle\ClientFactory\DummyClient;
66
use Symfony\Component\Config\FileLocator;
77
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
use Symfony\Component\DependencyInjection\Definition;
89
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
910
use Symfony\Component\DependencyInjection\Reference;
1011
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
@@ -49,6 +50,7 @@ public function load(array $configs, ContainerBuilder $container)
4950
foreach ($config['main_alias'] as $type => $id) {
5051
$container->setAlias(sprintf('httplug.%s', $type), $id);
5152
}
53+
$this->configurePlugins($container, $config['plugins']);
5254
$this->configureClients($container, $config);
5355
}
5456

@@ -90,4 +92,66 @@ protected function configureClients(ContainerBuilder $container, array $config)
9092
$container->setAlias('httplug.client.default', 'httplug.client.'.$first);
9193
}
9294
}
95+
96+
/**
97+
* @param ContainerBuilder $container
98+
* @param array $config
99+
*/
100+
protected function configurePlugins(ContainerBuilder $container, array $config)
101+
{
102+
foreach ($config as $name => $pluginConfig) {
103+
$pluginId = 'httplug.plugin.'.$name;
104+
if ($pluginConfig['enabled']) {
105+
$def = $container->getDefinition($pluginId);
106+
$this->configurePluginByName($name, $def, $pluginConfig);
107+
} else {
108+
$container->removeDefinition($pluginId);
109+
}
110+
}
111+
}
112+
113+
/**
114+
* @param string $name
115+
* @param Definition $definition
116+
* @param array $config
117+
*/
118+
private function configurePluginByName($name, Definition $definition, array $config)
119+
{
120+
switch ($name) {
121+
case 'authentication':
122+
$definition->replaceArgument(0, new Reference($config['authentication']));
123+
break;
124+
case 'cache':
125+
$definition
126+
->replaceArgument(0, new Reference($config['cache_pool']))
127+
->replaceArgument(1, new Reference($config['stream_factory']))
128+
->replaceArgument(2, $config['config']);
129+
break;
130+
case 'cookie':
131+
$definition->replaceArgument(0, new Reference($config['cookie_jar']));
132+
break;
133+
case 'decoder':
134+
$definition->addArgument($config['use_content_encoding']);
135+
break;
136+
case 'history':
137+
$definition->replaceArgument(0, new Reference($config['journal']));
138+
break;
139+
case 'logger':
140+
$definition
141+
->replaceArgument(0, new Reference($config['logger']))
142+
->replaceArgument(1, new Reference($config['formatter']));
143+
break;
144+
case 'redirect':
145+
$definition
146+
->addArgument($config['preserve_header'])
147+
->addArgument($config['use_default_for_multiple']);
148+
break;
149+
case 'retry':
150+
$definition->addArgument($config['retry']);
151+
break;
152+
case 'stopwatch':
153+
$definition->replaceArgument(0, new Reference($config['stopwatch']));
154+
break;
155+
}
156+
}
93157
}

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ For information how to write applications with the services provided by this bun
5151
| httplug.stream_factory | Service* that provides the `Http\Message\StreamFactory`
5252
| httplug.client.[name] | This is your Httpclient that you have configured. With the configuration below the name would be `acme_client`.
5353
| httplug.client | This is the first client configured or a client named `default`.
54-
| httplug.plugin.content_length <br> httplug.plugin.decoder<br> httplug.plugin.error<br> httplug.plugin.logger<br> httplug.plugin.redirect<br> httplug.plugin.retry | These are built in plugins that live in the `php-http/plugins` package. These servcies are not public and may only be used when configure HttpClients or services.
54+
| httplug.plugin.content_length <br> httplug.plugin.decoder<br> httplug.plugin.error<br> httplug.plugin.logger<br> httplug.plugin.redirect<br> httplug.plugin.retry | These are plugins that are enabled by default. These services are not public and may only be used when configure HttpClients or other services.
55+
| httplug.plugin.authentication <br> httplug.plugin.cache<br> httplug.plugin.cookie<br> httplug.plugin.history<br> httplug.plugin.stopwatch | These are plugins that are disabled by default. They need to be configured before they can be used. These services are not public and may only be used when configure HttpClients or other services.
5556

5657
\* *These services are always an alias to another service. You can specify your own service or leave the default, which is the same name with `.default` appended. The default services in turn use the service discovery mechanism to provide the best available implementation. You can specify a class for each of the default services to use instead of discovery, as long as those classes can be instantiated without arguments.*
5758

@@ -122,10 +123,13 @@ acme_plugin:
122123
```yaml
123124
// config.yml
124125
httpug:
126+
plugins:
127+
cache:
128+
cache_pool: 'my_cache_pool'
125129
clients:
126130
acme:
127131
factory: 'httplug.factory.guzzle6'
128-
plugins: ['acme_plugin' , 'httplug.plugin.logger']
132+
plugins: ['acme_plugin', 'httplug.plugin.logger', ''httplug.plugin.cache']
129133
config:
130134
base_uri: 'http://google.se/'
131135
```

Resources/config/plugins.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,31 @@
44
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
55

66
<services>
7+
<service id="httplug.plugin.authentication" class="Http\Client\Plugin\AuthenticationPlugin" public="false">
8+
<argument />
9+
</service>
10+
<service id="httplug.plugin.cache" class="Http\Client\Plugin\CachePlugin" public="false">
11+
<argument />
12+
<argument />
13+
<argument />
14+
</service>
715
<service id="httplug.plugin.content_length" class="Http\Client\Plugin\ContentLengthPlugin" public="false" />
16+
<service id="httplug.plugin.cookie" class="Http\Client\Plugin\CookiePlugin" public="false">
17+
<argument />
18+
</service>
819
<service id="httplug.plugin.decoder" class="Http\Client\Plugin\DecoderPlugin" public="false" />
920
<service id="httplug.plugin.error" class="Http\Client\Plugin\ErrorPlugin" public="false" />
21+
<service id="httplug.plugin.history" class="Http\Client\Plugin\HistoryPlugin" public="false">
22+
<argument />
23+
</service>
1024
<service id="httplug.plugin.logger" class="Http\Client\Plugin\LoggerPlugin" public="false">
11-
<argument type="service" id="logger"/>
25+
<argument />
26+
<argument />
1227
</service>
1328
<service id="httplug.plugin.redirect" class="Http\Client\Plugin\RedirectPlugin" public="false" />
1429
<service id="httplug.plugin.retry" class="Http\Client\Plugin\RetryPlugin" public="false" />
30+
<service id="httplug.plugin.stopwatch" class="Http\Client\Plugin\StopwatchPlugin" public="false">
31+
<argument />
32+
</service>
1533
</services>
1634
</container>

0 commit comments

Comments
 (0)