You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+88-16Lines changed: 88 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,38 +13,110 @@ Lightweight & simple PSR-15 server request handler implementation to handle midd
13
13
Request handler implementing the [RequestHandlerInterface](https://github.com/php-fig/http-server-handler/blob/master/src/RequestHandlerInterface.php)
14
14
and able to manage a collection of middlewares implementing the [MiddlewareInterface](https://github.com/php-fig/http-server-middleware/blob/master/src/MiddlewareInterface.php).
15
15
16
-
This middleware attempts to provide interoperability to process a collection of middlewares and
17
-
give the possibility to define the strategy on how middlewares are fetched.
16
+
This request handler attempts to provide interoperability to process a collection of middlewares and
17
+
give the possibility to define the strategy on how middlewares will be processed.
***Remember: the default request handler is responsible for providing a default response.***
35
+
***The default request handler is responsible to provide a default response if none of the middlewares created one.***
37
36
38
-
- A middleware collection (implementing [MiddlewareCollectionInterface](https://github.com/noglitchyo/middleware-collection-request-handler/blob/master/src/MiddlewareCollectionInterface.php))
37
+
Some examples of "default request handler":
38
+
- with the [ADR pattern](https://en.wikipedia.org/wiki/Action%E2%80%93domain%E2%80%93responder), the default request handler might be your action class.
39
+
- with the [MVC pattern](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller), the default request handler might be the action method of your controller.
39
40
40
-
It defines how you store your middlewares and the strategy to retrieve them.
41
-
Some implementations are provided by default and will probably fit your needs.
42
-
They offers different strategies varying from the Stack to the Queue.
41
+
It is possible to directly provide a `callable` using the factory method `RequestHandler::fromCallable()`.
42
+
It will generate a generic instance of RequestHandlerInterface wrapping the `callable` inside.
43
43
44
-
However, it is really simple to create you own collection if needed, since MiddlewareCollectionInterface requires only 3 methods.
Contains the middlewares and encapsulate the strategy used to store and retrieve the middlewares.
47
+
Some standard implementations are provided with different strategies: [stack (LIFO)](https://github.com/noglitchyo/middleware-collection-request-handler/blob/master/src/Collection/SplStackMiddlewareCollection.php), [queue (FIFO)](https://github.com/noglitchyo/middleware-collection-request-handler/blob/master/src/Collection/SplQueueMiddlewareCollection.php).
48
+
49
+
##### Example
50
+
51
+
Below, this is how simple it is to get your middleware stack running:
52
+
53
+
```php
54
+
<?php
55
+
56
+
use NoGlitchYo\MiddlewareCollectionRequestHandler\RequestHandler;
57
+
use NoGlitchYo\MiddlewareCollectionRequestHandler\Collection\SplStackMiddlewareCollection;
58
+
use Psr\Http\Server\MiddlewareInterface;
59
+
use Psr\Http\Message\ServerRequestInterface;
60
+
use Psr\Http\Server\RequestHandlerInterface;
61
+
use Psr\Http\Message\ResponseInterface;
62
+
63
+
// Instantiate a collection of middleware.
64
+
$middlewareCollection = new SplStackMiddlewareCollection([
65
+
new class implements MiddlewareInterface{
66
+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface{
67
+
$handler->handle($request);
68
+
}
69
+
}
70
+
]);
71
+
72
+
// Instantiate a new request handler with a default handler and the middleware collection.
0 commit comments