From a04c1fafd37636e0e6a8f55fc2393f243f242293 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 14 Feb 2021 16:24:47 +0100 Subject: [PATCH 1/4] Refactor to move redirects to new `RedirectHandler` --- src/App.php | 14 ++---------- src/FilesystemHandler.php | 14 ++---------- src/RedirectHandler.php | 33 +++++++++++++++++++++++++++ tests/AppTest.php | 6 +++-- tests/FilesystemHandlerTest.php | 2 ++ tests/RedirectHandlerTest.php | 40 +++++++++++++++++++++++++++++++++ 6 files changed, 83 insertions(+), 26 deletions(-) create mode 100644 src/RedirectHandler.php create mode 100644 tests/RedirectHandlerTest.php diff --git a/src/App.php b/src/App.php index 7b9636d..4b73dd4 100644 --- a/src/App.php +++ b/src/App.php @@ -7,7 +7,6 @@ use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Http\HttpServer; -use React\Http\Message\Response; use React\Http\Message\ServerRequest; use React\Promise\Deferred; use React\Promise\PromiseInterface; @@ -113,18 +112,9 @@ public function map(array $methods, string $route, callable $handler, callable . $this->router->map($methods, $route, $handler, ...$handlers); } - public function redirect($route, $target, $code = 302) + public function redirect(string $route, string $target, int $code = 302): void { - return $this->get($route, function () use ($target, $code) { - return new Response( - $code, - [ - 'Content-Type' => 'text/html', - 'Location' => $target - ], - 'See ' . $target . '...' . "\n" - ); - }); + $this->get($route, new RedirectHandler($target, $code)); } public function run() diff --git a/src/FilesystemHandler.php b/src/FilesystemHandler.php index cdd0f7b..acc2642 100644 --- a/src/FilesystemHandler.php +++ b/src/FilesystemHandler.php @@ -68,12 +68,7 @@ public function __invoke(ServerRequestInterface $request) \clearstatcache(); if ($valid && \is_dir($path)) { if ($local !== '' && \substr($local, -1) !== '/') { - return new Response( - 302, - [ - 'Location' => \basename($path) . '/' - ] - ); + return (new RedirectHandler(\basename($path) . '/'))(); } $response = '' . $this->html->escape($local === '' ? '/' : $local) . '' . "\n