[李景山php]每天TP5-20161212|App.php-2

来源:互联网 发布:数控铣床圆弧编程实例 编辑:程序博客网 时间:2024/06/06 08:51
    public static function run(Request $request = null)    {// thinkphp经过了 自动加载、错误接管、配置文件预设,终于开始执行了。        // 第一步:获取请求参数        is_null($request) && $request = Request::instance();        // self::$instance = new static($options); 执行了 这个 instance        // 默认 没有传入任何数值,is_null 所以执行 后面 $request = Request::instance();        // 这个写法 真的 很经典,我喜欢你,老刘,哈哈        // 最终获取了 一个 request 对象        $config = self::initCommon();// 加载 初始化 配置 文件 选项        // 2016-10-09        if (defined('BIND_MODULE')) {// 默认此处是没有定义的            // 模块/控制器绑定            BIND_MODULE && Route::bind(BIND_MODULE);//self::$bind = ['type' => $type, $type => $bind];            // 对 Route 的 $        } elseif ($config['auto_bind_module']) {// 默认这里也是没有定义的            // 入口自动绑定            $name = pathinfo($request->baseFile(), PATHINFO_FILENAME);            if ($name && 'index' != $name && is_dir(APP_PATH . $name)) {                Route::bind($name);            }        }        $request->filter($config['default_filter']);// 指定 过滤 参数 为空 直接 返回        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);// 配置调度 信息            // 记录路由和请求信息            if (self::$debug) {                Log::record('[ ROUTE ] ' . var_export($dispatch, true), 'info');                Log::record('[ HEADER ] ' . var_export($request->header(), true), 'info');                Log::record('[ PARAM ] ' . var_export($request->param(), 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']);                    break;                case 'method':                    // 执行回调方法                    $data = self::invokeMethod($dispatch['method']);                    break;                case 'function':                    // 执行闭包                    $data = self::invokeFunction($dispatch['function']);                    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);        return $response;    }
0 0
原创粉丝点击