diff --git a/CHANGELOG.md b/CHANGELOG.md index a35b332..ed13d8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,16 +5,20 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). -## [Unreleased] - -### Fixed - -- Use `phpstan` as a dev dependency to detect bugs +## [1.2.0] - 2018-10-22 ### Added - Added `responseFactory` option to `__construct` +### Deprecated + +- `responseFactory()` option as a method. Use the contructor argument instead. + +### Fixed + +- Use `phpstan` as a dev dependency to detect bugs + ## [1.1.0] - 2018-08-04 ### Added diff --git a/README.md b/README.md index f950e48..0917928 100644 --- a/README.md +++ b/README.md @@ -57,20 +57,35 @@ $response = $dispatcher->dispatch(new ServerRequest('/hello/world')); **FastRoute** allows anything to be defined as the router handler (a closure, callback, action object, controller class, etc). The middleware will store this handler in a request attribute. -## Options +## API -### `__construct(FastRoute\Dispatcher $dispatcher)` +### `__construct` -The dispatcher instance to use. +Type | Required | Description +--------------|----------|------------ +`FastRoute\Dispatcher` | Yes | The dispatcher instance to use. +`Psr\Http\Message\ResponseFactoryInterface` | No | A PSR-17 factory to create the error responses (`404` or `405`). If it's not defined, use [Middleware\Utils\Factory](https://github.com/middlewares/utils#factory) to detect it automatically. -### `attribute(string $attribute)` -The attribute name used to store the handler in the server request. The default attribute name is `request-handler`. +### `attribute` -#### `responseFactory(Psr\Http\Message\ResponseFactoryInterface $responseFactory)` +Changes the attribute name used to store the handler in the server request. The default name is `request-handler`. -A PSR-17 factory to create the error responses (`404` or `405`). +Type | Required | Description +--------------|----------|------------ +`string` | Yes | The new attribute name +```php +$dispatcher = new Dispatcher([ + //Save the route handler in an attribute called "route" + (new Middlewares\FastRoute($dispatcher))->attribute('route'), + + //Execute the route handler + (new Middlewares\RequestHandler())->attribute('route') +]); + +$response = $dispatcher->dispatch(new ServerRequest('/hello/world')); +``` --- Please see [CHANGELOG](CHANGELOG.md) for more information about recent changes and [CONTRIBUTING](CONTRIBUTING.md) for contributing details. diff --git a/src/FastRoute.php b/src/FastRoute.php index cdc7e19..85fa775 100644 --- a/src/FastRoute.php +++ b/src/FastRoute.php @@ -4,8 +4,8 @@ namespace Middlewares; use FastRoute\Dispatcher; -use Middlewares\Utils\Traits\HasResponseFactory; use Middlewares\Utils\Factory; +use Middlewares\Utils\Traits\HasResponseFactory; use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -27,7 +27,7 @@ class FastRoute implements MiddlewareInterface private $attribute = 'request-handler'; /** - * Set the Dispatcher instance. + * Set the Dispatcher instance and optionally the response factory to return the error responses. */ public function __construct(Dispatcher $router, ResponseFactoryInterface $responseFactory = null) {