@@ -115,80 +115,4 @@ and chose to put the AuthenticationPlugin after the LoggerPlugin.
115
115
116
116
## Implementing your own Plugin
117
117
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.
0 commit comments