Skip to content

Commit 5a68680

Browse files
committed
Separate plugin implementation for plugin documentation
1 parent b01c61c commit 5a68680

File tree

2 files changed

+80
-77
lines changed

2 files changed

+80
-77
lines changed

docs/plugins.md

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -115,80 +115,4 @@ and chose to put the AuthenticationPlugin after the LoggerPlugin.
115115

116116
## Implementing your own Plugin
117117

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`.
120-
121-
Each plugin must implement the `Http\Client\Plugin\Plugin` interface.
122-
123-
This interface defines the `handleRequest` method that allows to modify behavior of the call:
124-
125-
```php
126-
/**
127-
* handle the request and return the response coming from the next callable
128-
*
129-
* @param RequestInterface $request Request to use
130-
* @param callable $next Callback to call to have the request, it muse have the request as it first argument
131-
* @param callable $first First element in the plugin chain, used to to restart a request from the beginning
132-
*
133-
* @return Promise
134-
*/
135-
public function handleRequest(RequestInterface $request, callable $next, callable $first);
136-
```
137-
138-
The `$request` comes from upstream. You can replace it and pass a new version downstream if you need. Always be aware that
139-
the request is immutable.
140-
141-
The `$next` callable is the next plugin in the execution chain. When you need to call it, you must pass the `$request`
142-
as the first argument of this callable.
143-
144-
```
145-
public function handleRequest(RequestInterface $request, callable $next, callable $first)
146-
{
147-
$newRequest = $request->withHeader('MyHeader', 'MyValue');
148-
149-
return $next($newRequest);
150-
}
151-
```
152-
153-
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`
156-
as the first argument of this callable.
157-
158-
```
159-
public function handleRequest(RequestInterface $request, callable $next, callable $first)
160-
{
161-
if ($someCondition) {
162-
$newRequest = new Request();
163-
$promise = $first($newRequest);
164-
165-
// Use the promise do some jobs ...
166-
}
167-
168-
return $next($request);
169-
}
170-
```
171-
172-
In this example the condition is not superfluous, you need to have some way to not calling the $first callable each time or
173-
you will end up with a infinite execution loop.
174-
175-
The `$next` and `$first` callable will return a `Promise`. You can manipulate the `ResponseInterface` or the `Exception`
176-
by using the `then` method of the promise.
177-
178-
```
179-
public function handleRequest(RequestInterface $request, callable $next, callable $first)
180-
{
181-
$newRequest = $request->withHeader('MyHeader', 'MyValue');
182-
183-
return $next($request)->then(function (ResponseInterface $response) {
184-
return $response->withHeader('MyResponseHeader', 'value');
185-
}, function (Exception $exception) {
186-
echo $exception->getMessage();
187-
188-
throw $exception;
189-
});
190-
}
191-
```
192-
193-
Anyway it is always a good practice to read existing implementation inside the [plugin repository](https://github.com/php-http/plugins) to better understand the whole
194-
process.
118+
Read this [documentation](plugins/plugin-implementation.md) if you want to create your own Plugin.

docs/plugins/plugin-implementation.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Implementing your own Plugin
2+
3+
When writing your own Plugin, you need to be aware that the `PluginClient` is async first. This means that every plugin must
4+
be written by respecting the `HttpAsyncClient` contract and returns a `Promise`.
5+
6+
Each plugin must implement the `Http\Client\Plugin\Plugin` interface.
7+
8+
This interface defines the `handleRequest` method that allows to modify behavior of the call:
9+
10+
```php
11+
/**
12+
* handle the request and return the response coming from the next callable
13+
*
14+
* @param RequestInterface $request Request to use
15+
* @param callable $next Callback to call to have the request, it muse have the request as it first argument
16+
* @param callable $first First element in the plugin chain, used to to restart a request from the beginning
17+
*
18+
* @return Promise
19+
*/
20+
public function handleRequest(RequestInterface $request, callable $next, callable $first);
21+
```
22+
23+
The `$request` comes from upstream. You can replace it and pass a new version downstream if you need. Always be aware that
24+
the request is immutable.
25+
26+
The `$next` callable is the next plugin in the execution chain. When you need to call it, you must pass the `$request`
27+
as the first argument of this callable.
28+
29+
```
30+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
31+
{
32+
$newRequest = $request->withHeader('MyHeader', 'MyValue');
33+
34+
return $next($newRequest);
35+
}
36+
```
37+
38+
39+
The `$first` callable is the first plugin in the execution. It allows you to completely reboot the execution chain, or sends
40+
other request if needed while still using all the plugins defined. Like the `$next` callable, you must pass the `$request`
41+
as the first argument of this callable.
42+
43+
```
44+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
45+
{
46+
if ($someCondition) {
47+
$newRequest = new Request();
48+
$promise = $first($newRequest);
49+
50+
// Use the promise do some jobs ...
51+
}
52+
53+
return $next($request);
54+
}
55+
```
56+
57+
In this example the condition is not superfluous, you need to have some way to not calling the $first callable each time or
58+
you will end up with a infinite execution loop.
59+
60+
The `$next` and `$first` callable will return a `Promise`. You can manipulate the `ResponseInterface` or the `Exception`
61+
by using the `then` method of the promise.
62+
63+
```
64+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
65+
{
66+
$newRequest = $request->withHeader('MyHeader', 'MyValue');
67+
68+
return $next($request)->then(function (ResponseInterface $response) {
69+
return $response->withHeader('MyResponseHeader', 'value');
70+
}, function (Exception $exception) {
71+
echo $exception->getMessage();
72+
73+
throw $exception;
74+
});
75+
}
76+
```
77+
78+
Anyway it is always a good practice to read existing implementation inside the [plugin repository](https://github.com/php-http/plugins) to better understand the whole
79+
process.

0 commit comments

Comments
 (0)