Exemplo n.º 1
0
 /**
  * 执行应用程序
  * @access public
  * @param \think\Request $request Request对象
  * @return \think\Response
  * @throws Exception
  */
 public static function run($request)
 {
     // 初始化应用(公共模块)
     self::initModule(COMMON_MODULE, Config::get());
     // 获取配置参数
     $config = Config::get();
     // 注册根命名空间
     if (!empty($config['root_namespace'])) {
         Loader::addNamespace($config['root_namespace']);
     }
     // 加载额外文件
     if (!empty($config['extra_file_list'])) {
         foreach ($config['extra_file_list'] as $file) {
             $file = strpos($file, '.') ? $file : APP_PATH . $file . EXT;
             if (is_file($file)) {
                 include_once $file;
             }
         }
     }
     // 设置系统时区
     date_default_timezone_set($config['default_timezone']);
     // 监听app_init
     APP_HOOK && Hook::listen('app_init');
     // 开启多语言机制
     if ($config['lang_switch_on']) {
         // 获取当前语言
         defined('LANG_SET') or define('LANG_SET', Lang::range());
         // 加载系统语言包
         Lang::load(THINK_PATH . 'lang' . DS . LANG_SET . EXT);
         if (!APP_MULTI_MODULE) {
             Lang::load(APP_PATH . 'lang' . DS . LANG_SET . EXT);
         }
     }
     // 获取当前请求的调度信息
     $dispatch = $request->dispatch();
     if (empty($dispatch)) {
         // 未指定调度类型 则进行URL路由检测
         $dispatch = self::route($request, $config);
     }
     // 记录路由信息
     APP_DEBUG && Log::record('[ ROUTE ] ' . var_export($dispatch, true), 'info');
     // 监听app_begin
     APP_HOOK && Hook::listen('app_begin', $dispatch);
     try {
         switch ($dispatch['type']) {
             case 'redirect':
                 // 执行重定向跳转
                 header('Location: ' . $dispatch['url'], true, $dispatch['status']);
                 break;
             case 'module':
                 // 模块/控制器/操作
                 $data = self::module($dispatch['module'], $config);
                 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 'finish':
                 // 已经完成 不再继续执行
                 break;
             default:
                 throw new Exception('dispatch type not support', 10008);
         }
     } catch (HttpResponseException $exception) {
         $data = $exception->getResponse();
     }
     // 输出数据到客户端
     if (isset($data)) {
         if ($data instanceof Response) {
             return $data->send();
         } else {
             // 监听app_end
             APP_HOOK && Hook::listen('app_end', $data);
             // 自动响应输出
             return Response::instance()->send($data, '', Config::get('response_return'));
         }
     }
 }