Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Use response facttories in ErrorHandler and NotFoundHandler #153

Merged
merged 5 commits into from
Feb 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ Versions prior to 1.0 were originally released as `phly/conduit`; please visit
its [CHANGELOG](https://github.com/phly/conduit/blob/master/CHANGELOG.md) for
details.

## 3.0.0alpha4 - TBD

### Added

- Nothing.

### Changed

- [#153](https://github.com/zendframework/zend-stratigility/pull/153) modifies
the first argument of the `Zend\Expressive\Middleware\ErrorHandler` and
`NotFoundHandler` classes. Previously, they each expected a
`Psr\Http\Message\ResponseInterface` instance; they now both expect a PHP
callable capable of producing such an instance. This change was done to
simplify re-use of a service for producing unique response instances within
dependency injection containers.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Nothing.

## 3.0.0alpha3 - 2018-02-05

### Added
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"require": {
"php": "^7.1",
"fig/http-message-util": "^1.1",
"psr/http-message": "^1.0",
"psr/http-server-middleware": "^1.0",
"zendframework/zend-escaper": "^2.3"
Expand Down
130 changes: 90 additions & 40 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions docs/book/v3/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ internal logic.
`Psr\Http\Server\RequestHandlerInterface`, and provides a return typehint of
`Psr\Http\Message\ResponseInterface`.

- `ErrorHandler::__construct()` and `NotFoundHandler::__construct()`: the first
parameter of each constructor now expects a PHP `callable` capable of
returning a PSR-7 `ResponseInterface` instance (instead of typehinting
directly against `ResponseInterface`). This paves the way for usage with the
upcoming PSR-17 (HTTP Message Factories) specification, and simplifies re-use
of a dependency injection container service (as otherwise you would need to
specify a discrete service per class that expects a response prototype, due to
mutability of the response body).

### Class additions

- `Zend\Stratigility\MiddlewarePipeInterface` extends
Expand Down
17 changes: 10 additions & 7 deletions src/Middleware/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,22 @@ final class ErrorHandler implements MiddlewareInterface
private $responseGenerator;

/**
* @var ResponseInterface
* @var callable
*/
private $responsePrototype;
private $responseFactory;

/**
* @param ResponseInterface $responsePrototype Empty/prototype response to
* update and return when returning an error response.
* @param callable $responseFactory A factory capable of returning an
* empty ResponseInterface instance to update and return when returning
* an error response.
* @param null|callable $responseGenerator Callback that will generate the final
* error response; if none is provided, ErrorResponseGenerator is used.
*/
public function __construct(ResponseInterface $responsePrototype, callable $responseGenerator = null)
public function __construct(callable $responseFactory, callable $responseGenerator = null)
{
$this->responsePrototype = $responsePrototype;
$this->responseFactory = function () use ($responseFactory) : ResponseInterface {
return $responseFactory();
};
$this->responseGenerator = $responseGenerator ?: new ErrorResponseGenerator();
}

Expand Down Expand Up @@ -155,7 +158,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
private function handleThrowable(Throwable $e, ServerRequestInterface $request) : ResponseInterface
{
$generator = $this->responseGenerator;
$response = $generator($e, $request, $this->responsePrototype);
$response = $generator($e, $request, ($this->responseFactory)());
$this->triggerListeners($e, $request, $response);
return $response;
}
Expand Down
Loading