mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 23:12:11 +00:00
@@ -134,35 +134,21 @@ class RouterBase
|
|||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), RouterEntry $parent = null) {
|
protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), RouterEntry $parent = null)
|
||||||
|
{
|
||||||
$mergedSettings = [];
|
|
||||||
$mergedPrefixes = [];
|
|
||||||
|
|
||||||
// Loop through each route-request
|
// Loop through each route-request
|
||||||
/* @var $route RouterEntry */
|
/* @var $route RouterEntry */
|
||||||
foreach ($routes as $route) {
|
foreach ($routes as $route) {
|
||||||
|
|
||||||
$route->merge($settings);
|
$newPrefixes = $prefixes;
|
||||||
|
$newSettings = $settings;
|
||||||
|
|
||||||
if($parent !== null) {
|
if($parent !== null) {
|
||||||
$route->setParent($parent);
|
$route->setParent($parent);
|
||||||
$mergedPrefixes = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($route instanceof ILoadableRoute) {
|
if ($route instanceof RouterGroup) {
|
||||||
|
|
||||||
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->getCallback() !== null && is_callable($route->getCallback())) {
|
if ($route->getCallback() !== null && is_callable($route->getCallback())) {
|
||||||
|
|
||||||
@@ -171,24 +157,39 @@ class RouterBase
|
|||||||
$this->processingRoute = false;
|
$this->processingRoute = false;
|
||||||
|
|
||||||
if ($route->matchRoute($this->request)) {
|
if ($route->matchRoute($this->request)) {
|
||||||
|
|
||||||
$mergedSettings = array_merge($mergedSettings, $route->toArray());
|
|
||||||
|
|
||||||
// Add ExceptionHandler
|
// Add ExceptionHandler
|
||||||
if (count($route->getExceptionHandlers()) > 0) {
|
if (count($route->getExceptionHandlers()) > 0) {
|
||||||
$this->exceptionHandlers = array_merge($route->getExceptionHandlers(),
|
$this->exceptionHandlers = array_merge($route->getExceptionHandlers(), $this->exceptionHandlers);
|
||||||
$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) {
|
if (count($this->backStack) > 0) {
|
||||||
$backStack = $this->backStack;
|
$backStack = $this->backStack;
|
||||||
$this->backStack = array();
|
$this->backStack = [];
|
||||||
|
|
||||||
// Route any routes added to the backstack
|
// Route any routes added to the backstack
|
||||||
$this->processRoutes($backStack, $mergedSettings, $mergedPrefixes, $route);
|
$this->processRoutes($backStack, $newSettings, $newPrefixes, $route);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ abstract class RouterEntry
|
|||||||
protected $callback;
|
protected $callback;
|
||||||
|
|
||||||
protected $namespace;
|
protected $namespace;
|
||||||
|
protected $defaultNamespace;
|
||||||
protected $regex;
|
protected $regex;
|
||||||
protected $requestMethods = array();
|
protected $requestMethods = array();
|
||||||
protected $where = array();
|
protected $where = array();
|
||||||
@@ -293,7 +294,7 @@ abstract class RouterEntry
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $namespace
|
* @param string $namespace
|
||||||
* @return static
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setNamespace($namespace)
|
public function setNamespace($namespace)
|
||||||
{
|
{
|
||||||
@@ -301,6 +302,19 @@ abstract class RouterEntry
|
|||||||
return $this;
|
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
|
* @return string|array
|
||||||
*/
|
*/
|
||||||
@@ -314,7 +328,7 @@ abstract class RouterEntry
|
|||||||
*/
|
*/
|
||||||
public function getNamespace()
|
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;
|
$values['middleware'] = $this->middlewares;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*if (count($this->where) > 0) {
|
if (count($this->where) > 0) {
|
||||||
$values['where'] = $this->where;
|
$values['where'] = $this->where;
|
||||||
}*/
|
}
|
||||||
|
|
||||||
if (count($this->requestMethods) > 0) {
|
if (count($this->requestMethods) > 0) {
|
||||||
$values['method'] = $this->requestMethods;
|
$values['method'] = $this->requestMethods;
|
||||||
|
|||||||
@@ -141,14 +141,11 @@ class SimpleRouter
|
|||||||
* @throws RouterException
|
* @throws RouterException
|
||||||
* @return RouterGroup
|
* @return RouterGroup
|
||||||
*/
|
*/
|
||||||
public static function group($settings = array(), \Closure $callback)
|
public static function group(array $settings = array(), \Closure $callback)
|
||||||
{
|
{
|
||||||
$group = new RouterGroup();
|
$group = new RouterGroup();
|
||||||
$group->setCallback($callback);
|
$group->setCallback($callback);
|
||||||
|
|
||||||
if ($settings !== null && is_array($settings) === true) {
|
|
||||||
$group->merge($settings);
|
$group->merge($settings);
|
||||||
}
|
|
||||||
|
|
||||||
if (is_callable($callback) === false) {
|
if (is_callable($callback) === false) {
|
||||||
throw new RouterException('Invalid callback provided. Only functions or methods supported');
|
throw new RouterException('Invalid callback provided. Only functions or methods supported');
|
||||||
@@ -337,7 +334,7 @@ class SimpleRouter
|
|||||||
$namespace .= '\\' . $route->getNamespace();
|
$namespace .= '\\' . $route->getNamespace();
|
||||||
}
|
}
|
||||||
|
|
||||||
$route->setNamespace($namespace);
|
$route->setDefaultNamespace($namespace);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->result = false;
|
$this->result = false;
|
||||||
|
|
||||||
SimpleRouter::group(['domain' => '{subdomain}.world.com'], function () {
|
SimpleRouter::group(['domain' => '{subdomain}.world.com'], function () {
|
||||||
SimpleRouter::get('test', function ($subdomain = null) {
|
SimpleRouter::get('/test', function ($subdomain = null) {
|
||||||
$this->result = ($subdomain === 'hello');
|
$this->result = ($subdomain === 'hello');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user