/** * 执行应用程序 * @access public * @param Request $request Request对象 * @return Response * @throws Exception */ public static function run(Request $request = null) { is_null($request) && ($request = Request::instance()); if ('ico' == $request->ext()) { throw new HttpException(404, 'ico file not exists'); } $config = self::initCommon(); try { // 开启多语言机制 if ($config['lang_switch_on']) { // 获取当前语言 $request->langset(Lang::detect()); // 加载系统语言包 Lang::load(THINK_PATH . 'lang' . DS . $request->langset() . EXT); if (!$config['app_multi_module']) { Lang::load(APP_PATH . 'lang' . DS . $request->langset() . EXT); } } // 获取应用调度信息 $dispatch = self::$dispatch; if (empty($dispatch)) { // 进行URL路由检测 $dispatch = self::routeCheck($request, $config); } // 记录当前调度信息 $request->dispatch($dispatch); // 记录路由信息 self::$debug && Log::record('[ ROUTE ] ' . var_export($dispatch, true), 'info'); // 监听app_begin Hook::listen('app_begin', $dispatch); switch ($dispatch['type']) { case 'redirect': // 执行重定向跳转 $data = Response::create($dispatch['url'], 'redirect')->code($dispatch['status']); break; case 'module': // 模块/控制器/操作 $data = self::module($dispatch['module'], $config, isset($dispatch['convert']) ? $dispatch['convert'] : null); break; case 'controller': // 执行控制器操作 $data = Loader::action($dispatch['controller'], $dispatch['params']); break; case 'method': // 执行回调方法 $data = self::invokeMethod($dispatch['method'], $dispatch['params']); break; case 'function': // 执行闭包 $data = self::invokeFunction($dispatch['function'], $dispatch['params']); break; case 'response': $data = $dispatch['response']; break; default: throw new \InvalidArgumentException('dispatch type not support'); } } catch (HttpResponseException $exception) { $data = $exception->getResponse(); } // 清空类的实例化 Loader::clearInstance(); // 输出数据到客户端 if ($data instanceof Response) { $response = $data; } elseif (!is_null($data)) { // 默认自动识别响应输出类型 $isAjax = $request->isAjax(); $type = $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type'); $response = Response::create($data, $type); } else { $response = Response::create(); } // 监听app_end Hook::listen('app_end', $response); // Trace调试注入 if (Config::get('app_trace')) { Debug::inject($response); } return $response; }
/** * 发送数据到客户端 * @access public * @return mixed * @throws \InvalidArgumentException */ public function send() { // 处理输出数据 $data = $this->getContent(); // Trace调试注入 if (Env::get('app_trace', Config::get('app_trace'))) { Debug::inject($this, $data); } if (!headers_sent() && !empty($this->header)) { // 发送状态码 http_response_code($this->code); // 发送头部信息 foreach ($this->header as $name => $val) { header($name . ':' . $val); } } if (200 == $this->code) { $cache = Request::instance()->getCache(); if ($cache) { header('Cache-Control: max-age=' . $cache[1] . ',must-revalidate'); header('Last-Modified:' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Expires:' . gmdate('D, d M Y H:i:s', $_SERVER['REQUEST_TIME'] + $cache[1]) . ' GMT'); $header['Content-Type'] = $this->header['Content-Type']; Cache::set($cache[0], [$data, $header], $cache[1]); } } echo $data; if (function_exists('fastcgi_finish_request')) { // 提高页面响应 fastcgi_finish_request(); } // 监听response_end Hook::listen('response_end', $this); }
/** * 发送数据到客户端 * @access public * @return mixed * @throws \InvalidArgumentException */ public function send() { // 处理输出数据 $data = $this->getContent(); // Trace调试注入 if (Env::get('app_trace', Config::get('app_trace'))) { Debug::inject($this, $data); } if (!headers_sent() && !empty($this->header)) { // 发送状态码 http_response_code($this->code); // 发送头部信息 foreach ($this->header as $name => $val) { header($name . ':' . $val); } } echo $data; if (function_exists('fastcgi_finish_request')) { // 提高页面响应 fastcgi_finish_request(); } }
/** * 发送数据到客户端 * @access public * @return mixed * @throws \InvalidArgumentException */ public function send() { // 处理输出数据 $data = $this->getContent(); // Trace调试注入 if (Env::get('app_trace', Config::get('app_trace'))) { Debug::inject($this, $data); } if (!headers_sent() && !empty($this->header)) { // 发送状态码 http_response_code($this->code); // 发送头部信息 foreach ($this->header as $name => $val) { header($name . ':' . $val); } } echo $data; if (200 == $this->code) { $cache = Request::instance()->getCache(); if ($cache) { Cache::set($cache[0], $data, $cache[1]); Cache::set($cache[0] . '_header', $this->header['Content-Type']); } } if (function_exists('fastcgi_finish_request')) { // 提高页面响应 fastcgi_finish_request(); } // 监听response_end Hook::listen('response_end', $this); }