diff options
author | Arnold Daniels <arnold@jasny.net> | 2016-11-03 23:45:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-03 23:45:36 +0100 |
commit | f3d05dfca77843a869e938028824f19307780939 (patch) | |
tree | 846c5e559360e530d200d897f67b5af02f8795e5 /src/Router/Middleware | |
parent | aa31c40618b0cc7b43b7a1cb107e97b49e2c06f1 (diff) | |
parent | bd4cb77fbf04923fa31a08fdd1f33f2c0db87864 (diff) | |
download | router-1.0.0.zip router-1.0.0.tar.gz router-1.0.0.tar.bz2 |
Merge pull request #13 from jasny/fix-testsv1.0.0
Fix tests
Diffstat (limited to 'src/Router/Middleware')
-rw-r--r-- | src/Router/Middleware/BasePath.php | 24 | ||||
-rw-r--r-- | src/Router/Middleware/ErrorHandler.php | 54 | ||||
-rw-r--r-- | src/Router/Middleware/NotFound.php | 49 |
3 files changed, 41 insertions, 86 deletions
diff --git a/src/Router/Middleware/BasePath.php b/src/Router/Middleware/BasePath.php index a80d029..f356aed 100644 --- a/src/Router/Middleware/BasePath.php +++ b/src/Router/Middleware/BasePath.php @@ -57,13 +57,15 @@ class BasePath $uri = $request->getUri(); $path = $this->normalizePath($uri->getPath()); - if (!$this->hasBasePath($path)) return $this->setError($response); + if (!$this->hasBasePath($path)) { + return $this->notFound($request, $response); + } $noBase = $this->getBaselessPath($path); $noBaseUri = $uri->withPath($noBase); - $request = $request->withUri($noBaseUri)->withAttribute('original_uri', $uri); + $rewrittenRequest = $request->withUri($noBaseUri)->withAttribute('original_uri', $uri); - return call_user_func($next, $request, $response); + return $next($rewrittenRequest, $response); } /** @@ -100,19 +102,17 @@ class BasePath } /** - * Set error response + * Respond with 404 Not Found * - * @param ResponseInterface $response + * @param ServerRequestInterface $request + * @param ResponseInterface $response * @return ResponseInterface */ - protected function setError($response) + protected function notFound(ServerRequestInterface $request, ResponseInterface $response) { - $message = 'Not Found'; - - $body = $response->getBody(); - $body->rewind(); - $body->write($message); + $notFound = $response->withStatus(404); + $notFound->getBody()->write('Not Found'); - return $response->withStatus(404, $message)->withBody($body); + return $notFound; } } diff --git a/src/Router/Middleware/ErrorHandler.php b/src/Router/Middleware/ErrorHandler.php deleted file mode 100644 index 789c455..0000000 --- a/src/Router/Middleware/ErrorHandler.php +++ /dev/null @@ -1,54 +0,0 @@ -<?php - -namespace Jasny\Router\Middleware; - -use Psr\Http\Message\ServerRequestInterface; -use Psr\Http\Message\ResponseInterface; - -/** - * Handle error in following middlewares/app actions - */ -class ErrorHandler -{ - /** - * Run middleware action - * - * @param ServerRequestInterface $request - * @param ResponseInterface $response - * @param callback $next - * @return ResponseInterface - */ - public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next = null) - { - if ($next && !is_callable($next)) { - throw new \InvalidArgumentException("'next' should be a callback"); - } - - $error = false; - - try { - $response = $next ? call_user_func($next, $request, $response) : $response; - } catch(\Throwable $e) { - $error = true; - } catch(\Exception $e) { #This block can be removed when migrating to PHP7, because Throwable represents both Exception and Error - $error = true; - } - - return $error ? $this->handleError($response) : $response; - } - - /** - * Handle caught error - * - * @param ResponseInterface $response - * @return ResponseInterface - */ - protected function handleError($response) - { - $body = $response->getBody(); - $body->rewind(); - $body->write('Unexpected error'); - - return $response->withStatus(500, 'Internal Server Error')->withBody($body); - } -} diff --git a/src/Router/Middleware/NotFound.php b/src/Router/Middleware/NotFound.php index 97b4a51..2484a43 100644 --- a/src/Router/Middleware/NotFound.php +++ b/src/Router/Middleware/NotFound.php @@ -29,6 +29,7 @@ class NotFound **/ protected $methodNotAllowed = null; + /** * Class constructor * @@ -38,12 +39,22 @@ class NotFound */ public function __construct(Routes $routes, $notFound = 404, $methodNotAllowed = null) { - if (!(is_numeric($notFound) && $notFound >= 100 && $notFound <= 599) && !is_callable($notFound)) { - throw new \InvalidArgumentException("'Not found' parameter should be a code in range 100-599 or a callback"); + if (is_string($notFound) && ctype_digit($notFound)) { + $notFound = (int)$notFound; + } + if (!(is_int($notFound) && $notFound >= 100 && $notFound <= 999) && !is_callable($notFound)) { + throw new \InvalidArgumentException("'notFound' should be valid HTTP status code or a callback"); } - if ($methodNotAllowed && !(is_numeric($methodNotAllowed) && $methodNotAllowed >= 100 && $methodNotAllowed <= 599) && !is_callable($methodNotAllowed)) { - throw new \InvalidArgumentException("'Method not allowed' parameter should be a code in range 100-599 or a callback"); + if (is_string($methodNotAllowed) && ctype_digit($methodNotAllowed)) { + $methodNotAllowed = (int)$methodNotAllowed; + } + if ( + isset($methodNotAllowed) && + !(is_int($methodNotAllowed) && $methodNotAllowed >= 100 && $methodNotAllowed <= 999) && + !is_callable($methodNotAllowed) + ) { + throw new \InvalidArgumentException("'methodNotAllowed' should be valid HTTP status code or a callback"); } $this->routes = $routes; @@ -60,6 +71,7 @@ class NotFound { return $this->routes; } + /** * Run middleware action @@ -69,37 +81,34 @@ class NotFound * @param callback $next * @return ResponseInterface */ - public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next = null) + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next) { - if ($next && !is_callable($next)) { + if (!is_callable($next)) { throw new \InvalidArgumentException("'next' should be a callback"); } - if ($this->getRoutes()->hasRoute($request)) { - return $next ? $next($request, $response) : $response; - } - - $status = $this->methodNotAllowed && $this->getRoutes()->hasRoute($request, false) ? - $this->methodNotAllowed : $this->notFound; + if (!$this->getRoutes()->hasRoute($request)) { + $status = $this->methodNotAllowed && $this->getRoutes()->hasRoute($request, false) ? + $this->methodNotAllowed : $this->notFound; - return is_numeric($status) ? $this->simpleResponse($response, $status) : call_user_func($status, $request, $response); + return is_numeric($status) ? $this->simpleResponse($response, $status) : $status($request, $response); + } + + return $next($request, $response); } /** * Simple response * * @param ResponseInterface $response - * @param int $code + * @param int $code * @return ResponseInterface */ protected function simpleResponse(ResponseInterface $response, $code) { - $message = 'Not Found'; - - $body = $response->getBody(); - $body->rewind(); - $body->write($message); + $notFound = $response->withStatus($code); + $notFound->getBody()->write('Not found'); - return $response->withStatus($code, $message)->withBody($body); + return $notFound; } } |