This commit is contained in:
Simon Sessingø
2016-11-17 05:36:44 +01:00
parent b298665d33
commit c59ab12e1a
4 changed files with 19 additions and 29 deletions

View File

@@ -85,7 +85,6 @@ class RouterBase {
protected $routeChanges = array();
public function __construct() {
$this->reset();
}
@@ -118,16 +117,12 @@ class RouterBase {
protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), RouterEntry $parent = null) {
// Loop through each route-request
$mergedSettings = array();
/* @var $route RouterEntry */
for($i = 0; $i < count($routes); $i++) {
$route = $routes[$i];
if(count($settings)) {
$route->setData($settings);
}
$route->setData($settings);
if($parent !== null) {
@@ -150,22 +145,26 @@ class RouterBase {
}
if($route instanceof ILoadableRoute) {
$route->setUrl( trim(join('/', $prefixes) . $route->getUrl(), '/') );
$this->controllerUrlMap[] = $route;
} elseif($route instanceof RouterGroup) {
if ($route->getCallback() !== null && is_callable($route->getCallback())) {
$this->processingRoute = true;
$route->renderRoute($this->request);
$this->processingRoute = false;
if ($route->matchRoute($this->request)) {
$mergedSettings = array_merge($settings, $route->getMergeableData());
$settings = array_merge($settings, $route->getMergeableData());
// Add ExceptionHandler
if (count($route->getExceptionHandlers())) {
$this->exceptionHandlers = array_merge($this->exceptionHandlers, $route->getExceptionHandlers());
}
}
}
}
@@ -174,7 +173,7 @@ class RouterBase {
$this->backStack = array();
// Route any routes added to the backstack
$this->processRoutes($backStack, $mergedSettings, $prefixes, $route);
$this->processRoutes($backStack, $settings, $prefixes, $route);
}
$prefixes = [];

View File

@@ -329,9 +329,11 @@ abstract class RouterEntry {
*/
public function getMergeableData() {
$output = [
'namespace' => $this->namespace,
];
$output = array();
if($this->namespace !== null) {
$output['namespace'] = $this->namespace;
}
if(count($this->middlewares)) {
$output['middleware'] = $this->middlewares;
@@ -360,27 +362,17 @@ abstract class RouterEntry {
*/
public function setData(array $settings) {
if (isset($settings['namespace'])) {
if (isset($settings['namespace']) && $this->namespace === null) {
$this->setNamespace($settings['namespace']);
}
// Push middleware if multiple
if (isset($settings['middleware'])) {
if (!is_array($settings['middleware'])) {
$settings['middleware'] = array_merge($this->middlewares, array($settings['middleware']));
} else {
$settings['middleware'][] = $this->middlewares;
}
$middlewares = is_array($settings['middleware']) ? $settings['middleware'] : array($settings['middleware']);
$this->middlewares = array_reverse(array_merge($this->middlewares, $middlewares));
$this->middlewares = array_merge((array)$settings['middleware'], $this->middlewares);
}
if(isset($settings['method'])) {
$requestMethods = is_array($settings['method']) ? $settings['method'] : array($settings['method']);
$this->setRequestMethods($requestMethods);
$this->setRequestMethods((array)$settings['method']);
}
if(isset($settings['where'])) {

View File

@@ -79,16 +79,15 @@ class RouterGroup extends RouterEntry {
}
if(isset($settings['exceptionHandler'])) {
$handlers = is_array($settings['exceptionHandler']) ? $settings['exceptionHandler'] : array($settings['exceptionHandler']);
$this->setExceptionHandlers($handlers);
$this->setExceptionHandlers((array)$settings['exceptionHandler']);
}
if(isset($settings['domain'])) {
$domains = is_array($settings['domain']) ? $settings['domain'] : array($settings['domain']);
$this->setDomains($domains);
$this->setDomains((array)$settings['domain']);
}
return parent::setData($settings);
}
}

View File

@@ -124,7 +124,7 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
$this->result = false;
\Pecee\SimpleRouter\SimpleRouter::group(['domain' => '{subdomain}.world.com'], function() {
\Pecee\SimpleRouter\SimpleRouter::get('test', function($subdomain) {
\Pecee\SimpleRouter\SimpleRouter::get('test', function($subdomain = null) {
$this->result = ($subdomain === 'hello');
});
});