Skip to content

Plugins #16

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
merged 1 commit into from
Jan 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions ClientFactory/PluginClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Http\HttplugBundle\ClientFactory;

use Http\Client\Plugin\PluginClient;

/**
* This factory creates a PluginClient.
*
* @author Tobias Nyholm <[email protected]>
*/
class PluginClientFactory
{
/**
* @param array $plugins
* @param ClientFactoryInterface $factory
* @param array $config
*
* @return PluginClient
*/
static public function createPluginClient(array $plugins, ClientFactoryInterface $factory, array $config)
{
return new PluginClient($factory->createClient($config), $plugins);
}
}
4 changes: 4 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ protected function configureClients(ArrayNodeDefinition $root)
->cannotBeEmpty()
->info('The service id of a factory to use when creating the adapter.')
->end()
->arrayNode('plugins')
->info('A list of service ids of plugins. The order is important.')
->prototype('scalar')->end()
->end()
->variableNode('config')->end()
->end()
->end();
Expand Down
30 changes: 27 additions & 3 deletions DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

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

// Configure client services

}

/**
* Configure client services
*
* @param ContainerBuilder $container
* @param array $config
*/
protected function configureClients(ContainerBuilder $container, array $config)
{
$first = isset($config['clients']['default']) ? 'default' : null;
foreach ($config['clients'] as $name => $arguments) {
if ($first === null) {
$first = $name;
}

$def = $container->register('httplug.client.'.$name, DummyClient::class);
$def->setFactory([new Reference($arguments['factory']), 'createClient'])
->addArgument($arguments['config']);

if (empty($arguments['plugins'])) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want this? An alternative could be to send everything to PluginClientFactory so every client is an instance of PluginClient.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would keep it. there is no way to add plugins later at runtime, so it would only be relevant for people that typehint the concrete class instead of the interface, which would be so wrong that we should not care about helping them hide the problem :-)

$def->setFactory([new Reference($arguments['factory']), 'createClient'])
->addArgument($arguments['config']);
} else {
$def->setFactory('Http\HttplugBundle\ClientFactory\PluginClientFactory::createPluginClient')
->addArgument(array_map(function($id) {
return new Reference($id);
}, $arguments['plugins']))
->addArgument(new Reference($arguments['factory']))
->addArgument($arguments['config']);
}

}

// Alias the first client to httplug.client.default
if ($first !== null) {
$container->setAlias('httplug.client.default', 'httplug.client.'.$first);
}
Expand Down
45 changes: 37 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,26 @@ For information how to write applications with the services provided by this bun

#### Custom services

This bundle provides 3 services:

* `httplug.client` a service that provides the `Http\Client\HttpClient`
* `httplug.message_factory` a service that provides the `Http\Message\MessageFactory`
* `httplug.uri_factory` a service that provides the `Http\Message\UriFactory`
* `httplug.stream_factory` a service that provides the `Http\Message\StreamFactory`
| Service id | Description |
| ---------- | ----------- |
| httplug.message_factory | Service* that provides the `Http\Message\MessageFactory`
| httplug.uri_factory | Service* that provides the `Http\Message\UriFactory`
| httplug.stream_factory | Service* that provides the `Http\Message\StreamFactory`
| httplug.client.[name] | This is your Httpclient that you have configured. With the configuration below the name would be `acme_client`.
| httplug.client | This is the first client configured or a client named `default`.
| 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 build in plugins that lives in the `php-http/plugins` package. These servcies are not public and may only be used when configure HttpClients or services.

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.
\* *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.*

If you need a more custom setup, define the services in your application configuration and specify your service in the `main_alias` section. For example, to add authentication headers, you could define a service that decorates the service `httplug.client.default` with a plugin that injects the authentication headers into the request and configure `httplug.main_alias.client` to the name of your service.

```yaml
httplug:
clients:
acme_client: # This is the name of the client
factory: 'httplug.factory.guzzle6'

main_alias:
client: httplug.client.default
message_factory: httplug.message_factory.default
Expand All @@ -80,16 +87,16 @@ httplug:
factory: 'httplug.factory.guzzle5'
config:
# These options are given to Guzzle without validation.
base_url: 'http://google.se/'
defaults:
base_uri: 'http://google.se/'
verify_ssl: false
timeout: 4
headers:
Content-Type: 'application/json'
acme:
factory: 'httplug.factory.guzzle6'
config:
base_url: 'http://google.se/'
base_uri: 'http://google.se/'

```

Expand All @@ -99,6 +106,28 @@ $httpClient = $this->container->get('httplug.client.my_guzzle5');
$httpClient = $this->container->get('httplug.client.acme');
```

#### Plugins

You can configure the clients with plugins.

```yaml
// services.yml
acme_plugin:
class: Acme\Plugin\MyCustonPlugin
arguments: ["%api_key%"]
```
```yaml
// config.yml
httpug:
clients:
acme:
factory: 'httplug.factory.guzzle6'
plugins: ['acme_plugin' , 'httplug.plugin.logger']
config:
base_uri: 'http://google.se/'
```


### Use for Reusable Bundles

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.
Expand Down
17 changes: 17 additions & 0 deletions Resources/config/plugins.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="httplug.plugin.content_length" class="Http\Client\Plugin\ContentLengthPlugin" public="false" />
<service id="httplug.plugin.decoder" class="Http\Client\Plugin\DecoderPlugin" public="false">
</service>
<service id="httplug.plugin.error" class="Http\Client\Plugin\ErrorPlugin" public="false" />
<service id="httplug.plugin.logger" class="Http\Client\Plugin\LoggerPlugin" public="false">
<argument type="service" id="logger"/>
</service>
<service id="httplug.plugin.redirect" class="Http\Client\Plugin\RedirectPlugin" public="false" />
<service id="httplug.plugin.retry" class="Http\Client\Plugin\RetryPlugin" public="false" />
</services>
</container>