多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### **讲在前面的话:**  无数人在使用TP6的过程中,被各种问题所困扰。比如:无法根据错误信息快速定位到问题所在,对于手册模棱两可的介绍不知所云,对于同事的高级使用技巧不知所措,想扩展框架功能却不知如何下手等等。究其根本原因就是对框架的实现原理不熟悉,但框架历经十四年发展,代码量庞大,纷繁复杂,无数人想深入其中一探究竟,但也只能望洋兴叹,止步不前,苦于无人领路。为了彻底扭转这一局面,决定编写和录制一套全面、系统、深入介绍TP6实现原理的文字课程和视频课程以帮助大家。 本套课程分为10个章节,分别从生命周期、请求与响应、数据库、视图、错误和日志、验证、session和cookie、模型、路由、命令行系统、全面、深入介绍框架实现原理。本章节为生命周期详解,分为九小节,分别为应用创建、配置加载、服务注册、事件、加载中间件、中间件执行、路由解析、执行控制器方法、响应输出,以下本章节详细内容。 ### 1.1 应用创建: TP6框架采用MVC模型组织项目,采用单一入口(所有请求都是请求同一个文件),由路由模块解析请求,获取应用、控制器、方法,接着执行方法,方法返回的内容有响应对象输出。整个生命周期的轮廓,在入口文件中一览无余。代码如下: ``` namespace think; require __DIR__ . '/../vendor/autoload.php'; // 执行HTTP应用并响应 $http = (new App())->http; // 执行应用 $response = $http->run(); // 内容输出 $response->send(); // 结束应用 $http->end($response); ``` 首先创建 App 对象,此对象为框架中最重要的一个对象,管理后续所创建的系统类对象,相当于对象容器。在创建的过程,会做一些初始化工作,主要是系统目录的获取,代码如下: ``` // vendor\topthink\framework\src\think\App.php 163行 public function __construct(string $rootPath = '') { // 框架核心目录 src/ $this->thinkPath = dirname(__DIR__) . DIRECTORY_SEPARATOR; // 应用根目录 比如:frshop/ $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; if (is_file($this->appPath . 'provider.php')) { $this->bind(include $this->appPath . 'provider.php'); } static::setInstance($this); $this->instance('app', $this); $this->instance('think\Container', $this); } ``` 接着创建 HTTP 应用,HTTP应用的创建比较特殊,通过获取 APP 对象不存在属性,从而触发 \_\_get() 魔术方法,最终调用 make() 方法来创建对象,并将对象放置于一个容器中,方便后期的获取,代码如下: ``` // vendor\topthink\framework\src\think\Container.php 239 行 public function make(string $abstract, array $vars = [], bool $newInstance = false) { $abstract = $this->getAlias($abstract); if (isset($this->instances[$abstract]) && !$newInstance) { return $this->instances[$abstract]; } if (isset($this->bind[$abstract]) && $this->bind[$abstract] instanceof Closure) { $object = $this->invokeFunction($this->bind[$abstract], $vars); } else { $object = $this->invokeClass($abstract, $vars); } if (!$newInstance) { $this->instances[$abstract] = $object; } return $object; } ``` 紧接着执行 HTTP 应用类的 run() 方法启动一个 HTTP 应用。 ### 1.2 配置加载: 其实,对于任何一个软件而言,应用执行的第一步都是加载配置,当然TP6也不例外。TP6的配置加载不局限于配置文件的加载,其中包括环境变量加载、助手函数文件加载、配置文件加载、事件文件加载、服务文件加载等等,代码如下: ``` // vendor\topthink\framework\src\think\App.php 493 行 protected function load(): void { $appPath = $this->getAppPath(); if (is_file($appPath . 'common.php')) { include_once $appPath . 'common.php'; } include_once $this->thinkPath . 'helper.php'; $configPath = $this->getConfigPath(); $files = []; if (is_dir($configPath)) { $files = glob($configPath . '*' . $this->configExt); } foreach ($files as $file) { $this->config->load($file, pathinfo($file, PATHINFO_FILENAME)); } if (is_file($appPath . 'event.php')) { $this->loadEvent(include $appPath . 'event.php'); } if (is_file($appPath . 'service.php')) { $services = include $appPath . 'service.php'; foreach ($services as $service) { $this->register($service); } } } ```   框架配置信息皆由 Config 对象接管,所有配置信息的设置和获取都通过 Config 对象所提供的的接口设置和获取,其中重要的接口有 has() 、set() 、 get()。 ### 1.3 服务注册: 什么是服务?服务就是一个插件,用来扩展内核的功能,分为自定义服务和内置服务,自定义服务可由命令行的 make 命令创建,在通过配置文件配置,就可被框架加载,配置方式如下: ``` // app\service.php use app\AppService; // 系统服务定义文件 // 服务在完成全局初始化之后执行 return [ AppService::class, ]; ```  注册方式如下: ``` // vendor\topthink\framework\src\think\App.php 519行 if (is_file($appPath . 'service.php')) { $services = include $appPath . 'service.php'; foreach ($services as $service) { $this->register($service); } } ``` 系统服务注册由 RegisterService 类完成,代码如下: ``` // vendor\topthink\framework\src\think\initializer\RegisterService.php /** * 注册系统服务 */ class RegisterService { protected $services = [ PaginatorService::class, ValidateService::class, ModelService::class, ]; public function init(App $app) { $file = $app->getRootPath() . 'vendor/services.php'; $services = $this->services; if (is_file($file)) { $services = array_merge($services, include $file); } foreach ($services as $service) { if (class_exists($service)) { $app->register($service); } } } } ``` ### 1.4 事件: 什么是事件?在某个地点,某个时间发生的一件事。纯粹讨论事件本身并没有多大意义的,而是事件发生后我们为此做些什么事才是有意义的,这就是事件绑定操作,也就是所谓的事件机制。事件机制能灵活扩展框架的功能,整个事件机制的完成需要三步,一、创建操作(或者叫事件监听),二、注册事件监听,三、触发事件,代码如下: 创建事件监听: ``` // 事件类可以通过命令行快速生成 php think make:event AppInit // 修改 event.php return [ 'bind' => [ ], 'listen' => [ 'AppInit' => ['app\listener\AppInit'], 'HttpRun' => [], 'HttpEnd' => [], 'LogLevel' => [], 'LogWrite' => [], ], 'subscribe' => [ ], ]; ``` 注册事件监听(此步骤由系统完成) ``` // vendor\topthink\framework\src\think\Event.php 59 public function listenEvents(array $events) { foreach ($events as $event => $listeners) { if (isset($this->bind[$event])) { $event = $this->bind[$event]; } $this->listener[$event] = array_merge($this->listener[$event] ?? [], $listeners); } return $this; } ``` 触发事件(此步骤由程序员完成) ``` // 触发事件 $app->event->trigger(AppInit::class); ``` ### 1.5 加载中间件:  什么是中间件?顾名思义,就是位于中间的组件,也就是位于生命周期中间的组件,作用就是扩展内核功能。中间件对于框架十分重要,好几个重要的工功能都是由中间件完成,像 session、请求缓存等等。我们也可以通过中间件来做权限的验证,不管是自己定义的中间件还是内置的中间件,他们的加载都依赖于配置文件,只有定义在配置文件中的中间件才能加载,代码如下: ``` // 全局中间件定义文件 app\middleware.php return [ // 全局请求缓存 \think\middleware\CheckRequestCache::class, // 多语言加载 \think\middleware\LoadLangPack::class, // Session初始化 \think\middleware\SessionInit::class ]; ``` 中间价加载,代码如下: ``` // 加载全局中间件 vendor\topthink\framework\src\think\Http.php 192 行 $this->loadMiddleware(); // loadMiddleware() vendor\topthink\framework\src\think\Http.php 216行 protected function loadMiddleware(): void { if (is_file($this->app->getBasePath() . 'middleware.php')) { $this->app->middleware->import(include $this->app->getBasePath() . 'middleware.php'); } } // import() 方法实现 vendor\topthink\framework\src\think\Middleware.php 51行 public function import(array $middlewares = [], string $type = 'global'): void { foreach ($middlewares as $middleware) { $this->add($middleware, $type); } } ``` 中间价由 Middleware 类接管,中间价存储在 Middleware 对象的 $queue 属性中。 ### 1.6 执行中间件:  中间件的执行应该是整个框架中最核心的地方,也是最难理解的地方,实现的非常巧妙,用文字难以表达清楚,大家可以观看视频教程,这里把代码贴出来。 ``` // 执行应用程序 vendor\topthink\framework\src\think\Http.php 187 行 protected function runWithRequest(Request $request) { $this->initialize(); // 加载全局中间件 $this->loadMiddleware(); // 监听HttpRun $this->app->event->trigger(HttpRun::class); // 这段代码涵盖生命周期的90%,其中包括中间件的执行 return $this->app->middleware->pipeline() ->send($request) ->then(function ($request) { return $this->dispatchToRoute($request); }); } ``` ``` // 调度管道 vendor\topthink\framework\src\think\Middleware.php 133 行 public function pipeline(string $type = 'global') { return (new Pipeline()) ->through(array_map(function ($middleware) { // 这里中间件的执行逻辑,但中间件并没有在这里执行,只是一个回调函数而已 return function ($request, $next) use ($middleware) { [$call, $params] = $middleware; if (is_array($call) && is_string($call[0])) { $call = [$this->app->make($call[0]), $call[1]]; } $response = call_user_func($call, $request, $next, ...$params); if (!$response instanceof Response) { throw new LogicException('The middleware must return Response instance'); } return $response; }; }, $this->sortMiddleware($this->queue[$type] ?? []))) ->whenException([$this, 'handleException']); } ``` ``` // 执行 vendor\topthink\framework\src\think\Pipeline.php 52 行 // 这段代码非常的拗口,其难点在于 array_reduce 这个函数,吃透这个函数,就能理解这段代码 public function then(Closure $destination) { $pipeline = array_reduce( array_reverse($this->pipes), $this->carry(), function ($passable) use ($destination) { try { return $destination($passable); } catch (Throwable | Exception $e) { return $this->handleException($passable, $e); } }); return $pipeline($this->passable); } ``` ### 1.7 路由解析 什么是路由?说穿了路由就是解决从哪里来要到哪里去的问题,框架的最小执行单元是方法,也就是所有的请求最终的落脚点都在方法,但是我们也知道框架是单一入口,所有的请求都是请求同一个文件,那怎么去定位方法呢,这个事由路由完成。路由就是通过解析请求信息,分析出应用->控制器->方法,然后调用方法,并且将方法返回的内容交给响应。核心代码如下: 路由调度 ``` // 通过此方法 将路由解析 从 http 对象转给 route 对象 // vendor\topthink\framework\src\think\Http.php 204 行 protected function dispatchToRoute($request) { $withRoute = $this->app->config->get('app.with_route', true) ? function () { $this->loadRoutes(); } : null; return $this->app->route->dispatch($request, $withRoute); } // 路由调度 这是路由解析的核心代码 // vendor\topthink\framework\src\think\Route.php 739 行 public function dispatch(Request $request, $withRoute = true) { $this->request = $request; $this->host = $this->request->host(true); $this->init(); if ($withRoute) { //加载路由 if ($withRoute instanceof Closure) { $withRoute(); } $dispatch = $this->check(); } else { $dispatch = $this->url($this->path()); } $dispatch->init($this->app); return $this->app->middleware->pipeline('route') ->send($request) ->then(function () use ($dispatch) { return $dispatch->run(); }); } ```  解析 url 获取控制器和方法 ``` // 解析 url vendor\topthink\framework\src\think\route\dispatch\Url.php protected function parseUrl(string $url): array { $depr = $this->rule->config('pathinfo_depr'); $bind = $this->rule->getRouter()->getDomainBind(); if ($bind && preg_match('/^[a-z]/is', $bind)) { $bind = str_replace('/', $depr, $bind); // 如果有域名绑定 $url = $bind . ('.' != substr($bind, -1) ? $depr : '') . ltrim($url, $depr); } $path = $this->rule->parseUrlPath($url); if (empty($path)) { return [null, null]; } // 解析控制器 $controller = !empty($path) ? array_shift($path) : null; if ($controller && !preg_match('/^[A-Za-z0-9][\w|\.]*$/', $controller)) { throw new HttpException(404, 'controller not exists:' . $controller); } // 解析操作 $action = !empty($path) ? array_shift($path) : null; $var = []; // 解析额外参数 if ($path) { preg_replace_callback('/(\w+)\|([^\|]+)/', function ($match) use (&$var) { $var[$match[1]] = strip_tags($match[2]); }, implode('|', $path)); } $panDomain = $this->request->panDomain(); if ($panDomain && $key = array_search('*', $var)) { // 泛域名赋值 $var[$key] = $panDomain; } // 设置当前请求的参数 $this->param = $var; // 封装路由 $route = [$controller, $action]; if ($this->hasDefinedRoute($route)) { throw new HttpException(404, 'invalid request:' . str_replace('|', $depr, $url)); } return $route; } ``` ### 1.8 执行控制器方法:      通过 url 解析之后,我们就可以拿到控制器和方法(如果是多应用,那就是应用->控制器->方法,url 的解析定义在多应用包里面),接下来就是执行方法。代码如下: ``` // 执行路由调度 vendor\topthink\framework\src\think\route\Dispatch.php public function run(): Response { 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)]); } // 这里是执行控制器方法返回的数据 $data = $this->exec(); return $this->autoResponse($data); } ``` 执行方法 ``` // 执行方法 // vendor\topthink\framework\src\think\route\dispatch\Controller.php 70行 public function exec() { try { // 实例化控制器 $instance = $this->controller($this->controller); } catch (ClassNotFoundException $e) { throw new HttpException(404, 'controller not exists:' . $e->getClass()); } // 注册控制器中间件 $this->registerControllerMiddleware($instance); return $this->app->middleware->pipeline('controller') ->send($this->request) ->then(function () use ($instance) { // 获取当前操作名 $suffix = $this->rule->config('action_suffix'); $action = $this->actionName . $suffix; if (is_callable([$instance, $action])) { $vars = $this->request->param(); try { $reflect = new ReflectionMethod($instance, $action); // 严格获取当前操作方法名 $actionName = $reflect->getName(); if ($suffix) { $actionName = substr($actionName, 0, -strlen($suffix)); } $this->request->setAction($actionName); } catch (ReflectionException $e) { $reflect = new ReflectionMethod($instance, '__call'); $vars = [$action, $vars]; $this->request->setAction($action); } } else { // 操作不存在 throw new HttpException(404, 'method not exists:' . get_class($instance) . '->' . $action . '()'); } // 通过反射执行 控制器方法 $data = $this->app->invokeReflectMethod($instance, $reflect, $vars); return $this->autoResponse($data); }); } ``` ### 1.9 响应输出: 控制器方法执行返回的内容由响应对象接管,响应对象经过一系列处理之后将内容进行输出,输出之后响应对象将会关闭请求,但此时应用并没有结束,而是做一些收尾工作,比如 session 写入、日志写入,接下来看看详细代码: 接管内容 ``` // 接管内容 根据内容创建不同的响应对象 protected function autoResponse($data): Response { if ($data instanceof Response) { $response = $data; } elseif (!is_null($data)) { // 默认自动识别响应输出类型 $type = $this->request->isJson() ? 'json' : 'html'; $response = Response::create($data, $type); } else { $data = ob_get_clean(); $content = false === $data ? '' : $data; $status = '' === $content && $this->request->isJson() ? 204 : 200; $response = Response::create($content, 'html', $status); } return $response; } ``` 输出 ``` // 输出内容 vendor\topthink\framework\src\think\Response.php 128 public function send(): void { // 处理输出数据 $data = $this->getContent(); if (!headers_sent() && !empty($this->header)) { // 发送状态码 http_response_code($this->code); // 发送头部信息 foreach ($this->header as $name => $val) { header($name . (!is_null($val) ? ':' . $val : '')); } } if ($this->cookie) { $this->cookie->save(); } $this->sendData($data); // 关闭请求 if (function_exists('fastcgi_finish_request')) { // 提高页面响应 fastcgi_finish_request(); } } ``` 收尾 ``` // 收尾 vendor\topthink\framework\src\think\Http.php 271 行 public function end(Response $response): void { $this->app->event->trigger(HttpEnd::class, $response); //执行中间件 $this->app->middleware->end($response); // 写入日志 $this->app->log->save(); } ``` 整个生命周期的介绍到这里就全部结束了,感谢您的阅读。 读完此文,如果感觉意犹未尽,看移步观看视频教程,并且可以和作者一对一交流。 地址:[https://edu.csdn.net/course/detail/28045](https://edu.csdn.net/course/detail/28045)