[FEATURE]

- Added support for custom ExceptionHandlers on group level.
- Routes now contain parent group, if any.
- Fixed wrong usage of required parameter.
This commit is contained in:
Simon Sessingø
2015-11-22 20:24:43 +01:00
parent a9e7a33ff8
commit 8efec07a8b
5 changed files with 63 additions and 29 deletions
+7 -2
View File
@@ -39,12 +39,16 @@ class RouterBase {
}
}
protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), $backstack = false) {
protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), $backstack = false, $group = null) {
// Loop through each route-request
$activeGroup = null;
/* @var $route RouterEntry */
foreach($routes as $route) {
$route->setGroup($group);
if($this->defaultNamespace && !$route->getNamespace()) {
$namespace = null;
if ($route->getNamespace()) {
@@ -75,6 +79,7 @@ class RouterBase {
$this->currentRoute = $route;
if($route instanceof RouterGroup && is_callable($route->getCallback())) {
$route->renderRoute($this->request);
$activeGroup = $route;
}
$this->currentRoute = null;
@@ -83,7 +88,7 @@ class RouterBase {
$this->backstack = array();
// Route any routes added to the backstack
$this->processRoutes($backstack, $mergedSettings, $newPrefixes, true);
$this->processRoutes($backstack, $mergedSettings, $newPrefixes, true, $activeGroup);
}
}
}
+9
View File
@@ -317,6 +317,15 @@ abstract class RouterEntry {
return $this->settings['requestMethods'];
}
public function getGroup() {
return $this->group;
}
public function setGroup($group) {
$this->group = $group;
return $this;
}
abstract function matchRoute(Request $request);
}
+9
View File
@@ -33,4 +33,13 @@ class RouterGroup extends RouterEntry {
return null;
}
public function setExceptionHandler($class) {
$this->exceptionHandler = $class;
return $this;
}
public function getExceptionHandler() {
return $this->exceptionHandler;
}
}
+8 -7
View File
@@ -6,7 +6,7 @@ use Pecee\Http\Request;
class RouterRoute extends RouterEntry {
const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?\?{0,1})}';
const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?)\?{0,1}}';
protected $url;
@@ -60,12 +60,12 @@ class RouterRoute extends RouterEntry {
$isParameter = true;
} elseif($isParameter && $character === '}') {
$required = false;
$required = true;
// Check for optional parameter
if($lastCharacter === '?') {
$parameter = substr($parameter, 0, strlen($parameter)-1);
$regex .= '(?:(?:\/{0,1}(?P<'.$parameter.'>[a-z0-9]*?)){0,1}\\/)';
$required = true;
$required = false;
} else {
// Use custom parameter regex if it exists
$parameterRegex = '[a-z0-9]*?';
@@ -130,12 +130,13 @@ class RouterRoute extends RouterEntry {
public function setUrl($url) {
$parameters = array();
$matches = array();
preg_match_all('/'.self::PARAMETERS_REGEX_MATCH.'/is', $url, $parameters);
if(preg_match_all('/'.self::PARAMETERS_REGEX_MATCH.'/is', $url, $matches)) {
$parameters = $matches[1];
}
$parameters = $parameters[1];
if($parameters !== null) {
if(count($parameters)) {
foreach($parameters as $param) {
$this->parameters[$param] = '';
}