From 75566dc2bac77b2e2e4aafaae013ae818dfd63c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Wed, 16 Nov 2016 15:15:51 +0100 Subject: [PATCH] Bugfixes --- README.md | 2 + src/Pecee/Http/Middleware/IMiddleware.php | 4 +- src/Pecee/SimpleRouter/RouterBase.php | 57 ++++++++++++----------- test/GroupTest.php | 4 ++ 4 files changed, 37 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 53af7df..42ea1d0 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,8 @@ class CustomExceptionHandler implements IExceptionHandler { // Throw your custom 404-page view // - or - // load another route with our 404 page + // - or - + // you can return the $request object to ignore the error and continue on rendering the route. return $request->setUri(url('page.notfound')); } diff --git a/src/Pecee/Http/Middleware/IMiddleware.php b/src/Pecee/Http/Middleware/IMiddleware.php index 1f2fc2c..a210f71 100644 --- a/src/Pecee/Http/Middleware/IMiddleware.php +++ b/src/Pecee/Http/Middleware/IMiddleware.php @@ -8,9 +8,9 @@ interface IMiddleware { /** * @param Request $request - * @param RouterEntry|null $route + * @param RouterEntry $route * @return Request|null */ - public function handle(Request $request, RouterEntry &$route = null); + public function handle(Request $request, RouterEntry &$route); } \ No newline at end of file diff --git a/src/Pecee/SimpleRouter/RouterBase.php b/src/Pecee/SimpleRouter/RouterBase.php index 622cfb1..6c025d4 100644 --- a/src/Pecee/SimpleRouter/RouterBase.php +++ b/src/Pecee/SimpleRouter/RouterBase.php @@ -129,6 +129,9 @@ class RouterBase { if($backStack && $group !== null) { $route->setGroup($group); + } else { + $prefixes = []; + $group = null; } if($route->getNamespace() === null && $this->defaultNamespace !== null) { @@ -140,11 +143,6 @@ class RouterBase { $route->setNamespace($namespace); } - if($group !== null && $group->getPrefix() !== null && trim($group->getPrefix(), '/') !== '') { - $prefixes[] = trim($group->getPrefix(), '/'); - } - - $group = null; $this->currentRoute = $route; if($route instanceof ILoadableRoute) { @@ -154,22 +152,30 @@ class RouterBase { $this->controllerUrlMap[] = $route; } else { - if(is_callable($route->getCallback())) { - $route->renderRoute($this->request); + if($route instanceof RouterGroup) { - if ($route->matchRoute($this->request)) { + if ($route->getPrefix() !== null && trim($route->getPrefix(), '/') !== '') { + $prefixes[] = trim($route->getPrefix(), '/'); + } - /* @var $group RouterGroup */ - $group = $route; + if (is_callable($route->getCallback())) { - $mergedSettings = array_merge($settings, $group->getMergeableSettings()); + $route->renderRoute($this->request); + + if ($route->matchRoute($this->request)) { + + /* @var $group RouterGroup */ + $group = $route; + + $mergedSettings = array_merge($settings, $group->getMergeableSettings()); + + // Add ExceptionHandler + if ($group->getExceptionHandler() !== null) { + $this->exceptionHandlers[] = $route; + } - // Add ExceptionHandler - if ($group->getExceptionHandler() !== null) { - $this->exceptionHandlers[] = $route; } - } } } @@ -191,16 +197,13 @@ class RouterBase { $this->loadedRoute = null; $routeNotAllowed = false; - // Create a fictive request - so it can be changed in the middleware or exceptionhandler later on... - $request = clone $this->request; - try { // Initialize boot-managers if(count($this->bootManagers)) { /* @var $manager RouterBootManager */ foreach($this->bootManagers as $manager) { - $request = $manager->boot($request); + $this->request = $manager->boot($this->request); if(!($this->request instanceof Request)) { throw new RouterException('Custom router bootmanager "'. get_class($manager) .'" must return instance of Request.'); @@ -220,16 +223,14 @@ class RouterBase { } } - $request = ($newRequest !== null) ? $newRequest : $request; - /* @var $route RouterEntry */ for ($i = 0; $i < count($this->controllerUrlMap); $i++) { $route = $this->controllerUrlMap[$i]; - if ($route->matchRoute($request)) { + if ($route->matchRoute($this->request)) { - if (count($route->getRequestMethods()) && !in_array($request->getMethod(), $route->getRequestMethods())) { + if (count($route->getRequestMethods()) && !in_array($this->request->getMethod(), $route->getRequestMethods())) { $routeNotAllowed = true; continue; } @@ -238,16 +239,16 @@ class RouterBase { $this->loadedRoute = $route; - $request = $this->loadedRoute->loadMiddleware($request, $this->loadedRoute); - $request = ($request === null) ? $this->request : $request; + $request = clone $this->request; + $this->loadedRoute->loadMiddleware($request, $this->loadedRoute); - if($request !== null && $request->getUri() !== $this->request->getUri() && !in_array($request->getUri(), $this->routeChanges)) { + if($request->getUri() !== $this->request->getUri() && !in_array($request->getUri(), $this->routeChanges)) { $this->routeChanges[] = $request->getUri(); $this->routeRequest($request); return; } - $this->loadedRoute->renderRoute($request); + $this->loadedRoute->renderRoute($this->request); break; } @@ -262,7 +263,7 @@ class RouterBase { } if($this->loadedRoute === null) { - $this->handleException(new RouterException(sprintf('Route not found: %s', $request->getUri()), 404)); + $this->handleException(new RouterException(sprintf('Route not found: %s', $this->request->getUri()), 404)); } } diff --git a/test/GroupTest.php b/test/GroupTest.php index 4ed2415..9b3001e 100644 --- a/test/GroupTest.php +++ b/test/GroupTest.php @@ -64,6 +64,10 @@ class GroupTest extends PHPUnit_Framework_TestCase { public function testUrls() { + \Pecee\SimpleRouter\RouterBase::getInstance()->reset(); + \Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/fancy/url/1'); + \Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get'); + \Pecee\SimpleRouter\SimpleRouter::get('/my/fancy/url/1', 'DummyController@start', ['as' => 'fancy1']); \Pecee\SimpleRouter\SimpleRouter::get('/my/fancy/url/2', 'DummyController@start')->setAlias('fancy2');