日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/18 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TP5源码分析-执行应用【initialize方法分析】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

          • 回顧
          • 分析

回顧
  • 上一節我們分析了 執行應用的圖解,也就是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') {//重新申請一塊比較大的bufferif (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();

總結

以上是生活随笔為你收集整理的TP5源码分析-执行应用【initialize方法分析】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。