From 808d59d3d37c5effbd0a4968e3bd0025b5cc62f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Sat, 19 Nov 2016 08:46:48 +0100 Subject: [PATCH] Bugfixes --- src/Pecee/SimpleRouter/RouterBase.php | 55 +++++++++++++------------ src/Pecee/SimpleRouter/RouterEntry.php | 22 ++++++++-- src/Pecee/SimpleRouter/SimpleRouter.php | 9 ++-- test/RouterRouteTest.php | 2 +- 4 files changed, 50 insertions(+), 38 deletions(-) diff --git a/src/Pecee/SimpleRouter/RouterBase.php b/src/Pecee/SimpleRouter/RouterBase.php index ad40fcc..fdd4e4d 100644 --- a/src/Pecee/SimpleRouter/RouterBase.php +++ b/src/Pecee/SimpleRouter/RouterBase.php @@ -134,35 +134,21 @@ class RouterBase return $route; } - protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), RouterEntry $parent = null) { - - $mergedSettings = []; - $mergedPrefixes = []; + protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), RouterEntry $parent = null) + { // Loop through each route-request /* @var $route RouterEntry */ foreach ($routes as $route) { - $route->merge($settings); + $newPrefixes = $prefixes; + $newSettings = $settings; - if ($parent !== null) { + if($parent !== null) { $route->setParent($parent); - $mergedPrefixes = []; } - if ($route instanceof ILoadableRoute) { - - if($parent !== null && count($prefixes)) { - $route->setUrl(trim(join('/', $prefixes) . $route->getUrl(), '/')); - } - - $this->controllerUrlMap[] = $route; - - } elseif ($route instanceof RouterGroup) { - - if ($route->getPrefix() !== null && trim($route->getPrefix(), '/') !== '') { - $mergedPrefixes[] = trim($route->getPrefix(), '/'); - } + if ($route instanceof RouterGroup) { if ($route->getCallback() !== null && is_callable($route->getCallback())) { @@ -171,24 +157,39 @@ class RouterBase $this->processingRoute = false; if ($route->matchRoute($this->request)) { - - $mergedSettings = array_merge($mergedSettings, $route->toArray()); - // Add ExceptionHandler if (count($route->getExceptionHandlers()) > 0) { - $this->exceptionHandlers = array_merge($route->getExceptionHandlers(), - $this->exceptionHandlers); + $this->exceptionHandlers = array_merge($route->getExceptionHandlers(), $this->exceptionHandlers); } } } } + if ($route instanceof RouterGroup) { + $newPrefixes[] = trim($route->getPrefix(), '/'); + $newSettings = array_merge($settings, $route->toArray()); + } else { + if (count($settings)) { + $route->merge($settings); + } + } + + if ($route instanceof ILoadableRoute) { + + if (count($prefixes)) { + $route->setUrl(trim(join('/', $prefixes) . $route->getUrl(), '/')); + } + + $this->controllerUrlMap[] = $route; + + } + if (count($this->backStack) > 0) { $backStack = $this->backStack; - $this->backStack = array(); + $this->backStack = []; // Route any routes added to the backstack - $this->processRoutes($backStack, $mergedSettings, $mergedPrefixes, $route); + $this->processRoutes($backStack, $newSettings, $newPrefixes, $route); } } } diff --git a/src/Pecee/SimpleRouter/RouterEntry.php b/src/Pecee/SimpleRouter/RouterEntry.php index 0454e63..93ad206 100644 --- a/src/Pecee/SimpleRouter/RouterEntry.php +++ b/src/Pecee/SimpleRouter/RouterEntry.php @@ -27,6 +27,7 @@ abstract class RouterEntry protected $callback; protected $namespace; + protected $defaultNamespace; protected $regex; protected $requestMethods = array(); protected $where = array(); @@ -293,7 +294,7 @@ abstract class RouterEntry /** * @param string $namespace - * @return static + * @return static $this */ public function setNamespace($namespace) { @@ -301,6 +302,19 @@ abstract class RouterEntry return $this; } + /** + * @param string $namespace + * @return static $this + */ + public function setDefaultNamespace($namespace) { + $this->defaultNamespace = $namespace; + return $this; + } + + public function getDefaultNamespace() { + return $this->defaultNamespace; + } + /** * @return string|array */ @@ -314,7 +328,7 @@ abstract class RouterEntry */ public function getNamespace() { - return $this->namespace; + return ($this->namespace === null) ? $this->defaultNamespace : $this->namespace; } /** @@ -376,9 +390,9 @@ abstract class RouterEntry $values['middleware'] = $this->middlewares; } - /*if (count($this->where) > 0) { + if (count($this->where) > 0) { $values['where'] = $this->where; - }*/ + } if (count($this->requestMethods) > 0) { $values['method'] = $this->requestMethods; diff --git a/src/Pecee/SimpleRouter/SimpleRouter.php b/src/Pecee/SimpleRouter/SimpleRouter.php index 37a6f77..0de00ff 100644 --- a/src/Pecee/SimpleRouter/SimpleRouter.php +++ b/src/Pecee/SimpleRouter/SimpleRouter.php @@ -141,14 +141,11 @@ class SimpleRouter * @throws RouterException * @return RouterGroup */ - public static function group($settings = array(), \Closure $callback) + public static function group(array $settings = array(), \Closure $callback) { $group = new RouterGroup(); $group->setCallback($callback); - - if ($settings !== null && is_array($settings) === true) { - $group->merge($settings); - } + $group->merge($settings); if (is_callable($callback) === false) { throw new RouterException('Invalid callback provided. Only functions or methods supported'); @@ -337,7 +334,7 @@ class SimpleRouter $namespace .= '\\' . $route->getNamespace(); } - $route->setNamespace($namespace); + $route->setDefaultNamespace($namespace); } return $route; diff --git a/test/RouterRouteTest.php b/test/RouterRouteTest.php index 4788a47..5dfdc40 100644 --- a/test/RouterRouteTest.php +++ b/test/RouterRouteTest.php @@ -126,7 +126,7 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase $this->result = false; SimpleRouter::group(['domain' => '{subdomain}.world.com'], function () { - SimpleRouter::get('test', function ($subdomain = null) { + SimpleRouter::get('/test', function ($subdomain = null) { $this->result = ($subdomain === 'hello'); }); });