SimpleRouter/Route.php

115 lines
3.2 KiB
PHP
Raw Normal View History

<?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;
/**
* Function used to add a new route
* @param string $expression Route string or expression
* @param callable $function Function to call if 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(
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
));
}
public static function pathNotFound($function) {
2018-03-13 10:25:37 -05:00
self::$pathNotFound = $function;
}
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) {
// Parse current URL
$parsed_url = parse_url($_SERVER['REQUEST_URI']);
2018-03-13 10:25:37 -05:00
if (isset($parsed_url['path']) && $parsed_url['path'] != '/') {
if ($trailing_slash_matters) {
$path = $parsed_url['path'];
} else {
$path = rtrim($parsed_url['path'], '/');
}
} 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
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
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
// Check path match
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
// 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)) {
array_shift($matches); // Always remove first element. This contains the whole string
2018-03-13 10:25:37 -05:00
if ($basepath != '' && $basepath != '/') {
array_shift($matches); // Remove basepath
}
2018-03-13 10:25:37 -05:00
call_user_func_array($route['function'], $matches);
2018-03-13 10:25:37 -05:00
$route_match_found = true;
2018-03-13 10:25:37 -05: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
if (!$route_match_found) {
2018-03-13 10:01:52 -05:00
// But a matching path exists
if ($path_match_found) {
if (self::$methodNotAllowed) {
2018-03-13 10:01:52 -05:00
call_user_func_array(self::$methodNotAllowed, Array($path,$method));
}
} 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
}
}
}