Skip to content

Commit 22052d8

Browse files
committed
Added plugin feature
1 parent 050f490 commit 22052d8

File tree

5 files changed

+101
-3
lines changed

5 files changed

+101
-3
lines changed

ClientFactory/PluginClientFactory.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\ClientFactory;
4+
5+
use Http\Client\Plugin\PluginClient;
6+
7+
/**
8+
* This factory creates a PluginClient.
9+
*
10+
* @author Tobias Nyholm <[email protected]>
11+
*/
12+
class PluginClientFactory
13+
{
14+
/**
15+
* @param array $plugins
16+
* @param ClientFactoryInterface $factory
17+
* @param array $config
18+
*
19+
* @return PluginClient
20+
*/
21+
static public function createPluginClient(array $plugins, ClientFactoryInterface $factory, array $config)
22+
{
23+
return new PluginClient($factory->createClient($config), $plugins);
24+
}
25+
}

DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ protected function configureClients(ArrayNodeDefinition $root)
9090
->cannotBeEmpty()
9191
->info('The service id of a factory to use when creating the adapter.')
9292
->end()
93+
->arrayNode('plugins')
94+
->info('A list of service ids of plugins. The order is imortant.')
95+
->prototype('scalar')->end()
96+
->end()
9397
->variableNode('config')->end()
9498
->end()
9599
->end();

DependencyInjection/HttplugExtension.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function load(array $configs, ContainerBuilder $container)
2525
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
2626

2727
$loader->load('services.xml');
28+
$loader->load('plugins.xml');
2829
$loader->load('discovery.xml');
2930
foreach ($config['classes'] as $service => $class) {
3031
if (!empty($class)) {
@@ -36,19 +37,42 @@ public function load(array $configs, ContainerBuilder $container)
3637
foreach ($config['main_alias'] as $type => $id) {
3738
$container->setAlias(sprintf('httplug.%s', $type), $id);
3839
}
40+
$this->configureClients($container, $config);
3941

40-
// Configure client services
42+
43+
}
44+
45+
/**
46+
* Configure client services
47+
*
48+
* @param ContainerBuilder $container
49+
* @param array $config
50+
*/
51+
protected function configureClients(ContainerBuilder $container, array $config)
52+
{
4153
$first = isset($config['clients']['default']) ? 'default' : null;
4254
foreach ($config['clients'] as $name => $arguments) {
4355
if ($first === null) {
4456
$first = $name;
4557
}
4658

4759
$def = $container->register('httplug.client.'.$name, DummyClient::class);
48-
$def->setFactory([new Reference($arguments['factory']), 'createClient'])
49-
->addArgument($arguments['config']);
60+
61+
if (empty($arguments['plugins'])) {
62+
$def->setFactory([new Reference($arguments['factory']), 'createClient'])
63+
->addArgument($arguments['config']);
64+
} else {
65+
$def->setFactory('Http\HttplugBundle\ClientFactory\PluginClientFactory::createPluginClient')
66+
->addArgument(array_map(function($id) {
67+
return new Reference($id);
68+
}, $arguments['plugins']))
69+
->addArgument(new Reference($arguments['factory']))
70+
->addArgument($arguments['config']);
71+
}
72+
5073
}
5174

75+
// Alias the first client to httplug.client.default
5276
if ($first !== null) {
5377
$container->setAlias('httplug.client.default', 'httplug.client.'.$first);
5478
}

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,27 @@ $httpClient = $this->container->get('httplug.client.my_guzzle5');
9999
$httpClient = $this->container->get('httplug.client.acme');
100100
```
101101

102+
#### Plugins
103+
104+
You can configure the clients with plugins.
105+
106+
```yaml
107+
// services.yml
108+
my_cache_plugin:
109+
class: Http\Client\Plugin\CachePlugin
110+
arguments: ["@cache", "@stream_factory"]
111+
```
112+
```yaml
113+
// config.yml
114+
httpug:
115+
clients:
116+
acme:
117+
factory: 'httplug.factory.guzzle6'
118+
plugins: ['my_cache_plugin' , 'httplug.plugin.logger']
119+
config:
120+
base_uri: 'http://google.se/'
121+
```
122+
102123
### Use for Reusable Bundles
103124
104125
Rather than code against specific HTTP clients, you want to use the Httplug `Client` interface. To avoid building your own infrastructure to define services for the client, simply `require: php-http/httplug-bundle` in your bundles `composer.json`. You SHOULD provide configuration for each of your services that needs an HTTP client to specify the service to use, defaulting to `httplug.client`. This way, the default case needs no additional configuration for your users.

Resources/config/plugins.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<services>
7+
<service id="httplug.plugin.cache" class="Http\Client\Plugin\CachePlugin" public="false">
8+
<argument />
9+
<argument type="service" id="httplug.stream_factory"/>
10+
<argument />
11+
</service>
12+
<service id="httplug.plugin.content_length" class="Http\Client\Plugin\ContentLengthPlugin" public="false" />
13+
<service id="httplug.plugin.decoder" class="Http\Client\Plugin\DecoderPlugin" public="false">
14+
<argument />
15+
</service>
16+
<service id="httplug.plugin.error" class="Http\Client\Plugin\ErrorPlugin" public="false" />
17+
<service id="httplug.plugin.logger" class="Http\Client\Plugin\LoggerPlugin" public="false">
18+
<argument type="service" id="logger"/>
19+
<argument />
20+
</service>
21+
<service id="httplug.plugin.redirect" class="Http\Client\Plugin\RedirectPlugin" public="false" />
22+
<service id="httplug.plugin.retry" class="Http\Client\Plugin\RetryPlugin" public="false" />
23+
</services>
24+
</container>

0 commit comments

Comments
 (0)