diff options
Diffstat (limited to 'core/classes/Route.php')
| -rw-r--r-- | core/classes/Route.php | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/core/classes/Route.php b/core/classes/Route.php new file mode 100644 index 0000000..072723c --- /dev/null +++ b/core/classes/Route.php @@ -0,0 +1,97 @@ +<?php + +/** Route + * --- + * Methods: + * - add( expression , callback_function , method ) + * - notFound( callback_function ) + * - methodNotAllowed( callback_function ) + * - run() + * + * + matches static paths + * + matches dynamic paths through regex expressions + * + takes care of request-method + */ +class Route { + + private static $routes = Array(); // array to host routes + private static $notFound = null; // 404 error function + private static $methodNotAllowed = null; // 404 error function + + /** add (route) + * --- + * @param $expression : (string) static or regex matcher + * @param $function : callback function to be executed if expression is matched + * @param $method : get / post / any + */ + public static function add( $expression, $function, $method = 'get' ) + { + array_push(self::$routes, Array( + 'expression' => $expression, + 'function' => $function, + 'method' => strtolower($method) + )); + } + + + /** notFound($function) + * --- + * @param $function : call back function to be executed + */ + public static function notFound($function) + { + self::$notFound = $function; + } + + + /** run() + * --- + * Parse request ; Find mathing route ; + * then call route's function + * usualy a Controller::method([poarametres]) + */ + public static function run(Request $request) + { + // $request = Registry::get('REQUEST'); + $path = $request->PATH; // request path + $method = $request->METHOD; // request method + + $path_match_found = false; + $route_match_found = false; + + foreach(self::$routes as $route) { + + // If method matched check the path + if ($route['method'] == $method || $method == 'any') { + + // Add 'find string start' automatically + $route['expression'] = '^'.$route['expression']; + + // Add 'find string end' automatically + $route['expression'] = $route['expression'].'$'; + + // Check path match + if (preg_match('#'. $route['expression'] .'#', $path, $matches)) { + + $route_match_found = true; + + array_shift($matches); // Always remove first element. This contains the whole string + + call_user_func_array($route['function'], $matches); + break; // Do not check other routes + + } + } + } + + // No matching route was found + if (!$route_match_found) { + header("HTTP/1.0 404 Not Found"); + if (self::$notFound) { + call_user_func_array(self::$notFound, []); + } + } + + } + +} |
