如何查看php源码架构,框架加载流程

### **立刻马上安装ThinkPHP6 打开 public/index.php开始源码攻读**

首先了解一下框架的加载流程做到心中有全局。

对于一个HTTP应用来说,从用户发起请求到响应输出结束,大致的标准请求流程如下:

载入Composer的自动加载autoload文件

require __DIR__ . '/../vendor/autoload.php';

实例化系统应用基础类think\App

$http = (new App());

获取应用目录等相关路径信息

$this->thinkPath = dirname(__DIR__) . DIRECTORY_SEPARATOR;

$this->rootPath = $rootPath ? rtrim($rootPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR : $this->getDefaultRootPath();

$this->appPath = $this->rootPath . 'app' . DIRECTORY_SEPARATOR;

$this->runtimePath = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR;

加载全局的服务提供provider.php文件

if (is_file($this->appPath . 'provider.php')) {

$this->bind(include $this->appPath . 'provider.php');

}

设置容器实例及应用对象实例,确保当前容器对象唯一

$this->instance('app', $this);

$this->instance('think\Container', $this);

从容器中获取HTTP应用类think\Http

$http = (new App())->http;

执行HTTP应用类的run方法启动一个HTTP应用

$response = $http->run();

获取当前请求对象实例(默认为 app\Request 继承think\Request)保存到容器

//自动创建request对象

$request = $request ?? $this->app->make('request', [], true);

$this->app->instance('request', $request);

执行think\App类的初始化方法initialize

$this->initialize();

加载环境变量文件.env和全局初始化文件

加载全局公共文件、系统助手函数、全局配置文件、全局事件定义和全局服务定义

判断应用模式(调试或者部署模式)

监听AppInit事件

?注册异常处理

服务注册

启动注册的服务

加载全局中间件定义

// 加载全局中间件

$this->loadMiddleware();

监听HttpRun事件

// 设置开启事件机制

$this->app->event->withEvent($this->app->config->get('app.with_event', true));

// 监听HttpRun

$this->app->event->trigger(HttpRun::class);

执行全局中间件

return $this->app->middleware->pipeline()

->send($request)

->then(function ($request) {

return $this->dispatchToRoute($request);

});

执行路由调度(Route类dispatch方法)

protected function dispatchToRoute($request)

{

$withRoute = $this->app->config->get('app.with_route', true) ? function () {

$this->loadRoutes();

} : null;

return $this->app->route->dispatch($request, $withRoute);

}

如果开启路由则检查路由缓存

加载路由定义

监听RouteLoaded事件

$routePath = $this->getRoutePath();

if (is_dir($routePath)) {

$files = glob($routePath . '*.php');

foreach ($files as $file) {

include $file;

}

}

$this->app->event->trigger(RouteLoaded::class);

?如果开启注解路由则检测注解路由

?路由检测(中间流程很复杂 略)

路由调度对象think\route\Dispatch初始化

// 获取控制器名

$controller = strip_tags($result[0] ?: $this->rule->config('default_controller'));

if (strpos($controller, '.')) {

$pos = strrpos($controller, '.');

$this->controller = substr($controller, 0, $pos) . '.' . Str::studly(substr($controller, $pos + 1));

} else {

$this->controller = Str::studly($controller);

}

设置当前请求的控制器和操作名

// 获取操作名

$this->actionName = strip_tags($result[1] ?: $this->rule->config('default_action'));

注册路由中间件

if (!empty($this->config['middleware'])) {

$this->app->middleware->import($this->config['middleware'], 'route');

}

绑定数据模型

设置路由额外参数

$this->group->removeSlash($this->removeSlash);

执行数据自动验证

if ($this->rule instanceof RuleItem && $this->request->method() == 'OPTIONS' && $this->rule->isAutoOptions()) {

$rules = $this->rule->getRouter()->getRule($this->rule->getRule());

$allow = [];

foreach ($rules as $item) {

$allow[] = strtoupper($item->getMethod());

}

return Response::create('', 'html', 204)->header(['Allow' => implode(', ', $allow)]);

}

执行路由调度子类的exec方法返回响应think\Response对象

$data = $this->exec();

return $this->autoResponse($data);

获取当前请求的控制器对象实例

利用反射机制注册控制器中间件

执行控制器方法以及前后置中间件

执行当前响应对象的send方法输出

$response->send();

执行HTTP应用对象的end方法善后

$http->end($response);

监听HttpEnd事件

执行中间件的end回调

写入当前请求的日志信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值