| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- Lib::loadClass('HttpException');
- class Router {
- public static function route($route) {
- if (empty($route)) return;
- try {
- $route = self::getRoute($route);
- $route->route();
- } catch (HttpException $e) {
- Http::sendHeaderByCode($e->getCode());
- die($e->getMessage());
- } catch (Exception $e) {
- die($e->getMessage());
- }
- exit;
- }
- public static function handleAuth($route) {
- if (empty($route)) return;
- try {
- $route = self::getRoute($route);
- $route->handleAuth();
- } catch (HttpException $e) {
- Http::sendHeaderByCode($e->getCode());
- die($e->getMessage());
- } catch (Exception $e) {
- die($e->getMessage());
- }
- }
- public static function getRoute($route) {
- static $_routeInstances = null;
- if (!is_array($_routeInstances)) $_routeInstances = array();
- if (empty($route)) return;
- if (array_key_exists($route, $_routeInstances)) return $_routeInstances[$route];
- $routeClassName = "Route_{$route}";
- if ('UrlAction' === substr($route, 0, strlen('UrlAction'))) { // Use project/{$activeProject}/tools/{$routeName}.php
- $routeFileName = substr($route, strlen('UrlAction') + 1);
- $routeClassName = "RouteTool_{$routeFileName}";
- $activeProjectPath = Config::getProjectPath();
- if ($activeProjectPath) {
- $toolPath = "{$activeProjectPath}/tools/{$routeFileName}.php";
- if (file_exists($toolPath)) {
- require_once $toolPath;
- if (!class_exists($routeClassName)) throw new HttpException("Not found class", 404);
- $_routeInstances[$route] = new $routeClassName();
- return $_routeInstances[$route];
- }
- }
- }
- $routeClassName = "Route_{$route}";
- if (Lib::tryLoadClass($routeClassName)) {
- $_routeInstances[$route] = new $routeClassName();
- return $_routeInstances[$route];
- } else {
- throw new HttpException("Not found", 404);
- }
- }
- }
|