Fixed mixed indents
This commit is contained in:
parent
7f6d6eb9c6
commit
3a8218687f
112
Route.php
112
Route.php
|
@ -1,89 +1,89 @@
|
||||||
<?PHP
|
<?PHP
|
||||||
|
|
||||||
class Route{
|
class Route{
|
||||||
|
|
||||||
private static $routes = Array();
|
private static $routes = Array();
|
||||||
private static $pathNotFound = null;
|
private static $pathNotFound = null;
|
||||||
private static $methodNotAllowed = null;
|
private static $methodNotAllowed = null;
|
||||||
|
|
||||||
public static function add($expression, $function, $method = 'get'){
|
public static function add($expression, $function, $method = 'get'){
|
||||||
array_push(self::$routes,Array(
|
array_push(self::$routes,Array(
|
||||||
'expression' => $expression,
|
'expression' => $expression,
|
||||||
'function' => $function,
|
'function' => $function,
|
||||||
'method' => $method
|
'method' => $method
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function pathNotFound($function){
|
public static function pathNotFound($function){
|
||||||
self::$pathNotFound = $function;
|
self::$pathNotFound = $function;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function methodNotAllowed($function){
|
public static function methodNotAllowed($function){
|
||||||
self::$methodNotAllowed = $function;
|
self::$methodNotAllowed = $function;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function run($basepath = '/'){
|
public static function run($basepath = '/'){
|
||||||
|
|
||||||
// Parse current url
|
// Parse current url
|
||||||
$parsed_url = parse_url($_SERVER['REQUEST_URI']);//Parse Uri
|
$parsed_url = parse_url($_SERVER['REQUEST_URI']);//Parse Uri
|
||||||
|
|
||||||
|
if(isset($parsed_url['path'])){
|
||||||
|
$path = $parsed_url['path'];
|
||||||
|
}else{
|
||||||
|
$path = '/';
|
||||||
|
}
|
||||||
|
|
||||||
if(isset($parsed_url['path'])){
|
|
||||||
$path = $parsed_url['path'];
|
|
||||||
}else{
|
|
||||||
$path = '/';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get current request method
|
// Get current request method
|
||||||
$method = $_SERVER['REQUEST_METHOD'];
|
$method = $_SERVER['REQUEST_METHOD'];
|
||||||
|
|
||||||
$path_match_found = false;
|
$path_match_found = false;
|
||||||
|
|
||||||
$route_match_found = false;
|
$route_match_found = false;
|
||||||
|
|
||||||
foreach(self::$routes as $route){
|
foreach(self::$routes as $route){
|
||||||
|
|
||||||
// If the method matches check the path
|
// If the method matches check the path
|
||||||
|
|
||||||
// Add basepath to matching string
|
// Add basepath to matching string
|
||||||
if($basepath!=''&&$basepath!='/'){
|
if($basepath!=''&&$basepath!='/'){
|
||||||
$route['expression'] = '('.$basepath.')'.$route['expression'];
|
$route['expression'] = '('.$basepath.')'.$route['expression'];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add 'find string start' automatically
|
// Add 'find string start' automatically
|
||||||
$route['expression'] = '^'.$route['expression'];
|
$route['expression'] = '^'.$route['expression'];
|
||||||
|
|
||||||
// Add 'find string end' automatically
|
// Add 'find string end' automatically
|
||||||
$route['expression'] = $route['expression'].'$';
|
$route['expression'] = $route['expression'].'$';
|
||||||
|
|
||||||
// echo $route['expression'].'<br/>';
|
// echo $route['expression'].'<br/>';
|
||||||
|
|
||||||
// Check path match
|
// Check path match
|
||||||
if(preg_match('#'.$route['expression'].'#',$path,$matches)){
|
if(preg_match('#'.$route['expression'].'#',$path,$matches)){
|
||||||
|
|
||||||
$path_match_found = true;
|
$path_match_found = true;
|
||||||
|
|
||||||
// Check method match
|
// Check method match
|
||||||
if(strtolower($method) == strtolower($route['method'])){
|
if(strtolower($method) == strtolower($route['method'])){
|
||||||
|
|
||||||
array_shift($matches);// Always remove first element. This contains the whole string
|
array_shift($matches);// Always remove first element. This contains the whole string
|
||||||
|
|
||||||
if($basepath!=''&&$basepath!='/'){
|
if($basepath!=''&&$basepath!='/'){
|
||||||
array_shift($matches);// Remove basepath
|
array_shift($matches);// Remove basepath
|
||||||
}
|
}
|
||||||
|
|
||||||
call_user_func_array($route['function'], $matches);
|
call_user_func_array($route['function'], $matches);
|
||||||
|
|
||||||
$route_match_found = true;
|
$route_match_found = true;
|
||||||
|
|
||||||
// Do not check other routes
|
// Do not check other routes
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// No matching route was found
|
// No matching route was found
|
||||||
if(!$route_match_found){
|
if(!$route_match_found){
|
||||||
|
|
||||||
// But a matching path exists
|
// But a matching path exists
|
||||||
if($path_match_found){
|
if($path_match_found){
|
||||||
header("HTTP/1.0 405 Method Not Allowed");
|
header("HTTP/1.0 405 Method Not Allowed");
|
||||||
|
@ -96,9 +96,9 @@ class Route{
|
||||||
call_user_func_array(self::$pathNotFound, Array($path));
|
call_user_func_array(self::$pathNotFound, Array($path));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user