Laravel源碼入門-啓動引導過程(三)bootstrap/app.php

上篇:Laravel源碼入門-啓動引導過程(二)bootstrap/autoload.php

從請求發起開始,進入 public/index.php,到 bootstrap/autoload.php,下一步就進入了創建應用實例,代碼如下:

/*  /* 來自 public/index.php */
|
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

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

這句代碼成爲點亮(Turn on),創建了 $app,一切都有了開始,我們看看 bootstrap/app.php。

注:我本來在這裏要配置$app->useStoragePath (Storage的目錄)寫$_ENV的時候讀不到,原因php.ini中variables_order = "GPCS"全局變量中沒有加env,改成variables_order = "EGPCS" ,(Environment、Get、Post、Cookies、Server)。

<?php /* bootstrap/app.php */

/*
|--------------------------------------------------------------------------
| Create The Application 創建應用實例
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
| $app 是 Application 的實例,在面向對象編程(OOP)中實例是基本單位,原因就是面向對象
| 編程,不是面向過程編程,在我們眼中,要開發的東西都是對象,跑起來後都是一個一個的實例。
| 
| $app 這個實例,是個最開始的,也是最基本的實例,說像膠水,應該說像蜘蛛,能產絲一樣,把
| Laravel 的各個部分(其實也都是對象)連接起來,有機的連接,就能跑起來了!
|
| $app 也是個容器(Container),Application 繼承自 Container,粗淺理解,$app,
| 更像一副骨架,帶皮膚的骨架,還沒有內部各種組織,是個空的容器,當我們往裏填充時,使用的
| 是 Injection 的方法,像科幻片,注入一種生物水,那種生物就在容器內繁殖起來,並發揮作用
| ,如果非要用個好聽的名字,用《攻殼特工隊》裏面的話,就是強化,注入強化!再回顧一下上篇,
| 有一個 make() 方法,這個方法意味解析(resolve)
| 而不是簡單的 new 操作,之所以意味着解析(resolve),是因爲 $app 是一個容器,各種組件像藥水
| 一樣以 Injection 的方式注入到了 $app 體內,而在體外,比如 index.php 文件就相當於在 $app
| 體外,要獲得 $kernel,就可以用 $app->make(類的名字) 從各種有機融合在一起的藥水中來析出來,
| 這也就是 $app 是一個容器的 IoC 控制反轉 的理解方式。
| 具體參見https://laravel.com/docs/5.4/container,搜索 “make method”。
|
| 備註:realpath() 函數返回值,作爲 Application 構造函數輸入參數,這裏值爲:
| 部署目錄:/Users/me/mykaifa/laravel_orig,也就是 bootstrap 的上一級目錄。
|
*/

$app = new Illuminate\Foundation\Application(
   $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

/*
|--------------------------------------------------------------------------
| Bind Important Interfaces 綁定重要接口
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
| 這裏綁定的三個接口實在重要,一個Web端Kernel,二個是命令行端Kernel,最後一個是異常處理
| 。最基本的。用單例(singleton)來綁定,把三個協議 Contracts 純接口(無實現)綁定到
| 具體的 App\Http\、App\Console\和App\Exceptions 下的。查閱 Container::singleton()
| 實現代碼:singleton($abstract, $concrete),不知是不是 abstract 和 concrete 的關係。
|
*/

$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    App\Http\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

/*
|--------------------------------------------------------------------------
| Return The Application 腳本執行完返回 $app 到 public/index.php
|--------------------------------------------------------------------------
|
| This script returns the application instance. The instance is given to
| the calling script so we can separate the building of the instances
| from the actual running of the application and sending responses.
|
*/

return $app;

== 總結 ==

bootstrap/app.php 兩三行代碼,做了大量工作,如果用 dump($app),可以看到。總之是創建了 $app 容器實例,等待着更豐富的注入,讓應用活起來、跑起來。

下篇:Laravel源碼入門-啓動引導過程(四)app/Http/Kernel.php

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章