Skip to content

Commit 7c5225f

Browse files
committed
Added MiddlewareInterface to the request handler so it can be used either as RequestHandler or as middleware. Introduce BC break
1 parent c60e68d commit 7c5225f

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## [2.0.0] - 2019-07-12
8+
### Added
9+
- RequestHandler supports MiddlewareInterface to be use as a middleware as well
10+
11+
### Changed
12+
- Constructor arguments order for RequestHandler
13+
- Default handler is now optional
14+
15+
## [1.1.0] - 2019-06-26
16+
### Added
17+
- Handful factory methods
18+
- Updated documentation
19+
20+
## [1.0.0] - 2019-06-23
21+
- Initial release

README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,31 @@ Comes with a set of middleware collections using different strategy (LIFO, FIFO.
3434

3535
#### Run
3636

37-
Instantiate the RequestHandler class. It requires ony 2 arguments:
37+
Create a new instance of the request handler class which can be use as a request handler or as a middleware.
3838

39-
- ***`$defaultRequestHandler`*** : [RequestHandlerInterface](https://github.com/php-fig/http-server-handler/blob/master/src/RequestHandlerInterface.php)
39+
##### From the constructor
4040

41-
***The default request handler MUST provide a default response if none of the middlewares created one.***
41+
`RequestHandler::__construct(MiddlewareCollectionInterface $middlewareCollection, RequestHandlerInterface $defaultRequestHandler = null)`
42+
43+
- ***`$middlewareCollection`*** : [MiddlewareCollectionInterface](https://github.com/noglitchyo/middleware-collection-request-handler/blob/master/src/MiddlewareCollectionInterface.php)
44+
45+
Contains the middlewares and defines the strategy used to store the middlewares and to retrieve the next middleware.
46+
Some implementations with common strategies are provided: [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).
47+
48+
- ***`$defaultRequestHandler = null`*** : [RequestHandlerInterface](https://github.com/php-fig/http-server-handler/blob/master/src/RequestHandlerInterface.php)
49+
50+
Provides a default response implementing [ResponseInterface](https://github.com/php-fig/http-message/blob/master/src/ResponseInterface.php) if none of the middlewares in the collection was able to create one.
4251

4352
Some examples of what could be a "default request handler":
4453
- 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.*
4554
- 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.
4655

47-
It is possible to directly provide a `callable` and use the factory method `RequestHandler::fromCallable(callable $callable)`.
48-
It will create an anonymous instance of RequestHandlerInterface wrapping the given `callable` inside.
49-
50-
- ***`$middlewareCollection`*** : [MiddlewareCollectionInterface](https://github.com/noglitchyo/middleware-collection-request-handler/blob/master/src/MiddlewareCollectionInterface.php)
56+
##### From the factory method
5157

52-
Contains the middlewares and defines the strategy used to store the middlewares and to retrieve the next middleware.
53-
Some implementations with common strategies are provided: [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).
58+
`RequestHandler::fromCallable(callable $callable, MiddlewareCollectionInterface $middlewareCollection)`
59+
60+
It creates a RequestHandler instance by wrapping the given `callable` inside an anonymous instance of RequestHandlerInterface.
61+
The callable is the $defaultRequestHandler. **It MUST returns a response implementing [ResponseInterface](https://github.com/php-fig/http-message/blob/master/src/ResponseInterface.php).**
5462

5563
##### Example
5664

@@ -88,6 +96,7 @@ $requestHandler = RequestHandler::fromCallable(
8896
$middlewareCollection
8997
);
9098

99+
// As a RequestHandler:
91100
// Pass the request to the request handler which will dispatch the request to the middlewares.
92101
$response = $requestHandler->handle(/* ServerRequestInterface */);
93102

0 commit comments

Comments
 (0)