This commit is contained in:
Simon Sessingø
2016-11-19 08:46:48 +01:00
parent d1f33d9b01
commit 808d59d3d3
4 changed files with 50 additions and 38 deletions
+28 -27
View File
@@ -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);
}
}
}
+18 -4
View File
@@ -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;
+3 -6
View File
@@ -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;