From cbaa0bcaac18c9a2b0fe0206a5b5f8a93254cb79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Sat, 19 Dec 2015 09:21:12 +0100 Subject: [PATCH] [FEATURE] getRoute now support domains. - Optimisations + bugfixes. --- src/Pecee/SimpleRouter/RouterBase.php | 56 +++++++++++++++-------- src/Pecee/SimpleRouter/RouterGroup.php | 8 ++-- src/Pecee/SimpleRouter/RouterResource.php | 2 +- 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/Pecee/SimpleRouter/RouterBase.php b/src/Pecee/SimpleRouter/RouterBase.php index 9d04427..fbdd3e8 100644 --- a/src/Pecee/SimpleRouter/RouterBase.php +++ b/src/Pecee/SimpleRouter/RouterBase.php @@ -45,7 +45,6 @@ class RouterBase { protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), $backStack = false, $group = null) { // Loop through each route-request - $activeGroup = null; $routesCount = count($routes); $mergedSettings = array(); @@ -54,6 +53,7 @@ class RouterBase { $route = $routes[$i]; + $route->addSettings($settings); $route->setGroup($group); if($this->defaultNamespace && !$route->getNamespace()) { @@ -73,21 +73,20 @@ class RouterBase { array_push($newPrefixes, rtrim($route->getPrefix(), '/')); } - $route->addSettings($settings); - if(!($route instanceof RouterGroup)) { if(is_array($newPrefixes) && count($newPrefixes) && $backStack) { $route->setUrl( join('/', $newPrefixes) . $route->getUrl() ); } + $group = null; $this->controllerUrlMap[] = $route; } $this->currentRoute = $route; if($route instanceof RouterGroup && is_callable($route->getCallback())) { + $group = $route; $route->renderRoute($this->request); - $activeGroup = $route; $mergedSettings = array_merge($route->getMergeableSettings(), $settings); } @@ -98,7 +97,7 @@ class RouterBase { $this->backStack = array(); // Route any routes added to the backstack - $this->processRoutes($backStack, $mergedSettings, $newPrefixes, true, $activeGroup); + $this->processRoutes($backStack, $mergedSettings, $newPrefixes, true, $group); } } } @@ -239,7 +238,20 @@ class RouterBase { protected function processUrl($route, $method = null, $parameters = null, $getParams = null) { - $url = '/' . trim($route->getUrl(), '/'); + $domain = ''; + + if($route->getGroup() !== null && $route->getGroup()->getDomain() !== null) { + if(is_array($route->getGroup()->getDomain())) { + $domains = $route->getGroup()->getDomain(); + $domain = array_shift($domains); + } else { + $domain = $route->getGroup()->getDomain(); + } + + $domain = '//' . $domain; + } + + $url = $domain . '/' . trim($route->getUrl(), '/'); if(($route instanceof RouterController || $route instanceof RouterResource) && $method !== null) { $url .= $method; @@ -251,20 +263,26 @@ class RouterBase { } } else { /* @var $route RouterEntry */ - $params = $route->getParameters(); - if(count($params)) { - $i = 0; - foreach($params as $param => $value) { - $value = (isset($parameters[$param])) ? $parameters[$param] : $value; - $url = str_ireplace(array('{' . $param. '}', '{' . $param. '?}'), $value, $url); - $i++; - } + if(is_array($parameters)) { + $params = array_merge($route->getParameters(), $parameters); } else { - // If no parameters are specified in the route, assume that the provided parameters should be used. - if(count($parameters)) { - $url = rtrim($url, '/') . '/' . join('/', $parameters); - } + $params = $route->getParameters(); } + + $otherParams = []; + + $i = 0; + foreach($params as $param => $value) { + $value = (isset($parameters[$param])) ? $parameters[$param] : $value; + if(stripos($url, '{' . $param. '}') !== false || stripos($url, '{' . $param . '?}') !== false) { + $url = str_ireplace(array('{' . $param . '}', '{' . $param . '?}'), $value, $url); + } else { + $otherParams[$param] = $value; + } + $i++; + } + + $url = rtrim($url, '/') . '/' . join('/', $otherParams); } $url = rtrim($url, '/') . '/'; @@ -287,7 +305,7 @@ class RouterBase { } // Return current route if no options has been specified - if($controller === null && $parameters === null && $this->loadedRoute !== null) { + if($controller === null && $parameters === null) { $getParams = (is_array($getParams)) ? array_merge($_GET, $getParams) : $_GET; $url = parse_url(Request::getInstance()->getUri()); diff --git a/src/Pecee/SimpleRouter/RouterGroup.php b/src/Pecee/SimpleRouter/RouterGroup.php index 71de05d..e9fa128 100644 --- a/src/Pecee/SimpleRouter/RouterGroup.php +++ b/src/Pecee/SimpleRouter/RouterGroup.php @@ -60,9 +60,7 @@ class RouterGroup extends RouterEntry { throw new RouterException('Method not allowed'); } - if($this->matchDomain($request) === null) { - return null; - } + $this->matchDomain($request); return parent::renderRoute($request); } @@ -80,4 +78,8 @@ class RouterGroup extends RouterEntry { return $this->exceptionHandler; } + public function getDomain() { + return $this->domain; + } + } \ No newline at end of file diff --git a/src/Pecee/SimpleRouter/RouterResource.php b/src/Pecee/SimpleRouter/RouterResource.php index 63a5630..0dff961 100644 --- a/src/Pecee/SimpleRouter/RouterResource.php +++ b/src/Pecee/SimpleRouter/RouterResource.php @@ -51,7 +51,7 @@ class RouterResource extends RouterEntry { $route = rtrim($this->url, '/') . '/{id?}/{action?}'; - $parameters = $this->parseParameters($route, $url); + $parameters = $this->parseParameters($route, $url, '[0-9]*?'); if($parameters !== null) {