From 22563671c51785230b586b594839c64dd9e54313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Sat, 9 Apr 2016 15:26:18 +0200 Subject: [PATCH] [TASK] Moved group-middleware rendering to routeRequest to ensure all route-urls has been initialised. - Optimisations. --- src/Pecee/SimpleRouter/RouterBase.php | 27 ++++++++++++++++---------- src/Pecee/SimpleRouter/RouterRoute.php | 7 +------ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/Pecee/SimpleRouter/RouterBase.php b/src/Pecee/SimpleRouter/RouterBase.php index 6a86d2c..b7b775b 100644 --- a/src/Pecee/SimpleRouter/RouterBase.php +++ b/src/Pecee/SimpleRouter/RouterBase.php @@ -19,15 +19,17 @@ class RouterBase { protected $defaultNamespace; protected $bootManagers; protected $baseCsrfVerifier; + protected $middlewaresToLoad; // TODO: clean up - cut some of the methods down to smaller pieces public function __construct() { + $this->request = Request::getInstance(); $this->routes = array(); $this->backStack = array(); $this->controllerUrlMap = array(); - $this->request = Request::getInstance(); $this->bootManagers = array(); + $this->middlewaresToLoad = array(); } public function addRoute(RouterEntry $route) { @@ -56,11 +58,9 @@ class RouterBase { } if($this->defaultNamespace && !$route->getNamespace()) { - $namespace = null; + $namespace = $this->defaultNamespace; if ($route->getNamespace()) { - $namespace = $this->defaultNamespace . '\\' . $route->getNamespace(); - } else { - $namespace = $this->defaultNamespace; + $namespace .= '\\' . $route->getNamespace(); } $route->setNamespace($namespace); @@ -86,13 +86,13 @@ class RouterBase { if($route instanceof RouterGroup && is_callable($route->getCallback())) { $group = $route; - // Load middleware on group if route matches - if($route->getPrefix() !== null && $route->matchRoute($this->request)) { - $route->loadMiddleware($this->request); - } - $route->renderRoute($this->request); $mergedSettings = array_merge($settings, $route->getMergeableSettings()); + + // Load middleware on group if route matches + if($route->getPrefix() !== null && $route->matchRoute($this->request)) { + $this->middlewaresToLoad[] = $route; + } } $this->currentRoute = null; @@ -111,6 +111,13 @@ class RouterBase { $originalUri = $this->request->getUri(); + // Load group middlewares + + /* @var $middleware RouterEntry */ + foreach($this->middlewaresToLoad as $middleware) { + $middleware->loadMiddleware($this->request); + } + // Initialize boot-managers if(count($this->bootManagers)) { /* @var $manager RouterBootManager */ diff --git a/src/Pecee/SimpleRouter/RouterRoute.php b/src/Pecee/SimpleRouter/RouterRoute.php index 86c0a8d..f202cbe 100644 --- a/src/Pecee/SimpleRouter/RouterRoute.php +++ b/src/Pecee/SimpleRouter/RouterRoute.php @@ -102,13 +102,8 @@ class RouterRoute extends RouterEntry { return true; } } - } else { - if(strtolower($this->getAlias()) === strtolower($name)) { - return true; - } } - - return false; + return strtolower($this->getAlias()) === strtolower($name); } /**