Skip to content

Commit 7e5816f

Browse files
committed
Added config for the default plugins
1 parent 95d6b7b commit 7e5816f

File tree

5 files changed

+220
-4
lines changed

5 files changed

+220
-4
lines changed

DependencyInjection/Configuration.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* sections are normalized, and merged.
1616
*
1717
* @author David Buchmann <[email protected]>
18+
* @author Tobias Nyholm <[email protected]>
1819
*/
1920
class Configuration implements ConfigurationInterface
2021
{
@@ -27,6 +28,7 @@ public function getConfigTreeBuilder()
2728
$rootNode = $treeBuilder->root('httplug');
2829

2930
$this->configureClients($rootNode);
31+
$this->configurePlugins($rootNode);
3032

3133
$rootNode
3234
->validate()
@@ -108,4 +110,129 @@ protected function configureClients(ArrayNodeDefinition $root)
108110
->end()
109111
->end();
110112
}
113+
114+
/**
115+
* @param ArrayNodeDefinition $root
116+
*/
117+
protected function configurePlugins(ArrayNodeDefinition $root)
118+
{
119+
$root->children()
120+
->arrayNode('plugins')
121+
->addDefaultsIfNotSet()
122+
->children()
123+
124+
->arrayNode('authentication')
125+
->canBeEnabled()
126+
->children()
127+
->scalarNode('authentication')
128+
->info('This must be a service id to a service implementing Http\Message\Authentication')
129+
->isRequired()
130+
->cannotBeEmpty()
131+
->end()
132+
->end()
133+
->end() // End authentication plugin
134+
135+
->arrayNode('cache')
136+
->canBeEnabled()
137+
->addDefaultsIfNotSet()
138+
->children()
139+
->scalarNode('cache_pool')
140+
->info('This must be a service id to a service implementing Psr\Cache\CacheItemPoolInterface')
141+
->isRequired()
142+
->cannotBeEmpty()
143+
->end()
144+
->scalarNode('stream_factory')
145+
->info('This must be a service id to a service implementing Http\Message\StreamFactory')
146+
->defaultValue('httplug.stream_factory')
147+
->cannotBeEmpty()
148+
->end()
149+
->arrayNode('config')
150+
->addDefaultsIfNotSet()
151+
->children()
152+
->scalarNode('default_ttl')->defaultNull()->end()
153+
->scalarNode('respect_cache_headers')->defaultTrue()->end()
154+
->end()
155+
->end()
156+
->end()
157+
->end() // End cache plugin
158+
159+
->arrayNode('cookie')
160+
->canBeEnabled()
161+
->children()
162+
->scalarNode('cookie_jar')
163+
->info('This must be a service id to a service implementing Http\Message\CookieJar')
164+
->isRequired()
165+
->cannotBeEmpty()
166+
->end()
167+
->end()
168+
->end() // End cookie plugin
169+
170+
->arrayNode('decoder')
171+
->canBeDisabled()
172+
->addDefaultsIfNotSet()
173+
->children()
174+
->scalarNode('use_content_encoding')->defaultTrue()->end()
175+
->end()
176+
->end() // End decoder plugin
177+
178+
->arrayNode('history')
179+
->canBeEnabled()
180+
->children()
181+
->scalarNode('journal')
182+
->info('This must be a service id to a service implementing Http\Client\Plugin\Journal')
183+
->isRequired()
184+
->cannotBeEmpty()
185+
->end()
186+
->end()
187+
->end() // End history plugin
188+
189+
->arrayNode('logger')
190+
->canBeDisabled()
191+
->addDefaultsIfNotSet()
192+
->children()
193+
->scalarNode('logger')
194+
->info('This must be a service id to a service implementing Psr\Log\LoggerInterface')
195+
->defaultValue('logger')
196+
->cannotBeEmpty()
197+
->end()
198+
->scalarNode('formatter')
199+
->info('This must be a service id to a service implementing Http\Message\Formatter')
200+
->defaultNull()
201+
->end()
202+
->end()
203+
->end() // End logger plugin
204+
205+
->arrayNode('redirect')
206+
->canBeDisabled()
207+
->addDefaultsIfNotSet()
208+
->children()
209+
->scalarNode('preserve_header')->defaultTrue()->end()
210+
->scalarNode('use_default_for_multiple')->defaultTrue()->end()
211+
->end()
212+
->end() // End redirect plugin
213+
214+
->arrayNode('retry')
215+
->canBeDisabled()
216+
->addDefaultsIfNotSet()
217+
->children()
218+
->scalarNode('retry')->defaultValue(1)->end()
219+
->end()
220+
->end() // End retry plugin
221+
222+
->arrayNode('stopwatch')
223+
->canBeDisabled()
224+
->addDefaultsIfNotSet()
225+
->children()
226+
->scalarNode('stopwatch')
227+
->info('This must be a service id to a service extending Symfony\Component\Stopwatch\Stopwatch')
228+
->defaultValue('debug.stopwatch')
229+
->cannotBeEmpty()
230+
->end()
231+
->end()
232+
->end() // End stopwatch plugin
233+
234+
->end()
235+
->end()
236+
->end();
237+
}
111238
}

