laravel加載過程

既然我是走進(jìn)去的,就把我當(dāng)做 apache 吧把沼,我看到的第一個(gè)文件應(yīng)該是項(xiàng)目中 public 的 index.php 文件啡直。

define('LARAVEL_START', microtime(true));
解讀:在運(yùn)行時(shí)定義一個(gè)常量 LARAVEL_START 為當(dāng)前 Unix 時(shí)間戳房官。
require __DIR__.'/../vendor/autoload.php';
解讀:引入一個(gè)自動(dòng)加載器

2.1 進(jìn)入到 autoload.php 中

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit94ec56b468e10706582fb576e66d9831::getLoader();
解讀:從 autoload_real.php 中引入自加載入口 getLoader
public static function getLoader()
{
    //如果 $loader 非空袖牙,返回 $loader
    if (null !== self::$loader) {
        return self::$loader;
    }

    //spl_autoload_register:注冊(cè)指定的函數(shù) loadClassLoader 作為__autoload的實(shí)現(xiàn), throw:拋出異常肯尺,prepend:添加到隊(duì)列之首
    spl_autoload_register(array('ComposerAutoloaderInit94ec56b468e10706582fb576e66d9831', 'loadClassLoader'), true, true);
    // 創(chuàng)建 \Composer\Autoload\ClassLoader() 對(duì)象,并對(duì)象化類屬性 $loader
    self::$loader = $loader = new \Composer\Autoload\ClassLoader();
    // 注銷已注冊(cè)的指定的__autoload()函數(shù)loadClassLoader
    spl_autoload_unregister(array('ComposerAutoloaderInit94ec56b468e10706582fb576e66d9831', 'loadClassLoader'));

    // 判斷 php 版本是否大于5.6躯枢,且沒有定義 HHVM 版本(一個(gè)高性能 PHP 執(zhí)行引擎则吟,類似于 apache ),
    //且 (不存在函數(shù) zend_loader_file_encoded 或未執(zhí)行 zend_loader_file_encoded )
    $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') 
     && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
    if ($useStaticLoader) {
        // 加載 autoload_static.php 的 ComposerStaticInit94ec56b468e10706582fb576e66d9831 類锄蹂,
    //一個(gè)靜態(tài)信息文件氓仲,包含了需要自動(dòng)加載的files路徑、psr 規(guī)范信息得糜、laravel 框架類的路徑
        require_once __DIR__ . '/autoload_static.php';

        //執(zhí)行 ComposerStaticInit94ec56b468e10706582fb576e66d9831 類中的 getInitializer 靜態(tài)函數(shù)敬扛。 
        //call_user_func:運(yùn)行作為第一個(gè)參數(shù)的回調(diào)函數(shù),如果執(zhí)行失敗返回 false朝抖,避免發(fā)生錯(cuò)誤
        /**
         public static function getInitializer(ClassLoader $loader)
        {
        return \Closure::bind(function () use ($loader) {
        $loader->prefixLengthsPsr4 = ComposerStaticInit94ec56b468e10706582fb576e66d9831::$prefixLengthsPsr4;
        $loader->prefixDirsPsr4 = ComposerStaticInit94ec56b468e10706582fb576e66d9831::$prefixDirsPsr4;
        $loader->prefixesPsr0 = ComposerStaticInit94ec56b468e10706582fb576e66d9831::$prefixesPsr0;
        $loader->classMap = ComposerStaticInit94ec56b468e10706582fb576e66d9831::$classMap;

        }, null, ClassLoader::class);
        }
         */
        // getInitializer 其實(shí)就是在返回 files 路徑啥箭、psr 規(guī)范信息、laravel 框架類的路徑
        call_user_func(\Composer\Autoload\ComposerStaticInit94ec56b468e10706582fb576e66d9831::getInitializer($loader));
    } else {
        //加載 autoload_namespaces.php治宣,返回符合 prs0 自加載插件的命名空間-路徑數(shù)組
        $map = require __DIR__ . '/autoload_namespaces.php';
        foreach ($map as $namespace => $path) {
            //向 $loader 添加自加載插件
            $loader->set($namespace, $path);
        }

        //加載 autoload_psr4.php急侥,返回符合 prs4 自加載插件的命名空間-路徑數(shù)組
        $map = require __DIR__ . '/autoload_psr4.php';
        foreach ($map as $namespace => $path) {
            //向 $loader 添加自加載插件
            $loader->setPsr4($namespace, $path);
        }

        //加載 autoload_classmap.php,返回各種需自加載類的命名空間-路徑數(shù)組
        $classMap = require __DIR__ . '/autoload_classmap.php';
        if ($classMap) {
            //向 $loader 添加自加載類
            $loader->addClassMap($classMap);
        }
    }

    // 預(yù)加載 autoloader
    $loader->register(true);

    // 加載框架文件
    if ($useStaticLoader) {
        $includeFiles = Composer\Autoload\ComposerStaticInit94ec56b468e10706582fb576e66d9831::$files;
    } else {
        $includeFiles = require __DIR__ . '/autoload_files.php';
    }
    foreach ($includeFiles as $fileIdentifier => $file) {
        composerRequire94ec56b468e10706582fb576e66d9831($fileIdentifier, $file);
    }

    return $loader;
}

解讀:查看代碼注釋

$app = require_once __DIR__.'/../bootstrap/app.php';

解讀:引入 laravel 框架實(shí)例

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

解讀:?jiǎn)?dòng) laravel 應(yīng)用與 http 交互侮邀,完成 http 請(qǐng)求和響應(yīng)

