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