Skip to content

Commit b01c61c

Browse files
committed
Fix typos
1 parent b2c1be0 commit b01c61c

File tree

1 file changed

+31
-39
lines changed

1 file changed

+31
-39
lines changed

docs/plugins.md

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Plugin System
22

3-
The plugin system allow to manipulate requests and responses inside an `HttpClient`.
3+
The plugin system allows to look at requests and responses and replace them if needed, inside an `HttpClient`.
44

5-
By using the `Http\Client\Plugin\PluginClient` you can inject an `HttpClient` or an `HttpAsyncClient`, and an array
5+
Using the `Http\Client\Plugin\PluginClient`, you can inject an `HttpClient`, or an `HttpAsyncClient`, and an array
66
of plugins implementing the `Http\Client\Plugin\Plugin` interface.
77

8-
Each plugin can modify 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 is present.
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.
1010

1111
## Install
1212

@@ -42,7 +42,7 @@ $pluginClient = new PluginClient(HttpClientDiscovery::find(), [
4242
]);
4343
```
4444

45-
After you can use this plugin client like a classic `Http\Client\HttpClient` or `Http\Client\HttpAsyncClient` one:
45+
You can use the plugin client like a classic `Http\Client\HttpClient` or `Http\Client\HttpAsyncClient` one:
4646

4747
```php
4848
// Send a request
@@ -56,25 +56,24 @@ Go to the [tutorial](tutorial.md) to read more about using `HttpClient` and `Htt
5656

5757
## Available plugins
5858

59-
Each plugin have its own configuration and dependencies, check the documentation for each of the available plugin:
59+
Each plugin has its own configuration and dependencies, check the documentation for each of the available plugins:
6060

6161
- [Authentication](plugins/authentication.md): Add authentication header on a request
62-
- [Cookie](plugins/cookie.md): Add cookie to request and save them from the response by using a CookieJar
63-
- [Encoding](plugins/encoding.md): Add support for receiving chunked, deflate or gzip response
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
6464
- [Error](plugins/error.md): Transform bad response (400 to 599) to exception
6565
- [Redirect](plugins/redirect.md): Follow redirection coming from 3XX responses
6666
- [Retry](plugins/retry.md): Retry a failed call
67-
- [Stopwatch](plugins/stopwatch.md): Log time of a request call by using the Stopwatch component
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)
6868

6969
## Order of plugins
7070

71-
When you inject an array of plugins into the `PluginClient`, order of the plugins in array matters.
71+
When you inject an array of plugins into the `PluginClient`, the order of the plugins, injected into the `PluginClient`, matters.
7272

73-
Plugins are transformed into a plugin chain, where the first element in array will be called first, and,
74-
obviously, the last element will be called at the end. However when the `ResponseInterface` or the `Promise` is
75-
received from the underlying client, this execution chain is revert and the last plugin defined will be called first.
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.
7675

77-
i.e. with the following code:
76+
i.e. with the following code:
7877

7978
```php
8079
use Http\Discovery\HttpClientDiscovery;
@@ -102,26 +101,26 @@ Response <--- PluginClient <--- RetryPlugin <--- RedirectPlugin <--- HttpClient
102101
In order to have correct behavior over the global process, you need to understand well each plugin used,
103102
and manage a correct order when passing the array to the `PluginClient`
104103

105-
`RetryPlugin` will be best at the end to optimize the retry process, but it can also be good
104+
`RetryPlugin` will be best at the end to optimize the retry process, but it can also be good
106105
to have it as the first plugin, if one in the chain is inconsistent and may need a retry.
107106

108-
Our recommendation, which apply to most of use cases, is to organize your plugins with following rules:
107+
The recommended way to order plugins is the following rules:
109108

110109
1. Plugins that modify the request should be at the beginning (like the `AuthenticationPlugin` or the `CookiePlugin`)
111110
2. Plugins which intervene in the workflow should be in the "middle" (like the `RetryPlugin` or the `RedirectPlugin`)
112-
3. Plugins which log and a debug information about a call should be last (like the `LoggerPlugin` or the `HistoryPlugin`)
111+
3. Plugins which log information should be last (like the `LoggerPlugin` or the `HistoryPlugin`)
113112

114-
However there may be exception in this rule, if, for security reason, you don't want to log the authentication header, it will be better to put
115-
this plugin after the `LoggerPlugin` one.
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.
116115

117116
## Implementing your own Plugin
118117

119-
When writing your own Plugin, you need to be aware that the `PluginClient` is async first. It means that every plugin must
120-
be written by respecting the `HttpAsyncClient` contract and use `Promise` as the return.
118+
When writing your own Plugin, you need to be aware that the `PluginClient` is async first. This means that every plugin must
119+
be written by respecting the `HttpAsyncClient` contract and returns a `Promise`.
121120

122121
Each plugin must implement the `Http\Client\Plugin\Plugin` interface.
123122

124-
This interface defined the `handleRequest` method which allow to modify behavior of the call:
123+
This interface defines the `handleRequest` method that allows to modify behavior of the call:
125124

126125
```php
127126
/**
@@ -136,31 +135,24 @@ This interface defined the `handleRequest` method which allow to modify behavior
136135
public function handleRequest(RequestInterface $request, callable $next, callable $first);
137136
```
138137

139-
The `$request` is the one created by the client, you can transform it, or do what you like with it. Always be aware that
138+
The `$request` comes from upstream. You can replace it and pass a new version downstream if you need. Always be aware that
140139
the request is immutable.
141140

142-
```
143-
public function handleRequest(RequestInterface $request, callable $next, callable $first)
144-
{
145-
$newRequest = $request->withHeader('MyHeader', 'MyValue');
146-
}
147-
```
148-
149-
The `$next` callable is the next plugin in the execution chain. When you need to call it, you must pass the `$request`
141+
The `$next` callable is the next plugin in the execution chain. When you need to call it, you must pass the `$request`
150142
as the first argument of this callable.
151143

152144
```
153145
public function handleRequest(RequestInterface $request, callable $next, callable $first)
154146
{
155147
$newRequest = $request->withHeader('MyHeader', 'MyValue');
156-
157-
return $next($request);
148+
149+
return $next($newRequest);
158150
}
159151
```
160152

161153

162-
The `$first` callable is the first plugin in the execution. It allows you to completely reboot the execution chain, or sends
163-
other request if needed while still using all the plugins defined. Like the `$next` callable, you must pass the `$request`
154+
The `$first` callable is the first plugin in the execution. It allows you to completely reboot the execution chain, or sends
155+
other request if needed while still using all the plugins defined. Like the `$next` callable, you must pass the `$request`
164156
as the first argument of this callable.
165157

166158
```
@@ -169,10 +161,10 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
169161
if ($someCondition) {
170162
$newRequest = new Request();
171163
$promise = $first($newRequest);
172-
164+
173165
// Use the promise do some jobs ...
174166
}
175-
167+
176168
return $next($request);
177169
}
178170
```
@@ -187,12 +179,12 @@ by using the `then` method of the promise.
187179
public function handleRequest(RequestInterface $request, callable $next, callable $first)
188180
{
189181
$newRequest = $request->withHeader('MyHeader', 'MyValue');
190-
182+
191183
return $next($request)->then(function (ResponseInterface $response) {
192184
return $response->withHeader('MyResponseHeader', 'value');
193185
}, function (Exception $exception) {
194186
echo $exception->getMessage();
195-
187+
196188
throw $exception;
197189
});
198190
}

0 commit comments

Comments
 (0)