您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Laravel中kernel實例化的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
Laravel kernel實例化
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
在應(yīng)用進(jìn)行實例化時,已經(jīng)初始化了很多的基礎(chǔ)操作,所以下面的構(gòu)造方法將會直接使用服務(wù)容器的依賴注入來解決類之間的依賴關(guān)系。
// \Illuminate\Contracts\Http\Kernel 類構(gòu)造器依賴 \Illuminate\Contracts\Foundation\Application 和 \Illuminate\Routing\Router,將會通過服務(wù)容器來處理依賴關(guān)系 public function __construct(Application $app, Router $router) { $this->app = $app; // 主要委托 $router 來處理 $this->router = $router; // 以下均為中間件的設(shè)置 $router->middlewarePriority = $this->middlewarePriority; foreach ($this->middlewareGroups as $key => $middleware) { $router->middlewareGroup($key, $middleware); } foreach ($this->routeMiddleware as $key => $middleware) { $router->aliasMiddleware($key, $middleware); } } \Illuminate\Contracts\Foundation\Application 的處理: make 時通過別名方式直接調(diào)用 $this->instances['app'] \Illuminate\Routing\Router 的處理: make 時通過別名方式直接調(diào)用 $this->bindings['router'] 數(shù)組里面 concrete 對應(yīng)的匿名函數(shù) Router 依賴 \Illuminate\Contracts\Events\Dispatcher 和 \Illuminate\Container\Container public function __construct(Dispatcher $events, Container $container = null) { $this->events = $events; $this->routes = new RouteCollection; $this->container = $container ?: new Container; } \Illuminate\Contracts\Events\Dispatcher 的處理: make 時通過別名方式直接調(diào)用 $this->bindings['events'] 數(shù)組里面 concrete 對應(yīng)的匿名函數(shù) Dispatcher 依賴 \Illuminate\Contracts\Container\Container public function __construct(ContainerContract $container = null) { $this->container = $container ?: new Container; } \Illuminate\Container\Container 的處理: make 時直接調(diào)用 $this->instances['Illuminate\Container\Container'] = Object(app) \Illuminate\Contracts\Container\Container 的處理: make 時調(diào)用別名直接調(diào)用 $this->instances['app'] = Object(app) 上面兩個一樣,沒有區(qū)別
注意:以上所列出的依賴關(guān)系,都直接委托給服務(wù)容器進(jìn)行自動處理了,不需要怕怕
對 $this->bindings['router'] 和 $this->bindings['events'] 綁定事件的處理,make 時將會直接調(diào)用數(shù)組鍵 concrete 對應(yīng)的匿名函數(shù)。
make 時使用到的代碼片段
############################################## if ($concrete instanceof Closure) { return $concrete($this, end($this->with)); } ############################################### $this->bindings['router'] = [ 'concrete' => function ($app) { return new Router($app['events'], $app); }, 'shared' => 'true', ]; $router = new Router($app['events'], $app); \Illuminate\Routing\Router public function __construct(Dispatcher $events, Container $container = null) { $this->events = $events; $this->routes = new RouteCollection; $this->container = $container ?: new Container; }
返回一個 Router 對象,同時會重置 $this->instances['router'] = $router 對象,供下次直接調(diào)用。
$this->bindings['events'] = [ 'concrete' => function ($app) { return (new Dispatcher($app))->setQueueResolver(function () use ($app) { return $app->make(QueueFactoryContract::class); }); } 'shared' => 'true', ]; $dispatcher = (new \Illuminate\Events\Dispatcher($app))->setQueueResolver(function () use ($app) { return $app->make(QueueFactoryContract::class); }); Illuminate\Events\Dispatcher: public function __construct(ContainerContract $container = null) { $this->container = $container ?: new Container; } public function setQueueResolver(callable $resolver) { $this->queueResolver = $resolver; return $this; }
返回一個 Dispatcher 對象,同時會重置 $this->instances['events'] = $dispatcher 對象,供下次直接調(diào)用。
注意:
kernel對象是融合了應(yīng)用和路由的對象,路由又注入了IlluminateEventsDispatcher對象,此為核心對象。
關(guān)于“Laravel中kernel實例化的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。