1
0
mirror of https://github.com/dannyvankooten/AltoRouter.git synced 2025-08-02 14:37:43 +02:00

Fixed indentation, all tabs now

This commit is contained in:
Koen Punt
2012-09-07 20:38:52 +02:00
parent 68d9d2a4af
commit 65933f5a4d

View File

@@ -100,8 +100,8 @@ class AltoRouter {
// Strip query string (?a=b) from Request Url // Strip query string (?a=b) from Request Url
if (false !== strpos($requestUrl, '?')) { if (false !== strpos($requestUrl, '?')) {
$requestUrl = strstr($requestUrl, '?', true); $requestUrl = strstr($requestUrl, '?', true);
} }
// set Request Method if it isn't passed as a parameter // set Request Method if it isn't passed as a parameter
if($requestMethod === null) { if($requestMethod === null) {
@@ -109,82 +109,77 @@ class AltoRouter {
} }
// Force request_order to be GP // Force request_order to be GP
// http://www.mail-archive.com/internals@lists.php.net/msg33119.html // http://www.mail-archive.com/internals@lists.php.net/msg33119.html
$_REQUEST = array_merge($_GET, $_POST); $_REQUEST = array_merge($_GET, $_POST);
foreach($this->routes as $handler) { foreach($this->routes as $handler) {
list($method, $_route, $target, $name) = $handler; list($method, $_route, $target, $name) = $handler;
$methods = explode('|', $method); $methods = explode('|', $method);
$method_match = false; $method_match = false;
// Check if request method matches. If not, abandon early. (CHEAP) // Check if request method matches. If not, abandon early. (CHEAP)
foreach($methods as $method) { foreach($methods as $method) {
if (strcasecmp($requestMethod, $method) === 0) { if (strcasecmp($requestMethod, $method) === 0) {
$method_match = true; $method_match = true;
break; break;
} }
} }
// Method did not match, continue to next route. // Method did not match, continue to next route.
if(!$method_match) continue; if(!$method_match) continue;
// Check for a wildcard (matches all) // Check for a wildcard (matches all)
if ($_route === '*') { if ($_route === '*') {
$match = true; $match = true;
} elseif (isset($_route[0]) && $_route[0] === '@') { } elseif (isset($_route[0]) && $_route[0] === '@') {
$match = preg_match('`' . substr($_route, 1) . '`', $requestUrl, $params); $match = preg_match('`' . substr($_route, 1) . '`', $requestUrl, $params);
} else { } else {
$route = null; $route = null;
$regex = false; $regex = false;
$j = 0; $j = 0;
$n = isset($_route[0]) ? $_route[0] : null; $n = isset($_route[0]) ? $_route[0] : null;
$i = 0; $i = 0;
// Find the longest non-regex substring and match it against the URI // Find the longest non-regex substring and match it against the URI
while (true) { while (true) {
if (!isset($_route[$i])) { if (!isset($_route[$i])) {
break; break;
} elseif (false === $regex) { } elseif (false === $regex) {
$c = $n; $c = $n;
$regex = $c === '[' || $c === '(' || $c === '.'; $regex = $c === '[' || $c === '(' || $c === '.';
if (false === $regex && false !== isset($_route[$i+1])) { if (false === $regex && false !== isset($_route[$i+1])) {
$n = $_route[$i + 1]; $n = $_route[$i + 1];
$regex = $n === '?' || $n === '+' || $n === '*' || $n === '{'; $regex = $n === '?' || $n === '+' || $n === '*' || $n === '{';
} }
if (false === $regex && $c !== '/' && (!isset($requestUrl[$j]) || $c !== $requestUrl[$j])) { if (false === $regex && $c !== '/' && (!isset($requestUrl[$j]) || $c !== $requestUrl[$j])) {
continue 2; continue 2;
} }
$j++; $j++;
} }
$route .= $_route[$i++]; $route .= $_route[$i++];
} }
$regex = $this->compileRoute($route); $regex = $this->compileRoute($route);
$match = preg_match($regex, $requestUrl, $params); $match = preg_match($regex, $requestUrl, $params);
} }
if(($match == true || $match > 0)) {
if(($match == true || $match > 0)) { if($params) {
foreach($params as $key => $value) {
if($params) { if(is_numeric($key)) unset($params[$key]);
foreach($params as $key => $value) { }
if(is_numeric($key)) unset($params[$key]); }
}
}
return array(
'target' => $target,
'params' => $params,
'name' => $name
);
}
}
return false;
return array(
'target' => $target,
'params' => $params,
'name' => $name
);
}
}
return false;
} }
/** /**
@@ -193,38 +188,38 @@ class AltoRouter {
private function compileRoute($route) { private function compileRoute($route) {
if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
$match_types = array( $match_types = array(
'i' => '[0-9]++', 'i' => '[0-9]++',
'a' => '[0-9A-Za-z]++', 'a' => '[0-9A-Za-z]++',
'h' => '[0-9A-Fa-f]++', 'h' => '[0-9A-Fa-f]++',
'*' => '.+?', '*' => '.+?',
'**' => '.++', '**' => '.++',
'' => '[^/]++' '' => '[^/]++'
); );
foreach ($matches as $match) { foreach ($matches as $match) {
list($block, $pre, $type, $param, $optional) = $match; list($block, $pre, $type, $param, $optional) = $match;
if (isset($match_types[$type])) { if (isset($match_types[$type])) {
$type = $match_types[$type]; $type = $match_types[$type];
} }
if ($pre === '.') { if ($pre === '.') {
$pre = '\.'; $pre = '\.';
} }
//Older versions of PCRE require the 'P' in (?P<named>) //Older versions of PCRE require the 'P' in (?P<named>)
$pattern = '(?:' $pattern = '(?:'
. ($pre !== '' ? $pre : null) . ($pre !== '' ? $pre : null)
. '(' . '('
. ($param !== '' ? "?P<$param>" : null) . ($param !== '' ? "?P<$param>" : null)
. $type . $type
. '))' . '))'
. ($optional !== '' ? '?' : null); . ($optional !== '' ? '?' : null);
$route = str_replace($block, $pattern, $route); $route = str_replace($block, $pattern, $route);
} }
} }
return "`^$route$`"; return "`^$route$`";
} }
} }