Merge pull request #106 from skipperbent/development

Development
This commit is contained in:
Simon Sessingø
2016-06-04 18:21:16 +02:00
5 changed files with 56 additions and 28 deletions

View File

@@ -72,12 +72,14 @@ class RouterBase {
array_push($newPrefixes, trim($route->getPrefix(), '/'));
}
/* @var $group RouterGroup */
$group = null;
if(!($route instanceof RouterGroup)) {
if(is_array($newPrefixes) && count($newPrefixes) && $backStack) {
$route->setUrl( '/' . join('/', $newPrefixes) . $route->getUrl() );
}
$group = null;
$this->controllerUrlMap[] = $route;
}
@@ -86,11 +88,11 @@ class RouterBase {
if($route instanceof RouterGroup && is_callable($route->getCallback())) {
$group = $route;
$route->renderRoute($this->request);
$mergedSettings = array_merge($settings, $route->getMergeableSettings());
$group->renderRoute($this->request);
$mergedSettings = array_merge($settings, $group->getMergeableSettings());
// Add ExceptionHandler
if($route->matchRoute($this->request) && $route->getExceptionHandler() !== null) {
if($group->matchRoute($this->request) && $group->getExceptionHandler() !== null) {
$this->exceptionHandlers[] = $route;
}
}
@@ -131,14 +133,6 @@ class RouterBase {
// Loop through each route-request
$this->processRoutes($this->routes);
// Make sure routes with longer urls are rendered first
usort($this->controllerUrlMap, function($a, $b) {
if(strlen($a->getUrl()) < strlen($b->getUrl())) {
return 1;
}
return -1;
});
$routeNotAllowed = false;
$max = count($this->controllerUrlMap);
@@ -186,7 +180,7 @@ class RouterBase {
protected function handleException(\Exception $e) {
/* @var $route RouterEntry */
/* @var $route RouterGroup */
foreach ($this->exceptionHandlers as $route) {
$route->loadMiddleware($this->request);
$handler = $route->getExceptionHandler();
@@ -300,9 +294,7 @@ class RouterBase {
if(is_array($getParams)) {
if ($includeEmpty === false) {
$getParams = array_filter($getParams, function ($item) {
if (!empty($item)) {
return $item;
}
return (!empty($item));
});
}

View File

@@ -232,7 +232,7 @@ abstract class RouterEntry {
}
/**
* Dynamicially set settings value
* Dynamically set settings value
*
* @param string $name
* @param mixed|null $value
@@ -261,12 +261,6 @@ abstract class RouterEntry {
$character = $route[$i];
// Skip "/" if we are at the end of a parameter
if($lastCharacter === '}' && $character === '/') {
$lastCharacter = $character;
continue;
}
if($character === '{') {
// Remove "/" and "\" from regex
if(substr($regex, strlen($regex)-1) === '/') {
@@ -285,10 +279,10 @@ abstract class RouterEntry {
if($lastCharacter === '?') {
$parameter = substr($parameter, 0, strlen($parameter)-1);
$regex .= '(?:\\/?(?P<'.$parameter.'>[^\/]+)?\\/?)';
$regex .= '(?:\/?(?P<' . $parameter . '>'. $parameterRegex .')[^\/]?)?';
$required = false;
} else {
$regex .= '.*?(?P<' . $parameter . '>'. $parameterRegex .').*?';
$regex .= '\/?(?P<' . $parameter . '>'. $parameterRegex .')[^\/]?';
}
$parameterNames[] = array('name' => $parameter, 'required' => $required);
$parameter = '';
@@ -307,7 +301,8 @@ abstract class RouterEntry {
$parameterValues = array();
if(preg_match('/^'.$regex.'.?$/is', $url, $parameterValues)) {
if(preg_match('/^'.$regex.'$/is', $url, $parameterValues)) {
$parameters = array();
$max = count($parameterNames);
@@ -399,7 +394,7 @@ abstract class RouterEntry {
}
/**
* Get allowed requeset methods
* Get allowed request methods
*
* @return array
*/

View File

@@ -105,7 +105,7 @@ class RouterRoute extends RouterEntry {
} else {
return strtolower($this->getAlias()) === strtolower($name);
}
return false;
}

View File

@@ -6,4 +6,9 @@ class DummyController {
echo static::class . '@' .'start() OK';
}
public function param($params = null) {
$params = func_get_args();
echo 'Params: ' . join(', ', $params);
}
}

View File

@@ -66,4 +66,40 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
}
public function testSimpleParam() {
\Pecee\SimpleRouter\RouterBase::reset();
\Pecee\Http\Request::getInstance()->setMethod('get');
\Pecee\Http\Request::getInstance()->setUri('/test-param1');
\Pecee\SimpleRouter\SimpleRouter::get('/test-{param1}', 'DummyController@param');
\Pecee\SimpleRouter\SimpleRouter::start();
}
public function testMultiParam() {
\Pecee\SimpleRouter\RouterBase::reset();
\Pecee\Http\Request::getInstance()->setMethod('get');
\Pecee\Http\Request::getInstance()->setUri('/test-param1-param2');
\Pecee\SimpleRouter\SimpleRouter::get('/test-{param1}-{param2}', 'DummyController@param');
\Pecee\SimpleRouter\SimpleRouter::start();
}
public function testPathParam() {
\Pecee\SimpleRouter\RouterBase::reset();
\Pecee\Http\Request::getInstance()->setMethod('get');
\Pecee\Http\Request::getInstance()->setUri('/test/path/param1');
\Pecee\SimpleRouter\SimpleRouter::get('/test/path/{param}', 'DummyController@param');
\Pecee\SimpleRouter\SimpleRouter::start();
}
}