Merge pull request #3 from skipperbent/development

[FEATURE/BUGFIX] Improvements
This commit is contained in:
Simon Sessingø
2015-10-18 23:20:14 +02:00
3 changed files with 64 additions and 36 deletions
+7 -8
View File
@@ -1,6 +1,7 @@
<?php
namespace Pecee\SimpleRouter;
use Pecee\ArrayUtil;
use Pecee\Http\Request;
use Pecee\Url;
@@ -245,15 +246,13 @@ class RouterBase {
}
}
// Nothing found - return current route
if($this->loadedRoute) {
$getParams = ($getParams === null) ? array() : $getParams;
$params = ($this->loadedRoute->getParameters() == null) ? array() : $this->loadedRoute->getParameters();
$parameters = ($parameters === null) ? array() : $parameters;
return $this->processUrl($this->loadedRoute, null, array_merge($params, $parameters), array_merge($_GET, $getParams));
}
$controller = ($controller === null) ? '/' : $controller;
$url = array($controller);
return '/';
if(is_array($parameters)) {
ArrayUtil::append($url, $parameters);
}
return join('/', $url);
}
public static function getInstance() {
+12
View File
@@ -23,6 +23,7 @@ abstract class RouterEntry {
protected $callback;
protected $parameters;
protected $parametersRegex;
protected $regexMatch;
public function __construct() {
$this->settings = array();
@@ -158,6 +159,17 @@ abstract class RouterEntry {
return $this;
}
/**
* Add regular expression match for url
*
* @param string $regex
* @return self
*/
public function match($regex) {
$this->regexMatch = $regex;
return $this;
}
/**
* Get settings that are allowed to be inherited by child routes.
*
+45 -28
View File
@@ -6,6 +6,8 @@ use Pecee\Http\Request;
class RouterRoute extends RouterEntry {
const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?)}';
protected $url;
protected $requestTypes;
@@ -18,13 +20,13 @@ class RouterRoute extends RouterEntry {
$this->requestTypes = array();
}
protected function parseParameters($url, $multiple = false) {
protected function parseParameters($url, $multiple = false, $regex = self::PARAMETERS_REGEX_MATCH) {
$parameters = array();
if($multiple) {
preg_match_all('/{([A-Za-z\-\_]*?)}/is', $url, $parameters);
preg_match_all('/'.$regex.'/is', $url, $parameters);
} else {
preg_match('/{([A-Za-z\-\_]*?)}/is', $url, $parameters);
preg_match('/'.$regex.'/is', $url, $parameters);
}
if(isset($parameters[1]) && count($parameters[1]) > 0) {
@@ -42,43 +44,58 @@ class RouterRoute extends RouterEntry {
$url = parse_url($request->getUri());
$url = $url['path'];
$url = explode('/', trim($url, '/'));
$route = explode('/', trim($this->url, '/'));
$route = $this->url;
$routeMatch = preg_replace('/'.self::PARAMETERS_REGEX_MATCH.'/is', '', $route);
// Check if url parameter count matches
if(count($url) === count($route)) {
$parameters = array();
if(stripos($url, $routeMatch) === 0) {
$matches = true;
// Check if url matches
foreach($route as $i => $path) {
$parameter = $this->parseParameters($path);
if($this->regexMatch) {
$parameters = $this->parseParameters($url, true, $this->regexMatch);
// Check if parameter of path matches, otherwise quit..
if(is_null($parameter) && strtolower($path) != strtolower($url[$i])) {
$matches = false;
break;
// If regex doesn't match, make sure to return an array
if(!is_array($parameters)) {
$parameters = array();
}
// Save parameter if we have one
if($parameter) {
$parameterValue = $url[$i];
$regex = (isset($this->parametersRegex[$parameter]) ? $this->parametersRegex[$parameter] : null);
} else {
if($regex !== null) {
// Use the regular expression rule provided to filter the value
$matches = array();
preg_match('/'.$regex.'/is', $url[$i], $matches);
$url = explode('/', $url);
$route = explode('/', $route);
if(count($matches)) {
$parameterValue = $matches[0];
}
$parameters = array();
// Check if url matches
foreach ($route as $i => $path) {
$parameter = $this->parseParameters($path, false);
// Check if parameter of path matches, otherwise quit..
if (is_null($parameter) && strtolower($path) != strtolower($url[$i])) {
$matches = false;
break;
}
// Add parameter value
$parameters[$parameter] = $parameterValue;
// Save parameter if we have one
if ($parameter) {
$parameterValue = $url[$i];
$regex = (isset($this->parametersRegex[$parameter]) ? $this->parametersRegex[$parameter] : null);
if ($regex !== null) {
// Use the regular expression rule provided to filter the value
$matches = array();
preg_match('/' . $regex . '/is', $url[$i], $matches);
if (count($matches)) {
$parameterValue = $matches[0];
}
}
// Add parameter value
$parameters[$parameter] = $parameterValue;
}
}
}