2020-01-08 09:25:45 -06: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
|
2020-01-08 09:25:45 -06:00
|
|
|
* @param callable $function Function to call if route with allowed method is found
|
2019-02-21 17:08:36 -06:00
|
|
|
* @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'){
|
2020-01-08 09:25:45 -06:00
|
|
|
array_push(self::$routes, Array(
|
2018-03-13 10:25:37 -05:00
|
|
|
'expression' => $expression,
|
|
|
|
'function' => $function,
|
2018-03-13 10:01:52 -05:00
|
|
|
'method' => $method
|
2018-03-13 10:25:37 -05:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-01-08 09:25:45 -06:00
|
|
|
public static function pathNotFound($function) {
|
2018-03-13 10:25:37 -05:00
|
|
|
self::$pathNotFound = $function;
|
|
|
|
}
|
|
|
|
|
2020-01-08 09:25:45 -06:00
|
|
|
public static function methodNotAllowed($function) {
|
2018-03-13 10:25:37 -05:00
|
|
|
self::$methodNotAllowed = $function;
|
|
|
|
}
|
|
|
|
|
2020-01-11 15:44:56 -06:00
|
|
|
public static function run($basepath = '/', $case_matters = false, $trailing_slash_matters = false, $multimatch = false) {
|
2020-01-08 09:25:45 -06:00
|
|
|
// Parse current URL
|
|
|
|
$parsed_url = parse_url($_SERVER['REQUEST_URI']);
|
2018-03-13 10:25:37 -05:00
|
|
|
|
2020-01-08 09:25:45 -06:00
|
|
|
if (isset($parsed_url['path']) && $parsed_url['path'] != '/') {
|
|
|
|
if ($trailing_slash_matters) {
|
2019-01-31 03:54:07 -06:00
|
|
|
$path = $parsed_url['path'];
|
2020-01-08 09:25:45 -06:00
|
|
|
} else {
|
2019-01-31 03:54:07 -06:00
|
|
|
$path = rtrim($parsed_url['path'], '/');
|
|
|
|
}
|
2020-01-08 09:25:45 -06:00
|
|
|
} else {
|
2018-03-13 10:25:37 -05:00
|
|
|
$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
|
|
|
|
2020-01-08 09:25:45 -06: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
|
2020-01-08 09:25:45 -06:00
|
|
|
if ($basepath != '' && $basepath != '/') {
|
2018-03-13 10:01:52 -05:00
|
|
|
$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
|
|
|
|
2019-01-31 03:54:07 -06:00
|
|
|
// Check path match
|
2020-01-08 09:25:45 -06:00
|
|
|
if (preg_match('#'.$route['expression'].'#'.($case_matters ? '' : 'i'), $path, $matches)) {
|
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
|
2020-01-08 09:25:45 -06:00
|
|
|
if (strtolower($method) == strtolower($allowedMethod)) {
|
|
|
|
array_shift($matches); // Always remove first element. This contains the whole string
|
2018-03-13 10:25:37 -05:00
|
|
|
|
2020-01-08 09:25:45 -06:00
|
|
|
if ($basepath != '' && $basepath != '/') {
|
|
|
|
array_shift($matches); // Remove basepath
|
|
|
|
}
|
2018-03-13 10:25:37 -05:00
|
|
|
|
2020-01-08 09:25:45 -06:00
|
|
|
call_user_func_array($route['function'], $matches);
|
2018-03-13 10:25:37 -05:00
|
|
|
|
2020-01-08 09:25:45 -06:00
|
|
|
$route_match_found = true;
|
2018-03-13 10:25:37 -05:00
|
|
|
|
2020-01-08 09:25:45 -06:00
|
|
|
// Do not check other routes
|
|
|
|
break;
|
|
|
|
}
|
2018-03-13 10:01:52 -05:00
|
|
|
}
|
2018-03-13 10:25:37 -05:00
|
|
|
}
|
2020-01-11 15:44:56 -06:00
|
|
|
|
|
|
|
// Break the loop if the first found route is a match
|
|
|
|
if($route_match_found&&!$multimatch){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-03-13 10:25:37 -05:00
|
|
|
}
|
|
|
|
|
2018-03-13 10:01:52 -05:00
|
|
|
// No matching route was found
|
2020-01-08 09:25:45 -06:00
|
|
|
if (!$route_match_found) {
|
2018-03-13 10:01:52 -05:00
|
|
|
// But a matching path exists
|
2020-01-08 09:25:45 -06:00
|
|
|
if ($path_match_found) {
|
|
|
|
if (self::$methodNotAllowed) {
|
2018-03-13 10:01:52 -05:00
|
|
|
call_user_func_array(self::$methodNotAllowed, Array($path,$method));
|
|
|
|
}
|
2020-01-08 09:25:45 -06:00
|
|
|
} else {
|
|
|
|
if (self::$pathNotFound) {
|
2018-03-13 10:01:52 -05:00
|
|
|
call_user_func_array(self::$pathNotFound, Array($path));
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 10:25:37 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|