thinkphp5框架启动解析

1.加载start.php

// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';

2.加载基础文件

require __DIR__ . '/base.php';

3.定义常量,加载loader类

define ...
// 载入Loader类
require CORE_PATH . 'Loader.php';//(包括psr4,自动加载autoload等静态方法)

4.加载环境变量配置(.env)

if (is_file(ROOT_PATH . '.env')) {
    $env = parse_ini_file(ROOT_PATH . '.env', true);

    foreach ($env as $key => $val) {
        $name = ENV_PREFIX . strtoupper($key);

        if (is_array($val)) {
            foreach ($val as $k => $v) {
                $item = $name . '_' . strtoupper($k);
                putenv("$item=$v");
            }
        } else {
            putenv("$name=$val");
        }
    }
}

5.注册自动加载机制

\think\Loader::register();

6.注册错误和异常处理机制

\think\Error::register();

7.加载惯例配置文件convention.php

\think\Config::set(include THINK_PATH . 'convention' . EXT);

8.执行应用

App::run()->send();

9.接受请求

$request = is_null($request) ? Request::instance() : $request;

10.初始化应用配置,并返回初始化信息

$config = self::initCommon();

11.模块控制器绑定或入口绑定

if (defined('BIND_MODULE')) {
    BIND_MODULE && Route::bind(BIND_MODULE);
} elseif ($config['auto_bind_module']) {
    // 入口自动绑定
    $name = pathinfo($request->baseFile(), PATHINFO_FILENAME);
    if ($name && 'index' != $name && is_dir(APP_PATH . $name)) {
        Route::bind($name);
    }
}
//12、获取调度信息
$dispatch = self::$dispatch;

//13设置调度信息则进行 URL 路由检测
if (empty($dispatch)) {
    $dispatch = self::routeCheck($request, $config);
}

//记录当前调度信息
$request->dispatch($dispatch);

14.执行对应的操作

$data = self::exec($dispatch, $config);

15.exec方法详情

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': // 执行控制器操作
        $vars = array_merge(Request::instance()->param(), $dispatch['var']);
        $data = Loader::action(
            $dispatch['controller'],
            $vars,
            $config['url_controller_layer'],
            $config['controller_suffix']
        );
        break;
    case 'method': // 回调方法
        $vars = array_merge(Request::instance()->param(), $dispatch['var']);
        $data = self::invokeMethod($dispatch['method'], $vars);
        break;
    case 'function': // 闭包
        $data = self::invokeFunction($dispatch['function']);
        break;
    case 'response': // Response 实例
        $data = $dispatch['response'];
        break;
    default:
        throw new \InvalidArgumentException('dispatch type not support');
}

return $data;

16.清空类的实例化

Loader::clearInstance();

17.输出数据到客户端

if ($data instanceof Response) {
    $response = $data;
} elseif (!is_null($data)) {
    // 默认自动识别响应输出类型
    $type = $request->isAjax() ?
    Config::get('default_ajax_return') :
    Config::get('default_return_type');

    $response = Response::create($data, $type);
} else {
    $response = Response::create();
}
return $response;

18.处理输出数据

$data = $this->getContent();
if (200 == $this->code) {
   $cache = Request::instance()->getCache();
    if ($cache) {
        $this->header['Cache-Control'] = 'max-age=' . $cache[1] . ',must-revalidate';
        $this->header['Last-Modified'] = gmdate('D, d M Y H:i:s') . ' GMT';
        $this->header['Expires']       = gmdate('D, d M Y H:i:s', $_SERVER['REQUEST_TIME'] + $cache[1]) . ' GMT';
        Cache::tag($cache[2])->set($cache[0], [$data, $this->header], $cache[1]);
    }
}

if (!headers_sent() && !empty($this->header)) {
    // 发送状态码
    http_response_code($this->code);
    // 发送头部信息
    foreach ($this->header as $name => $val) {
        if (is_null($val)) {
            header($name);
        } else {
            header($name . ':' . $val);
        }
    }
}

echo $data;

19.flushes all response data to the client and finishes the request

fastcgi_finish_request();
// 清空当次请求有效的数据
if (!($this instanceof RedirectResponse)) {
    Session::flush();
}

至此thinkphp5框架启动截止

发布了97 篇原创文章 · 获赞 59 · 访问量 29万+

猜你喜欢

转载自blog.csdn.net/b1303110335/article/details/81713882