|
1 | 1 | # Plugin System
|
2 | 2 |
|
3 |
| -The plugin system will allow to manipulate requests and responses inside a `HttpClient`. |
| 3 | +The plugin system allows to look at requests and responses and replace them if needed, inside an `HttpClient`. |
4 | 4 |
|
5 |
| -TODO: finalize system and document. |
| 5 | +Using the `Http\Client\Plugin\PluginClient`, you can inject an `HttpClient`, or an `HttpAsyncClient`, and an array |
| 6 | +of plugins implementing the `Http\Client\Plugin\Plugin` interface. |
| 7 | + |
| 8 | +Each plugin can replace the `RequestInterface` sent or the `ResponseInterface` received. It can also change the behavior of a call, |
| 9 | +like retrying the request or emit another one when a redirection response was received. |
| 10 | + |
| 11 | +## Install |
| 12 | + |
| 13 | +Install the plugin client in your project with composer: |
| 14 | + |
| 15 | +``` bash |
| 16 | +composer require "php-http/plugins:^1.0" |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +First you need to have some plugins: |
| 22 | + |
| 23 | +```php |
| 24 | +use Http\Client\Plugin\RetryPlugin; |
| 25 | +use Http\Client\Plugin\RedirectPlugin; |
| 26 | + |
| 27 | +$retryPlugin = new RetryPlugin(); |
| 28 | +$redirectPlugin = new RedirectPlugin(); |
| 29 | +``` |
| 30 | + |
| 31 | +Then you can create a `PluginClient`: |
| 32 | + |
| 33 | +```php |
| 34 | +use Http\Discovery\HttpClientDiscovery; |
| 35 | +use Http\Client\Plugin\PluginClient; |
| 36 | + |
| 37 | +... |
| 38 | + |
| 39 | +$pluginClient = new PluginClient(HttpClientDiscovery::find(), [ |
| 40 | + $retryPlugin, |
| 41 | + $redirectPlugin |
| 42 | +]); |
| 43 | +``` |
| 44 | + |
| 45 | +You can use the plugin client like a classic `Http\Client\HttpClient` or `Http\Client\HttpAsyncClient` one: |
| 46 | + |
| 47 | +```php |
| 48 | +// Send a request |
| 49 | +$response = $pluginClient->sendRequest($request); |
| 50 | + |
| 51 | +// Send an asynchronous request |
| 52 | +$promise = $pluginClient->sendAsyncRequest($request); |
| 53 | +``` |
| 54 | + |
| 55 | +Go to the [tutorial](tutorial.md) to read more about using `HttpClient` and `HttpAsyncClient` |
| 56 | + |
| 57 | +## Available plugins |
| 58 | + |
| 59 | +Each plugin has its own configuration and dependencies, check the documentation for each of the available plugins: |
| 60 | + |
| 61 | + - [Authentication](plugins/authentication.md): Add authentication header on a request |
| 62 | + - [Cookie](plugins/cookie.md): Add cookies to request and save them from the response |
| 63 | + - [Encoding](plugins/encoding.md): Add support for receiving chunked, deflate or gzip response |
| 64 | + - [Error](plugins/error.md): Transform bad response (400 to 599) to exception |
| 65 | + - [Redirect](plugins/redirect.md): Follow redirection coming from 3XX responses |
| 66 | + - [Retry](plugins/retry.md): Retry a failed call |
| 67 | + - [Stopwatch](plugins/stopwatch.md): Log time of a request call by using [the Symfony Stopwatch component](http://symfony.com/doc/current/components/stopwatch.html) |
| 68 | + |
| 69 | +## Order of plugins |
| 70 | + |
| 71 | +When you inject an array of plugins into the `PluginClient`, the order of the plugins matters. |
| 72 | + |
| 73 | +During the request, plugins are called in the order they have in the array, from first to last plugin. Once a response has been received, |
| 74 | +they are called again in inverse order, from last to first. |
| 75 | + |
| 76 | +i.e. with the following code: |
| 77 | + |
| 78 | +```php |
| 79 | +use Http\Discovery\HttpClientDiscovery; |
| 80 | +use Http\Client\Plugin\PluginClient; |
| 81 | +use Http\Client\Plugin\RetryPlugin; |
| 82 | +use Http\Client\Plugin\RedirectPlugin; |
| 83 | + |
| 84 | +$retryPlugin = new RetryPlugin(); |
| 85 | +$redirectPlugin = new RedirectPlugin(); |
| 86 | + |
| 87 | +$pluginClient = new PluginClient(HttpClientDiscovery::find(), [ |
| 88 | + $retryPlugin, |
| 89 | + $redirectPlugin |
| 90 | +]); |
| 91 | +``` |
| 92 | + |
| 93 | +The execution chain will look like this: |
| 94 | + |
| 95 | +``` |
| 96 | +Request ---> PluginClient ---> RetryPlugin ---> RedirectPlugin ---> HttpClient ---- |
| 97 | + | (processing call) |
| 98 | +Response <--- PluginClient <--- RetryPlugin <--- RedirectPlugin <--- HttpClient <--- |
| 99 | +``` |
| 100 | + |
| 101 | +In order to have correct behavior over the global process, you need to understand well each plugin used, |
| 102 | +and manage a correct order when passing the array to the `PluginClient` |
| 103 | + |
| 104 | +`RetryPlugin` will be best at the end to optimize the retry process, but it can also be good |
| 105 | +to have it as the first plugin, if one of the plugins is inconsistent and may need a retry. |
| 106 | + |
| 107 | +The recommended way to order plugins is the following rules: |
| 108 | + |
| 109 | + 1. Plugins that modify the request should be at the beginning (like the `AuthenticationPlugin` or the `CookiePlugin`) |
| 110 | + 2. Plugins which intervene in the workflow should be in the "middle" (like the `RetryPlugin` or the `RedirectPlugin`) |
| 111 | + 3. Plugins which log information should be last (like the `LoggerPlugin` or the `HistoryPlugin`) |
| 112 | + |
| 113 | +However, there can be exceptions to these rules. For example, for security reasons you might not want to log the authentication header |
| 114 | +and chose to put the AuthenticationPlugin after the LoggerPlugin. |
| 115 | + |
| 116 | +## Implementing your own Plugin |
| 117 | + |
| 118 | +Read this [documentation](plugins/plugin-implementation.md) if you want to create your own Plugin. |
0 commit comments