From a2dbf4149be1fb09089255fa8970cd79764cc986 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Sat, 23 Apr 2016 10:50:56 +0200 Subject: [PATCH 1/2] - Added route to ExceptionHandler so Middlewares can be loaded. --- src/Pecee/SimpleRouter/RouterBase.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Pecee/SimpleRouter/RouterBase.php b/src/Pecee/SimpleRouter/RouterBase.php index 8f58016..27b39e0 100644 --- a/src/Pecee/SimpleRouter/RouterBase.php +++ b/src/Pecee/SimpleRouter/RouterBase.php @@ -2,7 +2,6 @@ namespace Pecee\SimpleRouter; use Pecee\Exception\RouterException; -use Pecee\Handler\IExceptionHandler; use Pecee\Http\Middleware\BaseCsrfVerifier; use Pecee\Http\Request; @@ -89,9 +88,9 @@ class RouterBase { $route->renderRoute($this->request); $mergedSettings = array_merge($settings, $route->getMergeableSettings()); - // Add exceptionhandler + // Add ExceptionHandler if($route->matchRoute($this->request) && $route->getExceptionHandler() !== null) { - $this->exceptionHandlers[] = $route->getExceptionHandler(); + $this->exceptionHandlers[] = $route; } } @@ -178,12 +177,11 @@ class RouterBase { protected function handleException(\Exception $e) { - foreach ($this->exceptionHandlers as $handler) { + /* @var $route RouterEntry */ + foreach ($this->exceptionHandlers as $route) { + $route->loadMiddleware($this->request); + $handler = $route->getExceptionHandler(); $handler = new $handler($this->request); - if (!($handler instanceof IExceptionHandler)) { - throw new RouterException('Exception handler must implement the IExceptionHandler interface.'); - } - $handler->handleError($this->request, $this->request->loadedRoute, $e); } From f98e5ac59d4314fa23817e89b599583a60d0e6b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Sat, 23 Apr 2016 10:52:51 +0200 Subject: [PATCH 2/2] - Optimized handleException method in RouterBase. --- src/Pecee/SimpleRouter/RouterBase.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Pecee/SimpleRouter/RouterBase.php b/src/Pecee/SimpleRouter/RouterBase.php index 27b39e0..10f782b 100644 --- a/src/Pecee/SimpleRouter/RouterBase.php +++ b/src/Pecee/SimpleRouter/RouterBase.php @@ -2,6 +2,7 @@ namespace Pecee\SimpleRouter; use Pecee\Exception\RouterException; +use Pecee\Handler\IExceptionHandler; use Pecee\Http\Middleware\BaseCsrfVerifier; use Pecee\Http\Request; @@ -181,7 +182,12 @@ class RouterBase { foreach ($this->exceptionHandlers as $route) { $route->loadMiddleware($this->request); $handler = $route->getExceptionHandler(); - $handler = new $handler($this->request); + $handler = new $handler(); + + if (!($handler instanceof IExceptionHandler)) { + throw new RouterException('Exception handler must implement the IExceptionHandler interface.'); + } + $handler->handleError($this->request, $this->request->loadedRoute, $e); }