/**
     * Resolve the given type from the container.
     * 映射指定類型的容器
     * (Overriding Container::make)
     *
     * @param  string  $abstract
     * @param  array  $parameters
     * @return mixed
     */
    public function make($abstract, array $parameters = [])
    {
        $abstract = $this->getAlias($abstract);

        if (isset($this->deferredServices[$abstract]) && ! isset($this->instances[$abstract])) {
            $this->loadDeferredProvider($abstract);
        }

        return parent::make($abstract, $parameters);
    }

// Illuminate\Container\Container::class
/**
     * Get the alias for an abstract if available.
     * 獲取可信賴的類組
     * @param  string  $abstract
     * @return string
     *
     * @throws \LogicException
     */
    public function getAlias($abstract)
    {
        if (! isset($this->aliases[$abstract])) {
            return $abstract;
        }

        if ($this->aliases[$abstract] === $abstract) {
            throw new LogicException("[{$abstract}] is aliased to itself.");
        }

        return $this->getAlias($this->aliases[$abstract]);
    }

    // Illuminate\Foundation\Application::class
    public function loadDeferredProvider($service)
    {
        if (! isset($this->deferredServices[$service])) {
            return;
        }

        $provider = $this->deferredServices[$service];

        // If the service provider has not already been loaded and registered we can
        // register it with the application and remove the service from this list
        // of deferred services, since it will already be loaded on subsequent.
        // 當(dāng)服務(wù)提供者即將被加載坏怪,如果服務(wù)提供者還沒有被加載和注冊(cè),
        // 我們可以在這個(gè)應(yīng)用中注冊(cè)它绊茧,并且去除
        // 這個(gè)列表中已經(jīng)緩存的服務(wù)
        if (! isset($this->loadedProviders[$provider])) {
            $this->registerDeferredProvider($provider, $service);
        }
    }

    public function registerDeferredProvider($provider, $service = null)
    {
        // Once the provider that provides the deferred service has been registered we
        // will remove it from our local list of the deferred services with related
        // providers so that this container does not try to resolve it out again.
        // 一旦服務(wù)提供者提供被注冊(cè)的緩存的服務(wù)铝宵,我們將從我們本地緩存的服務(wù)列表中去除它和
        //與它相關(guān)聯(lián)的服務(wù)提供者,如此這個(gè)容器就不會(huì)再嘗試映射它华畏。
        if ($service) {
            unset($this->deferredServices[$service]);
        }

        $this->register($instance = new $provider($this));

        if (! $this->booted) {
            $this->booting(function () use ($instance) {
                $this->bootProvider($instance);
            });
        }
    }

————————————————
原文作者:LeoYao
轉(zhuǎn)自鏈接:https://learnku.com/articles/9052/i-walk-into-laravel55-alone-1
版權(quán)聲明:著作權(quán)歸作者所有鹏秋。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)保留以上作者信息和原文鏈接唯绍。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末拼岳,一起剝皮案震驚了整個(gè)濱河市枝誊,隨后出現(xiàn)的幾起案子况芒,更是在濱河造成了極大的恐慌,老刑警劉巖叶撒,帶你破解...
    沈念sama閱讀 216,591評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绝骚,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡祠够,警方通過查閱死者的電腦和手機(jī)压汪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來古瓤,“玉大人止剖,你說我怎么就攤上這事腺阳。” “怎么了穿香?”我有些...
    開封第一講書人閱讀 162,823評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵亭引,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我皮获,道長(zhǎng)焙蚓,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,204評(píng)論 1 292
  • 正文 為了忘掉前任洒宝,我火速辦了婚禮购公,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘雁歌。我一直安慰自己宏浩,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評(píng)論 6 388
  • 文/花漫 我一把揭開白布靠瞎。 她就那樣靜靜地躺著绘闷,像睡著了一般。 火紅的嫁衣襯著肌膚如雪较坛。 梳的紋絲不亂的頭發(fā)上印蔗,一...
    開封第一講書人閱讀 51,190評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音丑勤,去河邊找鬼华嘹。 笑死,一個(gè)胖子當(dāng)著我的面吹牛法竞,可吹牛的內(nèi)容都是我干的耙厚。 我是一名探鬼主播,決...
    沈念sama閱讀 40,078評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼岔霸,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼薛躬!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起呆细,我...
    開封第一講書人閱讀 38,923評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤型宝,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后絮爷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體趴酣,經(jīng)...
    沈念sama閱讀 45,334評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評(píng)論 2 333
  • 正文 我和宋清朗相戀三年坑夯,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了岖寞。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,727評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡柜蜈,死狀恐怖仗谆,靈堂內(nèi)的尸體忽然破棺而出指巡,到底是詐尸還是另有隱情,我是刑警寧澤隶垮,帶...
    沈念sama閱讀 35,428評(píng)論 5 343
  • 正文 年R本政府宣布厌处,位于F島的核電站,受9級(jí)特大地震影響岁疼,放射性物質(zhì)發(fā)生泄漏阔涉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評(píng)論 3 326
  • 文/蒙蒙 一捷绒、第九天 我趴在偏房一處隱蔽的房頂上張望瑰排。 院中可真熱鬧,春花似錦暖侨、人聲如沸椭住。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)京郑。三九已至,卻和暖如春葫掉,著一層夾襖步出監(jiān)牢的瞬間些举,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工俭厚, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留户魏,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,734評(píng)論 2 368
  • 正文 我出身青樓挪挤,卻偏偏與公主長(zhǎng)得像叼丑,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子扛门,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評(píng)論 2 354