SimpleRouter/Route.php

109 lines
2.7 KiB
PHP
Raw Normal View History

2015-09-10 06:37:52 -05:00
<?PHP
class Route{
2018-03-13 10:25:37 -05:00
private static $routes = Array();
private static $pathNotFound = null;
2018-03-13 10:01:52 -05:00
private static $methodNotAllowed = null;
2018-03-13 10:25:37 -05:00
public static function add($expression, $function, $method = 'get'){
array_push(self::$routes,Array(
'expression' => $expression,
'function' => $function,
2018-03-13 10:01:52 -05:00
'method' => $method
2018-03-13 10:25:37 -05:00
));
}
public static function pathNotFound($function){
self::$pathNotFound = $function;
}
2018-03-13 10:01:52 -05:00
public static function methodNotAllowed($function){
2018-03-13 10:25:37 -05:00
self::$methodNotAllowed = $function;
}
public static function run($basepath = '/', $case_matters = false, $trailing_slash_matters = false){
2018-03-13 10:25:37 -05:00
2018-03-13 10:01:52 -05:00
// Parse current url
2018-03-13 10:25:37 -05:00
$parsed_url = parse_url($_SERVER['REQUEST_URI']);//Parse Uri
if(isset($parsed_url['path']) && $parsed_url['path'] != '/'){
if($trailing_slash_matters){
$path = $parsed_url['path'];
}else{
$path = rtrim($parsed_url['path'], '/');
}
2018-03-13 10:25:37 -05:00
}else{
$path = '/';
}
2018-03-13 10:01:52 -05:00
// Get current request method
$method = $_SERVER['REQUEST_METHOD'];
2018-03-13 10:25:37 -05:00
$path_match_found = false;
2018-03-13 10:01:52 -05:00
$route_match_found = false;
2018-03-13 10:25:37 -05:00
foreach(self::$routes as $route){
2018-03-13 10:01:52 -05:00
// If the method matches check the path
2018-03-13 10:25:37 -05:00
2018-03-12 10:49:55 -05:00
// Add basepath to matching string
2018-03-13 10:01:52 -05:00
if($basepath!=''&&$basepath!='/'){
$route['expression'] = '('.$basepath.')'.$route['expression'];
}
2018-03-13 10:25:37 -05:00
2018-03-13 10:01:52 -05:00
// Add 'find string start' automatically
$route['expression'] = '^'.$route['expression'];
2018-03-13 10:25:37 -05:00
2018-03-13 10:01:52 -05:00
// Add 'find string end' automatically
$route['expression'] = $route['expression'].'$';
2018-03-13 10:25:37 -05:00
// echo $route['expression'].'<br/>';
// Check path match
if(preg_match('#'.$route['expression'].'#'.($case_matters ? '':'i'),$path,$matches)){
2018-03-13 10:25:37 -05:00
2018-03-13 10:01:52 -05:00
$path_match_found = true;
2018-03-13 10:25:37 -05:00
2018-03-13 10:01:52 -05:00
// Check method match
if(strtolower($method) == strtolower($route['method'])){
2018-03-13 10:25:37 -05:00
2018-03-13 10:01:52 -05:00
array_shift($matches);// Always remove first element. This contains the whole string
2018-03-13 10:25:37 -05:00
2018-03-13 10:01:52 -05:00
if($basepath!=''&&$basepath!='/'){
array_shift($matches);// Remove basepath
}
2018-03-13 10:25:37 -05:00
2018-03-13 10:01:52 -05:00
call_user_func_array($route['function'], $matches);
2018-03-13 10:25:37 -05:00
2018-03-13 10:01:52 -05:00
$route_match_found = true;
2018-03-13 10:25:37 -05:00
2018-03-13 10:01:52 -05:00
// Do not check other routes
break;
}
2018-03-13 10:25:37 -05:00
}
}
2018-03-13 10:01:52 -05:00
// No matching route was found
2018-03-13 10:25:37 -05:00
if(!$route_match_found){
2018-03-13 10:01:52 -05:00
// But a matching path exists
if($path_match_found){
header("HTTP/1.0 405 Method Not Allowed");
if(self::$methodNotAllowed){
call_user_func_array(self::$methodNotAllowed, Array($path,$method));
}
}else{
header("HTTP/1.0 404 Not Found");
if(self::$pathNotFound){
call_user_func_array(self::$pathNotFound, Array($path));
}
}
2018-03-13 10:25:37 -05:00
}
}
}