/** * 解析一个静态资源的内容 * */ public static function parseResourceFile() { $pathinfo = Route::getPathInfo(); array_shift($pathinfo); $resource = implode('/', $pathinfo); if (Cml::$debug) { $appName = $file = ''; $i = 0; while (true) { $resource = ltrim($resource, '/'); $pos = strpos($resource, '/'); $appName = ($appName == '' ? '' : $appName . DIRECTORY_SEPARATOR) . substr($resource, 0, $pos); $resource = substr($resource, $pos); $file = Cml::getApplicationDir('apps_path') . DIRECTORY_SEPARATOR . $appName . DIRECTORY_SEPARATOR . Cml::getApplicationDir('app_static_path_name') . $resource; if (is_file($file) || ++$i >= 3) { break; } } if (is_file($file)) { Response::sendContentTypeBySubFix(substr($resource, strrpos($resource, '.') + 1)); exit(file_get_contents($file)); } else { Response::sendHttpStatus(404); } } }
/** * 解析一个静态资源的内容 * */ public static function parseResourceFile() { $pathinfo = Route::getPathInfo(); array_shift($pathinfo); $resource = implode('/', $pathinfo); if ($GLOBALS['debug'] && CML_IS_MULTI_MODULES) { $pos = strpos($resource, '/'); $file = CML_APP_MODULES_PATH . DIRECTORY_SEPARATOR . substr($resource, 0, $pos) . DIRECTORY_SEPARATOR . Config::get('modules_static_path_name') . substr($resource, $pos); if (is_file($file)) { Response::sendContentTypeBySubFix(substr($resource, strrpos($resource, '.') + 1)); exit(file_get_contents($file)); } else { Response::sendHttpStatus(404); } } }
/** * 解析url * * @return mixed */ public function parseUrl() { $dispatcher = \FastRoute\simpleDispatcher(function (RouteCollector $r) { foreach ($this->routes as $route) { $r->addRoute($route['method'], $route['uri'], $route['action']); } }); \Cml\Route::parsePathInfo(); $httpMethod = isset($_POST['_method']) ? strtoupper($_POST['_method']) : strtoupper($_SERVER['REQUEST_METHOD']); $routeInfo = $dispatcher->dispatch($httpMethod, implode('/', \Cml\Route::getPathInfo())); switch ($routeInfo[0]) { case Dispatcher::NOT_FOUND: case Dispatcher::METHOD_NOT_ALLOWED: break; case Dispatcher::FOUND: $_GET += $routeInfo[2]; if (is_callable($routeInfo[1])) { call_user_func($routeInfo[1]); Cml::cmlStop(); } $this->parseUrlParams($routeInfo[1]); break; } return $dispatcher; }
/** * 解析url * * @return void */ public function parseUrl() { $path = '/'; //定义URL常量 $subDir = dirname($_SERVER['SCRIPT_NAME']); if ($subDir == '/' || $subDir == '\\') { $subDir = ''; } //定义项目根目录地址 self::$urlParams['root'] = $subDir . '/'; \Cml\Route::parsePathInfo(); $pathInfo = \Cml\Route::getPathInfo(); //检测路由 if (self::$rules) { //配置了路由,所有请求通过路由处理 $isRoute = self::isRoute($pathInfo); if ($isRoute[0]) { //匹配路由成功 if (is_array($isRoute['route'])) { self::$urlParams['action'] = $isRoute['route'][2]; self::$urlParams['controller'] = $isRoute['route'][1]; $path = self::$urlParams['path'] = $isRoute['route'][0]; } else { $routeArr = explode('/', $isRoute['route']); $isRoute = null; self::$urlParams['action'] = array_pop($routeArr); self::$urlParams['controller'] = ucfirst(array_pop($routeArr)); $controllerPath = ''; while ($dir = array_shift($routeArr)) { if ($path == '/') { $path .= $dir . '/'; } else { $controllerPath .= $dir . '/'; } } self::$urlParams['controller'] = $controllerPath . self::$urlParams['controller']; unset($routeArr); } } else { self::findAction($pathInfo, $path); //未匹配到路由 按文件名映射查找 } } else { self::findAction($pathInfo, $path); //未匹配到路由 按文件名映射查找 } $pathInfo = array_values($pathInfo); for ($i = 0; $i < count($pathInfo); $i += 2) { $_GET[$pathInfo[$i]] = $pathInfo[$i + 1]; } unset($pathInfo); self::$urlParams['path'] = $path ? $path : '/'; unset($path); $_REQUEST = array_merge($_REQUEST, $_GET); }