Merge pull request #142 from skipperbent/v2

V2
This commit is contained in:
Simon Sessingø
2016-11-17 06:37:11 +02:00
committed by GitHub
4 changed files with 19 additions and 29 deletions
+7 -8
View File
@@ -85,7 +85,6 @@ class RouterBase {
protected $routeChanges = array(); protected $routeChanges = array();
public function __construct() { public function __construct() {
$this->reset(); $this->reset();
} }
@@ -118,16 +117,12 @@ class RouterBase {
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) {
// Loop through each route-request // Loop through each route-request
$mergedSettings = array();
/* @var $route RouterEntry */ /* @var $route RouterEntry */
for($i = 0; $i < count($routes); $i++) { for($i = 0; $i < count($routes); $i++) {
$route = $routes[$i]; $route = $routes[$i];
if(count($settings)) { $route->setData($settings);
$route->setData($settings);
}
if($parent !== null) { if($parent !== null) {
@@ -150,22 +145,26 @@ class RouterBase {
} }
if($route instanceof ILoadableRoute) { if($route instanceof ILoadableRoute) {
$route->setUrl( trim(join('/', $prefixes) . $route->getUrl(), '/') ); $route->setUrl( trim(join('/', $prefixes) . $route->getUrl(), '/') );
$this->controllerUrlMap[] = $route; $this->controllerUrlMap[] = $route;
} elseif($route instanceof RouterGroup) { } elseif($route instanceof RouterGroup) {
if ($route->getCallback() !== null && is_callable($route->getCallback())) { if ($route->getCallback() !== null && is_callable($route->getCallback())) {
$this->processingRoute = true; $this->processingRoute = true;
$route->renderRoute($this->request); $route->renderRoute($this->request);
$this->processingRoute = false; $this->processingRoute = false;
if ($route->matchRoute($this->request)) { if ($route->matchRoute($this->request)) {
$mergedSettings = array_merge($settings, $route->getMergeableData());
$settings = array_merge($settings, $route->getMergeableData());
// Add ExceptionHandler // Add ExceptionHandler
if (count($route->getExceptionHandlers())) { if (count($route->getExceptionHandlers())) {
$this->exceptionHandlers = array_merge($this->exceptionHandlers, $route->getExceptionHandlers()); $this->exceptionHandlers = array_merge($this->exceptionHandlers, $route->getExceptionHandlers());
} }
} }
} }
} }
@@ -174,7 +173,7 @@ class RouterBase {
$this->backStack = array(); $this->backStack = array();
// Route any routes added to the backstack // Route any routes added to the backstack
$this->processRoutes($backStack, $mergedSettings, $prefixes, $route); $this->processRoutes($backStack, $settings, $prefixes, $route);
} }
$prefixes = []; $prefixes = [];
+8 -16
View File
@@ -329,9 +329,11 @@ abstract class RouterEntry {
*/ */
public function getMergeableData() { public function getMergeableData() {
$output = [ $output = array();
'namespace' => $this->namespace,
]; if($this->namespace !== null) {
$output['namespace'] = $this->namespace;
}
if(count($this->middlewares)) { if(count($this->middlewares)) {
$output['middleware'] = $this->middlewares; $output['middleware'] = $this->middlewares;
@@ -360,27 +362,17 @@ abstract class RouterEntry {
*/ */
public function setData(array $settings) { public function setData(array $settings) {
if (isset($settings['namespace'])) { if (isset($settings['namespace']) && $this->namespace === null) {
$this->setNamespace($settings['namespace']); $this->setNamespace($settings['namespace']);
} }
// Push middleware if multiple // Push middleware if multiple
if (isset($settings['middleware'])) { if (isset($settings['middleware'])) {
$this->middlewares = array_merge((array)$settings['middleware'], $this->middlewares);
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));
} }
if(isset($settings['method'])) { if(isset($settings['method'])) {
$requestMethods = is_array($settings['method']) ? $settings['method'] : array($settings['method']); $this->setRequestMethods((array)$settings['method']);
$this->setRequestMethods($requestMethods);
} }
if(isset($settings['where'])) { if(isset($settings['where'])) {
+3 -4
View File
@@ -79,16 +79,15 @@ class RouterGroup extends RouterEntry {
} }
if(isset($settings['exceptionHandler'])) { if(isset($settings['exceptionHandler'])) {
$handlers = is_array($settings['exceptionHandler']) ? $settings['exceptionHandler'] : array($settings['exceptionHandler']); $this->setExceptionHandlers((array)$settings['exceptionHandler']);
$this->setExceptionHandlers($handlers);
} }
if(isset($settings['domain'])) { if(isset($settings['domain'])) {
$domains = is_array($settings['domain']) ? $settings['domain'] : array($settings['domain']); $this->setDomains((array)$settings['domain']);
$this->setDomains($domains);
} }
return parent::setData($settings); return parent::setData($settings);
} }
} }
+1 -1
View File
@@ -124,7 +124,7 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
$this->result = false; $this->result = false;
\Pecee\SimpleRouter\SimpleRouter::group(['domain' => '{subdomain}.world.com'], function() { \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'); $this->result = ($subdomain === 'hello');
}); });
}); });