From 5fedd15b12338afd732d0c0367d80a5eed8b5a1b Mon Sep 17 00:00:00 2001 From: ljl Date: Fri, 26 Jan 2024 21:13:03 +0800 Subject: [PATCH] xiugai --- app/libs/const/class.ErrorCodeConst.php | 2 + .../order/class.OrderPrintController.php | 1 + .../exception/MethodNotAllowException.php | 8 ++ app/libs/exception/RouteNotFoundException.php | 8 ++ app/libs/middleware/AbstractMiddleware.php | 12 ++ app/libs/tool/Route.php | 82 ++++++++++++++ app/libs/tool/RouteManager.php | 103 ++++++++++++++++++ app/libs/tool/class.AppUrlHandler.php | 37 +------ app/routes/route.php | 7 ++ index.php | 4 +- 10 files changed, 232 insertions(+), 32 deletions(-) create mode 100644 app/libs/exception/MethodNotAllowException.php create mode 100644 app/libs/exception/RouteNotFoundException.php create mode 100644 app/libs/middleware/AbstractMiddleware.php create mode 100644 app/libs/tool/Route.php create mode 100644 app/libs/tool/RouteManager.php create mode 100644 app/routes/route.php diff --git a/app/libs/const/class.ErrorCodeConst.php b/app/libs/const/class.ErrorCodeConst.php index b2c42cf..d62d65e 100644 --- a/app/libs/const/class.ErrorCodeConst.php +++ b/app/libs/const/class.ErrorCodeConst.php @@ -40,6 +40,8 @@ class ErrorCodeConst { const systemPermissionDenied = 'system.permission-denied'; const systemDaoDefineUncorrect = 'system.dao-define-uncorrect'; const systemDaoNotFound = 'system.dao-not-found'; + const systemRouteNotFound = 'system.route-not-found'; + const systemMethodNotAllow = 'system.method-not-allow'; const systemAttributeDefinedUncorrect = 'system.attribute-defined-uncorrect'; const bizCommonError = 'biz.common-error'; const bizInvalidParameter = 'biz.invalid-parameter'; diff --git a/app/libs/controller/order/class.OrderPrintController.php b/app/libs/controller/order/class.OrderPrintController.php index fdfd330..02f3275 100644 --- a/app/libs/controller/order/class.OrderPrintController.php +++ b/app/libs/controller/order/class.OrderPrintController.php @@ -262,5 +262,6 @@ class OrderPrintController extends AbstractApiController { } public function test() { + var_dump('hahaha'); } } \ No newline at end of file diff --git a/app/libs/exception/MethodNotAllowException.php b/app/libs/exception/MethodNotAllowException.php new file mode 100644 index 0000000..cbe6052 --- /dev/null +++ b/app/libs/exception/MethodNotAllowException.php @@ -0,0 +1,8 @@ + $actionRoute, + 'methods' => $methods, + 'middlewares' => $options['middlewares'] ?: [] + ]; + } + + public static function group($groupUri, $callback, $moudle = '', $options = []) { + self::$groupUri = $groupUri; + $callback(); + self::$groupUri = ''; + } + + public static function getRouteMap() { + return self::$routeMap; + } + + private static function getRouteByAction($action, $moudle = '') { + $action = trim($action, '/'); + $actionItems = explode('/', $action); + $moudle = trim($moudle, '/'); + $route = $moudle ? '/' . $moudle . '/' : '/'; + foreach ($actionItems as $item) { + if (strpos($item, '@') !== false) { + list($controller, $method) = explode('@', $item); + $controller = str_replace('Controller', '', $controller); + $route .= self::toUnderScore($controller. '/' . $method); + } else { + $route .= $item . '/'; + } + } + return $route; + } + + private static function toUnderScore($str) { + $dstr = preg_replace_callback('/([A-Z])/', function ($matchs) { + return '_' . strtolower($matchs[0]); + }, $str); + return trim(preg_replace('/_{2,}/', '_', $dstr), '_'); + } +} \ No newline at end of file diff --git a/app/libs/tool/RouteManager.php b/app/libs/tool/RouteManager.php new file mode 100644 index 0000000..145e37d --- /dev/null +++ b/app/libs/tool/RouteManager.php @@ -0,0 +1,103 @@ +routeConfigPath = $routeConfigPath; + } else { + $this->routeConfigPath = APP_PATH . '/routes'; + } + $this->initRoutes(); + } + + public function rewrite($route) { + $routeMap = Route::getRouteMap(); + $routeInfo = $routeMap[$route]; + $route = $routeInfo ? $routeInfo['actionRoute'] : $route; + $this->checkMethodAllow($routeInfo['methods']); + $this->checkActionExists($route); + return $route; + } + + private function checkMethodAllow($allowMethods) { + $allowMethods = ['get', 'post']; + array_walk($allowMethods, function(&$value) { + $value = strtoupper($value); + }); + if(!in_array(strtoupper($_SERVER['REQUEST_METHOD']), $allowMethods)) { + throw new MethodNotAllowException('Method not allow.'); + } + } + + private function checkActionExists($route) { + $routeItems = explode('/', trim($route, '/')); + $routeItemLen = count($routeItems); + if ($routeItemLen < 2) { + throw new RouteNotFoundException('Route[' . $route . '] can not parse.'); + } + $action = $routeItems[$routeItemLen - 1]; + $controller = $routeItems[$routeItemLen - 2]; + + $pathItems = array_slice($routeItems, 0, $routeItemLen - 2); + $controllerPath = ''; + if (!empty($pathItems)) { + $controllerPath = implode('/', $pathItems); + } + + $controllerDir = Zc::C(ZcConfigConst::DirFsLibsController); + $controllerClass = ucfirst($this->toCamel($controller)) . 'Controller'; + $controllerFile = $controllerPath . '/' . 'class.' . $controllerClass . '.php'; + if (!is_file($controllerDir . $controllerFile)) { + throw new RouteNotFoundException('Can nott find controller[' . $controllerFile . '].'); + } + + require_once($controllerDir . $controllerFile); + + $reflection = new ReflectionClass($controllerClass); + if (!$reflection->hasMethod($this->toCamel($action))) { + throw new RouteNotFoundException('Can nott find method[' . $action . '] in ' . $controllerFile . '.'); + } + } + + private function initRoutes() { + $files = $this->getDirFiles($this->routeConfigPath); + foreach ($files as $file) { + require $file; + } + } + + private function getDirFiles($path) { + $files = []; + if(is_dir($path)) { + $dir = scandir($path); + foreach($dir as $value) { + $file = $path . '/'. $value; + if ($value == '.' || $value == '..') { + continue; + } elseif (is_dir($file)) { + $files = array_merge($files, getDir($file)); + } else { + $files[] = $file; + } + } + } + return $files; + } + + private function toCamel($str) { + $array = explode('_', $str); + $result = $array[0]; + $len = count($array); + if ($len > 1) { + for ($i = 1; $i < $len; $i++) { + $result .= ucfirst($array[$i]); + } + } + return $result; + } +} \ No newline at end of file diff --git a/app/libs/tool/class.AppUrlHandler.php b/app/libs/tool/class.AppUrlHandler.php index 9174163..b7953e9 100644 --- a/app/libs/tool/class.AppUrlHandler.php +++ b/app/libs/tool/class.AppUrlHandler.php @@ -1,4 +1,7 @@ isUseRealRoute($route)) { - return $route; - } - if (AppConst::isRubyWeb()) { - if (stripos($route, 'ruby/') === 0) { - return $route; - } else { - return 'ruby/' . $route; - } - } else if (AppConst::isRubyDesktop()) { - if (stripos($route, 'ruby_dt/') === 0) { - return $route; - } else { - return 'ruby_dt/' . $route; - } - } else if (AppConst::isPddTaoBaiKeApp()) { - if (stripos($route, 'taobaike/') === 0) { - return $route; - } else { - return 'taobaike/' . $route; - } - } - - return $route; + private function rewriteRoute($route) { + $manager = new RouteManager(); + return $manager->rewrite($route); } private function isUseRealRoute($route){ diff --git a/app/routes/route.php b/app/routes/route.php new file mode 100644 index 0000000..9cb9f10 --- /dev/null +++ b/app/routes/route.php @@ -0,0 +1,7 @@ + [], 'apps' => []]); + Route::get('/order-print-test', 'OrderPrintController@test'); + Route::get('/dgfdgs/sdfs', 'OrderPrintController@test'); +}, 'order', ['middleware' => []]); \ No newline at end of file diff --git a/index.php b/index.php index 94ea49e..213d5ad 100644 --- a/index.php +++ b/index.php @@ -4,10 +4,12 @@ require 'vendor/autoload.php'; $rootDir = dirname(__FILE__); $appDir = 'app'; +define('APP_PATH', $rootDir . '/app'); + if (substr($_SERVER['HTTP_HOST'], -3, 3) === '.me') { @ini_set('display_errors', '1'); error_reporting(E_ALL); -} +} ini_set('default_charset', 'utf-8'); // 启动MVC include $rootDir . '/zc-framework/zc.php';