by Liigo. 20151105.
安裝,和建立項目,都是通過Composer,簡單,略過。 Entry && Kernel
網站入口檔案,${Laravel-project}/public/index.php:
$app = require_once __DIR__.'/../bootstrap/app.php';$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);$response = $kernel->handle( $request = Illuminate\Http\Request::capture());$response->send();$kernel->terminate($request, $response);
產生Request,處理Request(Http\Kernel::handle()),產生Response,發送Resonse。常規的Web處理流程。
注意 Illuminate\Contracts\Http\Kernel 只是一個Interface:
interface Kernel{ public function bootstrap(); public function handle($request); public function terminate($request, $response); public function getApplication();}
可見入口檔案中的代碼,$kernel = $app->make是關鍵,後面都是調用Kerenl介面的執行個體對象方法(特別是Kernel::handle())。
可是 $app 是誰建立的呢。是什麼類型呢。 Bootstrap && Application
入口檔案的第一行代碼:
$app = require_once __DIR__.'/../bootstrap/app.php';
引導我們去查看 bootstrap/app.php 源碼,代碼不多,都拿過來看看吧:
$app = new Illuminate\Foundation\Application( realpath(__DIR__.'/../'));$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 $app;
第一行建立,最後一行返回。現在我們知道啦,$app是Illuminate\Foundation\Application類型的對象。(因為require_once,$app是一個單一實例對象。中間幾行代碼後面可能有用,此處暫時忽略。)
自然,$kernel = $app->make()也就是調用Applicaton::make()了,代碼拿來看一下:
/** * Resolve the given type from the container. * (Overriding Container::make) * @return mixed */public function make($abstract, array $parameters = []){ $abstract = $this->getAlias($abstract); if (isset($this->deferredServices[$abstract])) { $this->loadDeferredProvider($abstract); } return parent::make($abstract, $parameters);}
然而還是不清楚它具體返回哪個類型。
對照前面bootstrap的代碼:
$app->singleton( Illuminate\Contracts\Http\Kernel::class, App\Http\Kernel::class);
我們推論得出:$kernel = $app->make()的真實類型是App\Http\Kernel(同時實現了介面Illuminate\Contracts\Http\Kernel)。
這個推論很容易被證實,此處省略細節($app->singleton->bind->alias->make())。
更加具體的建立Kernel對象的細節十分瑣碎,我們也一併忽略,有興趣的可以去看父類方法Illuminate\Container\Container::make()/build()的代碼。
現在我們初步總結一下:先new出Application類型對象 app, app,app建立Kernel類型對象 kernel, kernel,kernel處理Request、產生Response並發送給用戶端。
附加:$app是被直接new出來的(new Illuminate\Foundation\Application),其建構函式做了一些重要的初始化工作,整理代碼如下:
public function __construct($basePath = null){ $this->instance('app', $this); $this->instance('Illuminate\Container\Container', $this); $this->register(new EventServiceProvider($this)); $this->register(new RoutingServiceProvider($this)); // ... $this->setBasePath($basePath);}Kernel::handle() && Request=>Response
下一步我想知道 $response = $kernel->handle($request) 內部具體做了什麼工作,是怎麼處理Request並產生Response的。
前面我們已經分析過了,$kernel的真實類型是App\Http\Kernel,也實現了介面Illuminate\Contracts\Http\Kernel。
拿來App\Http\Kernel::handle()的原始碼看看,咦,沒有此方法。
看其父類同名方法Illuminate\Foundation\Http\Kernel::handle()代碼:
public function handle($request){ try { $request->enableHttpMethodParameterOverride(); $response = $this->sendRequestThroughRouter($request); } catch (...) { // ... } $this->app['events']->fire('kernel.handled', [$request, $response]); return $response;}
上面代碼中我感興趣的只有sendRequestThroughRouter($request)這個調用,進去看一下:
/** * Send the given request through the middleware / router. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */protected function sendRequestThroughRouter($request){ $this->app->instance('request', $request); Facade::clearResolvedInstance('request'); $this->bootstrap(); //! Note: $kernel->bootstrap(); return (new Pipeline($this->app)) ->send($request) ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware) ->then($this->dispatchToRouter());}
上面代碼中最後一行是我們關注的重點,它把Request送進一個新建立的流水線(Pipeline),
供各個中介軟體(Middleware)處理,然後再派發給路由器(Router)。下文將展開分析。 Pipeline && Middleware && Router
流水線,中介軟體,路由器。 Pipleline
流水線Illuminate\Pipeline\Pipeline實現了介面Illuminate\Contracts\Pipeline\Pipeline。
其主要介面方法有send,through,via,then。其中send設定Request對象,through設定中介軟體數組,via設定方法名(預設為”handle”),then最終運行此並執行閉包參數(then的代碼極其複雜,各種閉包嵌套,把我搞糊塗了,有興趣的朋友可以看一下)。
簡單推斷來說,其工作內容是:依次調用各中介軟體的handle方法。特別的,如果某個中介軟體是閉包,以閉包的形式調用之。 Middleware
中介軟體Illuminate\Contracts\Routing\Middleware是一個很簡單的介面:
interface Middleware{ /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next);}
其文檔也極其簡陋,看不出太多有價值的資訊。第二個參數什麼意思,傳回值什麼意思,鬼才能看出來。可能需要從其他地方入手研究中介軟體。 Router
將請求派發給Router的調用流程:$kernel->handle($request) => $kernel->sendRequestThroughRouter => $kernel->dispatchToRouter() => $kernel->router->dispatch($request)。
其中$kernel->router是建立$kernel時通過建構函式傳入的Router對象。
有必要先看一下Router是怎樣建立出來的。調用流程:$app = new Applicaton(__construct) => $app->register(new RoutingServiceProvider($app)) => RoutingServiceProvider->register()->registerRouter()。
protected function registerRouter(){ $this->app['router'] = $this->app->share(function ($app) { return new Router($app['events'], $app); });}Unresolved
流水線怎麼調用中介軟體,怎麼派發給路由器,路由器又是怎麼工作的呢。這中間有很多細節還沒搞明白。
流水線那裡,代碼很繞,暫時無法理解。中介軟體那裡,文檔太簡陋,暫時無法理解。路由器運行原理那裡,暫時還沒有去看代碼。
目前就是這個樣子,此文到此為止吧。我想我需要去看一下Laravel 5.1的基礎文檔,然後再回頭去讀源碼,可能效果會更好。 補記 Bootstrap
我之前在分析Kernel::handle()時,忽略了一個地方,Kernel::sendRequestThroughRouter()內部調用了Kernel::bootstrap()方法:
/** * Bootstrap the application for HTTP requests. * * @return void */public function bootstrap(){ if (! $this->app->hasBeenBootstrapped()) { $this->app->bootstrapWith($this->bootstrappers()); }}
Kernel::bootstrap()內部又調用了Applicaton::bootstrapWith():
/** * Run the given array of bootstrap classes. * * @param array $bootstrappers * @return void */public function bootstrapWith(array $bootstrappers){ $this->hasBeenBootstrapped = true; foreach ($bootstrappers as $bootstrapper) { $this['events']->fire('bootstrapping: '.$bootstrapper, [$this]); $this->make($bootstrapper)->bootstrap($this); $this['events']->fire('bootstrapped: '.$bootstrapper, [$this]); }}
Applicaton::bootstrapWith()的參數是Kernel::bootstrappers(),其初始值為:
/** * The bootstrap classes for the application. * * @var array */protected $bootstrappers = [ 'Illuminate\Foundation\Bootstrap\DetectEnvironment', 'Illuminate\Foundation\Bootstrap\LoadConfiguration', 'Illuminate\Foundation\Bootstrap\ConfigureLogging', 'Illuminate\Foundation\Bootstrap\HandleExceptions', 'Illuminate\Foundation\Bootstrap\RegisterFacades', 'Illuminate\Foundation\Bootstrap\RegisterProviders', 'Illuminate\Foundation\Bootstrap\BootProviders',];
以其中RegisterProviders為例,其bootstrap()方法調用了$app->registerConfiguredProviders():
public function registerConfiguredProviders(){ $manifestPath = $this->getCachedServicesPath(); (new ProviderRepository($this, new Filesystem, $manifestPath)) ->load($this->config['app.providers']);}
其中$this->config['app.providers']的值來自於檔案config/app.php:
'providers' => [ /* * Laravel Framework Service Providers... */ Illuminate\Foundation\Providers\ArtisanServiceProvider::class, Illuminate\Auth\AuthServiceProvider::class, Illuminate\Broadcasting\BroadcastServiceProvider::class, Illuminate\Bus\BusServiceProvider::class, Illuminate\Cache\CacheServiceProvider::class, Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class, Illuminate\Routing\ControllerServiceProvider::class, Illuminate\Cookie\CookieServiceProvider::class, Illuminate\Database\DatabaseServiceProvider::class, Illuminate\Encryption\EncryptionServiceProvider::class, Illuminate\Filesystem\FilesystemServiceProvider::class, Illuminate\Foundation\Providers\FoundationServiceProvider::class, Illuminate\Hashing\HashServiceProvider::class, Illuminate\Mail\MailServiceProvider::class, Illuminate\Pagination\PaginationServiceProvider::class, Illuminate\Pipeline\PipelineServiceProvider::class, Illuminate\Queue\QueueServiceProvider::class, Illuminate\Redis\RedisServiceProvider::class, Illuminate\Auth\Passwords\PasswordResetServiceProvider::class, Illuminate\Session\SessionServiceProvider::class, Illuminate\Translation\TranslationServiceProvider::class, Illuminate\Validation\ValidationServiceProvider::class, Illuminate\View\ViewServiceProvider::class, /* * Application Service Providers... */ App\Providers\AppServiceProvider::class, App\Providers\AuthServiceProvider::class, App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class,],
大家都看到了,Kernel和Application互相交叉調用,Bootstrap過程又穿插在Request處理過程中間。暫時看不出清晰的思路。 References
Laravel不是一個小項目,邏輯複雜,劃分模組之後,布局分散。你很難在短時間內僅僅通過瀏覽原始碼理清架構主體設計思路,尤其是在本人對Laravel還十分陌生的情況下。適可而止是理智的選擇。
還是先看一下基礎性的文檔吧: http://laravel.com/docs/5.1/lifecycle http://laravel.com/docs/5.1/providers http://laravel.com/docs/5.1/container http://laravel.com/docs/5.1/middleware http://laravel.com/docs/5.1/requests http://laravel.com/docs/5.1/responses http://laravel.com/docs/5.1/routing