TP5源码分析-执行应用【initialize方法分析】

    技术2022-07-11  78

    文章目录

    回顾分析

    回顾
    上一节我们分析了 执行应用的图解,也就是Container::get(‘app’)->run() 这一部分,这一节我们要分析的是thinkphp\library\think\App.php 中的 initialize 方法
    分析
    上源码 其实源码上很多地方我都有注解 public function initialize() { if ($this->initialized) { //初始化 return; } $this->initialized = true; $this->beginTime = microtime(true); $this->beginMem = memory_get_usage(); $this->rootPath = dirname($this->appPath) . DIRECTORY_SEPARATOR; $this->runtimePath = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR; $this->routePath = $this->rootPath . 'route' . DIRECTORY_SEPARATOR; $this->configPath = $this->rootPath . 'config' . DIRECTORY_SEPARATOR; static::setInstance($this);//当前对象单例化 $this->instance('app', $this);//当前对象放入容器 // 加载环境变量配置文件 if (is_file($this->rootPath . '.env')) { $this->env->load($this->rootPath . '.env');//Container ----> __get()--->Env.php ---->load() } $this->configExt = $this->env->get('config_ext', '.php'); //加载惯例配置文件 $this->config->set(include $this->thinkPath . 'convention.php'); // 设置路径环境变量 $this->env->set([ 'think_path' => $this->thinkPath, 'root_path' => $this->rootPath, 'app_path' => $this->appPath, 'config_path' => $this->configPath, 'route_path' => $this->routePath, 'runtime_path' => $this->runtimePath, 'extend_path' => $this->rootPath . 'extend' . DIRECTORY_SEPARATOR, 'vendor_path' => $this->rootPath . 'vendor' . DIRECTORY_SEPARATOR, ]); $this->namespace = $this->env->get('app_namespace', $this->namespace); $this->env->set('app_namespace', $this->namespace); // 注册应用命名空间 Loader::addNamespace($this->namespace, $this->appPath); //app => D:\www\ThinkPHP_V5.1.39\application\ // 初始化应用 $this->init(); // 开启类名后缀 $this->suffix = $this->config('app.class_suffix'); // 应用调试模式 $this->appDebug = $this->env->get('app_debug', $this->config('app.app_debug')); $this->env->set('app_debug', $this->appDebug); if (!$this->appDebug) { ini_set('display_errors', 'Off'); } elseif (PHP_SAPI != 'cli') { //重新申请一块比较大的buffer if (ob_get_level() > 0) { $output = ob_get_clean(); } ob_start(); if (!empty($output)) { echo $output; } } // 注册异常处理类 if ($this->config('app.exception_handle')) { Error::setExceptionHandler($this->config('app.exception_handle')); } // 注册根命名空间 if (!empty($this->config('app.root_namespace'))) { Loader::addNamespace($this->config('app.root_namespace')); } // 加载composer autofile文件 Loader::loadComposerAutoloadFiles(); // 注册类库别名 配置文件中的 也可以把别名写在配置文件中 Loader::addClassAlias($this->config->pull('alias')); // 数据库配置初始化 Db::init($this->config->pull('database')); // 设置系统时区 date_default_timezone_set($this->config('app.default_timezone')); // 读取语言包 $this->loadLangPack(); // 路由初始化 $this->routeInit(); } 加载环境变量配置文件 $this->env->load($this->rootPath . '.env');//Container ----> __get()--->Env.php ---->load()

    这个过程我稍微分析一下 App这个类继承Container ,当调用env 的时候就会去Container里面找,但是Container 里面也没有这个方法,那么就会调用__get()魔术方法 这个魔术方法就会去调用 make方法 然后就会返回Env.php实例最后调用load()

    加载惯例配置文件 $this->config->set(include $this->thinkPath . 'convention.php');

    tp5里面的配置文件 分为了:惯例配置->应用配置->模块配置->动态配置 惯例配置:核心框架内置的配置文件,无需更改。 应用配置:每个应用的全局配置文件(框架安装后会生成初始的应用配置文件),有部分配置参数仅能在应用配置文件中设置。 模块配置:每个模块的配置文件(相同的配置参数会覆盖应用配置),有部分配置参数模块配置是无效的,因为已经使用过。 动态配置:主要是指在控制器或者行为中进行(动态)更改配置,该配置方式只在当次请求有效,因为不会保存到配置文件中。

    注意:其他配置将会在$this->init()里面详细介绍下一节干吧!!!

    开启类名后缀 $this->suffix = $this->config('app.class_suffix');

    应用配置文件app.php中设置: ‘class_suffix’ => true,// 开启应用类库后缀 app\index\model\User类定义就要改成 <?php namespace app\index\model; use think\Model; class UserModel extends Model { } 并且文件名也要改为UserModel.php。

    注册类库别名 配置文件中的 也可以把别名写在配置文件中 Loader::addClassAlias($this->config->pull('alias'));

    类似这样 [‘alias’=> [ ‘App’ => facade\App::class, ‘Build’ => facade\Build::class, ‘Cache’ => facade\Cache::class, ‘Config’ => facade\Config::class, ‘Cookie’ => facade\Cookie::class, ] ]

    路由初始化【下次重点介绍对象】 $this->routeInit();
    Processed: 0.018, SQL: 9