Skip to content

Commit 849488a

Browse files
authored
Merge pull request #4 from jaapio/feature/middleware-locator
Adds middleware locator
2 parents 0a2ebed + ccb9d02 commit 849488a

File tree

7 files changed

+239
-52
lines changed

7 files changed

+239
-52
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,49 @@
77
[![License](https://poser.pugx.org/api-clients/middleware/license.png)](https://packagist.org/packages/api-clients/middleware)
88
[![PHP 7 ready](http://php7ready.timesplinter.ch/php-api-clients/middleware/badge.svg)](https://appveyor-ci.org/php-api-clients/middleware)
99

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+
1053

1154
# License
1255

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
},
1818
"require-dev": {
1919
"api-clients/test-utilities": "^1.0",
20+
"container-interop/container-interop": "^1.1",
2021
"guzzlehttp/psr7": "^1.3"
2122
},
2223
"suggest": {
2324
"api-clients/middleware-cache": "Cache requests with different strategies",
2425
"api-clients/middleware-log": "Log requests and their context",
2526
"api-clients/middleware-oauth1": "Sign requests with oauth1",
26-
"api-clients/middleware-pool": "Pool the maximum concurrent requests"
27+
"api-clients/middleware-pool": "Pool the maximum concurrent requests",
28+
"container-interop/container-interop": "Add a ContainerInterface implementation to use the ContainerLocator"
2729
},
2830
"autoload": {
2931
"psr-4": {

0 commit comments

Comments
 (0)