DependencyInjection/HttplugExtension.php

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
use Http\HttplugBundle\ClientFactory\DummyClient;
77
use Symfony\Component\Config\FileLocator;
88
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
use Symfony\Component\DependencyInjection\Definition;
910
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1011
use Symfony\Component\DependencyInjection\Reference;
1112
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1213

1314
/**
1415
* @author David Buchmann <[email protected]>
16+
* @author Tobias Nyholm <[email protected]>
1517
*/
1618
class HttplugExtension extends Extension
1719
{
@@ -50,6 +52,7 @@ public function load(array $configs, ContainerBuilder $container)
5052
foreach ($config['main_alias'] as $type => $id) {
5153
$container->setAlias(sprintf('httplug.%s', $type), $id);
5254
}
55+
$this->configurePlugins($container, $config['plugins']);
5356
$this->configureClients($container, $config);
5457
}
5558

@@ -59,7 +62,7 @@ public function load(array $configs, ContainerBuilder $container)
5962
* @param ContainerBuilder $container
6063
* @param array $config
6164
*/
62-
protected function configureClients(ContainerBuilder $container, array $config)
65+
private function configureClients(ContainerBuilder $container, array $config)
6366
{
6467
$first = isset($config['clients']['default']) ? 'default' : null;
6568
foreach ($config['clients'] as $name => $arguments) {
@@ -96,4 +99,67 @@ protected function configureClients(ContainerBuilder $container, array $config)
9699
->addArgument([new Reference('httplug.collector.history_plugin')]);
97100
}
98101
}
102+
103+
/**
104+
* @param ContainerBuilder $container
105+
* @param array $config
106+
*/
107+
private function configurePlugins(ContainerBuilder $container, array $config)
108+
{
109+
foreach ($config as $name => $pluginConfig) {
110+
$pluginId = 'httplug.plugin.'.$name;
111+
if ($pluginConfig['enabled']) {
112+
$def = $container->getDefinition($pluginId);
113+
$this->configurePluginByName($name, $def, $pluginConfig);
114+
} else {
115+
$container->removeDefinition($pluginId);
116+
}
117+
}
118+
}
119+
120+
/**
121+
* @param string $name
122+
* @param Definition $definition
123+
* @param array $config
124+
*/
125+
private function configurePluginByName($name, Definition $definition, array $config)
126+
{
127+
switch ($name) {
128+
case 'authentication':
129+
$definition->replaceArgument(0, new Reference($config['authentication']));
130+
break;
131+
case 'cache':
132+
$definition
133+
->replaceArgument(0, new Reference($config['cache_pool']))
134+
->replaceArgument(1, new Reference($config['stream_factory']))
135+
->replaceArgument(2, $config['config']);
136+
break;
137+
case 'cookie':
138+
$definition->replaceArgument(0, new Reference($config['cookie_jar']));
139+
break;
140+
case 'decoder':
141+
$definition->addArgument($config['use_content_encoding']);
142+
break;
143+
case 'history':
144+
$definition->replaceArgument(0, new Reference($config['journal']));
145+
break;
146+
case 'logger':
147+
$definition->replaceArgument(0, new Reference($config['logger']));
148+
if (!empty($config['formatter'])) {
149+
$definition->replaceArgument(1, new Reference($config['formatter']));
150+
}
151+
break;
152+
case 'redirect':
153+
$definition
154+
->addArgument($config['preserve_header'])
155+
->addArgument($config['use_default_for_multiple']);
156+
break;
157+
case 'retry':
158+
$definition->addArgument($config['retry']);
159+
break;
160+
case 'stopwatch':
161+
$definition->replaceArgument(0, new Reference($config['stopwatch']));
162+
break;
163+
}
164+
}
99165
}

HttplugBundle.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/**
88
* @author David Buchmann <[email protected]>
9+
* @author Tobias Nyholm <[email protected]>
910
*/
1011
class HttplugBundle extends Bundle
1112
{

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<br> httplug.plugin.stopwatch | 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 | 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.cache', ''httplug.plugin.retry']
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>null</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)