From 17e1d1d18465d509ad7eaffdffd8f11290ea62b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Mon, 19 Aug 2019 14:27:40 +0200 Subject: [PATCH 1/4] Creating the `NotFoundHandler` in the correct namespace As the `NotFoundHandler` technically is a `RequestHandler` and not a `Middleware`, we should pass it to the proper namespace. --- src/Handler/NotFoundHandler.php | 57 ++++++++++++++++++++++++++++ test/Handler/NotFoundHandlerTest.php | 44 +++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/Handler/NotFoundHandler.php create mode 100644 test/Handler/NotFoundHandlerTest.php diff --git a/src/Handler/NotFoundHandler.php b/src/Handler/NotFoundHandler.php new file mode 100644 index 0000000..438eb21 --- /dev/null +++ b/src/Handler/NotFoundHandler.php @@ -0,0 +1,57 @@ +responseFactory = function () use ($responseFactory) : ResponseInterface { + return $responseFactory(); + }; + } + + /** + * Creates and retursn a 404 response. + */ + public function handle(ServerRequestInterface $request): ResponseInterface + { + /** @var ResponseInterface $response */ + $response = ($this->responseFactory)() + ->withStatus(StatusCode::STATUS_NOT_FOUND); + $response->getBody()->write(sprintf( + 'Cannot %s %s', + $request->getMethod(), + (string) $request->getUri() + )); + return $response; + } +} diff --git a/test/Handler/NotFoundHandlerTest.php b/test/Handler/NotFoundHandlerTest.php new file mode 100644 index 0000000..0902075 --- /dev/null +++ b/test/Handler/NotFoundHandlerTest.php @@ -0,0 +1,44 @@ +prophesize(StreamInterface::class); + $stream->write('Cannot POST https://example.com/foo'); + + $response = $this->prophesize(ResponseInterface::class); + $response->withStatus(404)->will([$response, 'reveal']); + $response->getBody()->will([$stream, 'reveal']); + + $request = $this->prophesize(ServerRequestInterface::class); + $request->getMethod()->willReturn('POST'); + $request->getUri()->willReturn('https://example.com/foo'); + + $responseFactory = function () use ($response) { + return $response->reveal(); + }; + + $middleware = new NotFoundHandler($responseFactory); + + $this->assertSame( + $response->reveal(), + $middleware->handle($request->reveal()) + ); + } +} From 9f4444c09ea857ed015856f41dfc7a227c190351 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Mon, 19 Aug 2019 14:30:12 +0200 Subject: [PATCH 2/4] Mark `NotFoundHandler` as deprecated While marking the `NotFoundHandler` in the `Middleware` namespace as deprecated, we are piping all the logic to the right `NotFoundHandler` by just invoking its `RequestHandlerInterface::handle` method --- src/Middleware/NotFoundHandler.php | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/Middleware/NotFoundHandler.php b/src/Middleware/NotFoundHandler.php index 0e3ca0f..8e27fdd 100644 --- a/src/Middleware/NotFoundHandler.php +++ b/src/Middleware/NotFoundHandler.php @@ -16,13 +16,18 @@ use Psr\Http\Server\RequestHandlerInterface; use function sprintf; +use Zend\Stratigility\Handler\NotFoundHandler as NotFoundRequestHandler; +/** + * @deprecated Will be removed in v4 in favor of {@see \Zend\Stratigility\Handler\NotFoundHandler} + */ final class NotFoundHandler implements MiddlewareInterface { + /** - * @var callable + * @var NotFoundRequestHandler */ - private $responseFactory; + private $notFoundHandler; /** * @param callable $responseFactory A factory capable of returning an @@ -31,23 +36,14 @@ final class NotFoundHandler implements MiddlewareInterface */ public function __construct(callable $responseFactory) { - $this->responseFactory = function () use ($responseFactory) : ResponseInterface { - return $responseFactory(); - }; + $this->notFoundHandler = new NotFoundRequestHandler($responseFactory); } /** - * Creates and returns a 404 response. + * Uses the {@see \Zend\Stratigility\Handler\NotFoundHandler} to create a 404 response. */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface { - $response = ($this->responseFactory)() - ->withStatus(StatusCode::STATUS_NOT_FOUND); - $response->getBody()->write(sprintf( - 'Cannot %s %s', - $request->getMethod(), - (string) $request->getUri() - )); - return $response; + return $this->notFoundHandler->handle($request); } } From 3c1698965e200152bc1774cc14331cdffb2354f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Mon, 19 Aug 2019 14:32:24 +0200 Subject: [PATCH 3/4] Code cleanup --- src/Handler/NotFoundHandler.php | 3 --- src/Middleware/NotFoundHandler.php | 3 --- 2 files changed, 6 deletions(-) diff --git a/src/Handler/NotFoundHandler.php b/src/Handler/NotFoundHandler.php index 438eb21..9995172 100644 --- a/src/Handler/NotFoundHandler.php +++ b/src/Handler/NotFoundHandler.php @@ -17,9 +17,6 @@ use function sprintf; -/** - * @todo Remove the `MiddlewareInterface` implementation in v4. - */ final class NotFoundHandler implements RequestHandlerInterface { /** diff --git a/src/Middleware/NotFoundHandler.php b/src/Middleware/NotFoundHandler.php index 8e27fdd..aac04cc 100644 --- a/src/Middleware/NotFoundHandler.php +++ b/src/Middleware/NotFoundHandler.php @@ -9,13 +9,10 @@ namespace Zend\Stratigility\Middleware; -use Fig\Http\Message\StatusCodeInterface as StatusCode; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; - -use function sprintf; use Zend\Stratigility\Handler\NotFoundHandler as NotFoundRequestHandler; /** From 348124184722a8976a38a61f06cf8807313d44c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Thu, 5 Dec 2019 16:21:40 +0100 Subject: [PATCH 4/4] Fixed typo --- src/Handler/NotFoundHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Handler/NotFoundHandler.php b/src/Handler/NotFoundHandler.php index 9995172..149d01e 100644 --- a/src/Handler/NotFoundHandler.php +++ b/src/Handler/NotFoundHandler.php @@ -37,7 +37,7 @@ public function __construct(callable $responseFactory) } /** - * Creates and retursn a 404 response. + * Creates and returns a 404 response. */ public function handle(ServerRequestInterface $request): ResponseInterface {