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;
|
|
|
|
|
2019-02-21 17:08:36 -06:00
|
|
|
/**
|
|
|
|
* Function used to add a new route
|
|
|
|
* @param string $expression Route string or expression
|
|
|
|
* @param callable $function Function to call when route with allowed method is found
|
|
|
|
* @param string|array $method Either a string of allowed method or an array with string values
|
|
|
|
*
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-31 03:54:07 -06:00
|
|
|
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
|
|
|
|
|
2019-01-30 19:26:38 -06:00
|
|
|
if(isset($parsed_url['path']) && $parsed_url['path'] != '/'){
|
2019-01-31 03:54:07 -06:00
|
|
|
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
|
|
|
|
2018-03-12 11:12:05 -05:00
|
|
|
// echo $route['expression'].'<br/>';
|
2018-03-13 10:25:37 -05:00
|
|
|
|
2019-01-31 03:54:07 -06:00
|
|
|
// 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
|
|
|
|
2019-02-21 17:06:38 -06:00
|
|
|
// Cast allowed method to array if it's not one already, then run through all methods
|
|
|
|
foreach ((array)$route['method'] as $allowedMethod) {
|
|
|
|
// Check method match
|
|
|
|
if(strtolower($method) == strtolower($allowedMethod)){
|
2018-03-13 10:25:37 -05:00
|
|
|
|
2019-02-21 17:06:38 -06:00
|
|
|
array_shift($matches);// Always remove first element. This contains the whole string
|
2018-03-13 10:25:37 -05:00
|
|
|
|
2019-02-21 17:06:38 -06:00
|
|
|
if($basepath!=''&&$basepath!='/'){
|
|
|
|
array_shift($matches);// Remove basepath
|
|
|
|
}
|
2018-03-13 10:25:37 -05:00
|
|
|
|
2019-02-21 17:06:38 -06:00
|
|
|
call_user_func_array($route['function'], $matches);
|
2018-03-13 10:25:37 -05:00
|
|
|
|
2019-02-21 17:06:38 -06:00
|
|
|
$route_match_found = true;
|
2018-03-13 10:25:37 -05:00
|
|
|
|
2019-02-21 17:06:38 -06:00
|
|
|
// Do not check other routes
|
|
|
|
break;
|
|
|
|
}
|
2018-03-13 10:01:52 -05:00
|
|
|
}
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|