|
7 | 7 | [](https://packagist.org/packages/api-clients/middleware)
|
8 | 8 | [](https://appveyor-ci.org/php-api-clients/middleware)
|
9 | 9 |
|
| 10 | +This library contains the a async `MiddlewareRunner` used in our api clients. It |
| 11 | +provides an interface for all middlewares. Middlewares are ordered by priority |
| 12 | +when they where added to the `MiddlewareRunner`. Order of middlewares with the |
| 13 | +same priority is not guaranteed. |
| 14 | + |
| 15 | +A number of traits are provided for your convenience, if your middleware |
| 16 | +implementation does not require all the methods defined in the |
| 17 | +`MiddlewareInterface`. |
| 18 | + |
| 19 | +## Locator |
| 20 | +The locator can be used by your application to fetch middleware instances. |
| 21 | +It will check whether the created instance implements the `MiddlewareInterface`. |
| 22 | +Currently the only provided locator is the `ContainerLocator` which accepts a |
| 23 | +`Interop\Container\ContainerInterface` to fetch your middleware instances. |
| 24 | + |
| 25 | +## Example |
| 26 | +```php |
| 27 | + $container = /* ... */; |
| 28 | + $locator = new ContainerLocator($container); |
| 29 | + $middlewares = []; |
| 30 | + |
| 31 | + $config = [ |
| 32 | + 'middlewares' => [/*...*/] |
| 33 | + 'options' => [/*...*/] |
| 34 | + ]; |
| 35 | + |
| 36 | + foreach ($config['middlewares'] as $middleware) { |
| 37 | + $middlewares[] = $locator->get($middleware); |
| 38 | + } |
| 39 | + |
| 40 | + $runner = new MiddlewareRunner($config['options'], $middelwares); |
| 41 | + |
| 42 | + $runner->pre($request)->then(function ($request) use ($options) { |
| 43 | + return resolve($this->browser->send( |
| 44 | + $request |
| 45 | + )); |
| 46 | + }, function (ResponseInterface $response) { |
| 47 | + return resolve($response); |
| 48 | + })->then(function (ResponseInterface $response) use ($runner) { |
| 49 | + return $runner->post($response); |
| 50 | + }); |
| 51 | +``` |
| 52 | + |
10 | 53 |
|
11 | 54 | # License
|
12 | 55 |
|
|
0 commit comments