生活随笔
收集整理的這篇文章主要介紹了
TP5源码分析-执行应用【initialize方法分析】
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
回顧
- 上一節(jié)我們分析了 執(zhí)行應(yīng)用的圖解,也就是Container::get(‘a(chǎn)pp’)->run() 這一部分,這一節(jié)我們要分析的是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');}$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); $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') {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'));}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');
這個過程我稍微分析一下 App這個類繼承Container ,當調(diào)用env 的時候就會去Container里面找,但是Container 里面也沒有這個方法,那么就會調(diào)用__get()魔術(shù)方法 這個魔術(shù)方法就會去調(diào)用 make方法 然后就會返回Env.php實例最后調(diào)用load()
$this->config->set(include $this->thinkPath . 'convention.php');
tp5里面的配置文件 分為了:慣例配置->應(yīng)用配置->模塊配置->動態(tài)配置
慣例配置:核心框架內(nèi)置的配置文件,無需更改。
應(yīng)用配置:每個應(yīng)用的全局配置文件(框架安裝后會生成初始的應(yīng)用配置文件),有部分配置參數(shù)僅能在應(yīng)用配置文件中設(shè)置。
模塊配置:每個模塊的配置文件(相同的配置參數(shù)會覆蓋應(yīng)用配置),有部分配置參數(shù)模塊配置是無效的,因為已經(jīng)使用過。
動態(tài)配置:主要是指在控制器或者行為中進行(動態(tài))更改配置,該配置方式只在當次請求有效,因為不會保存到配置文件中。
注意:其他配置將會在$this->init()里面詳細介紹下一節(jié)干吧!!!
$this->suffix = $this->config('app.class_suffix');
應(yīng)用配置文件app.php中設(shè)置:
‘class_suffix’ => true,// 開啟應(yīng)用類庫后綴
app\index\model\User類定義就要改成
<?php
namespace app\index\model;
use think\Model;
class UserModel extends Model
{
}
并且文件名也要改為UserModel.php。
- 注冊類庫別名 配置文件中的 也可以把別名寫在配置文件中
Loader
::addClassAlias($this->config->pull('alias'));
類似這樣
[‘a(chǎn)lias’=>
[ ‘App’ => facade\App::class,
‘Build’ => facade\Build::class,
‘Cache’ => facade\Cache::class,
‘Config’ => facade\Config::class,
‘Cookie’ => facade\Cookie::class,
] ]
$this->routeInit();
總結(jié)
以上是生活随笔為你收集整理的TP5源码分析-执行应用【initialize方法分析】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。