$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
在應用進行實例化時,已經初始化了不少的基礎操做,因此下面的構造方法將會直接使用服務容器的依賴注入來解決類之間的依賴關係。數組
// \Illuminate\Contracts\Http\Kernel 類構造器依賴 \Illuminate\Contracts\Foundation\Application 和 \Illuminate\Routing\Router,將會經過服務容器來處理依賴關係 public function __construct(Application $app, Router $router) { $this->app = $app; // 主要委託 $router 來處理 $this->router = $router; // 如下均爲中間件的設置 $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 時經過別名方式直接調用 $this->instances['app'] \Illuminate\Routing\Router 的處理: make 時經過別名方式直接調用 $this->bindings['router'] 數組裏面 concrete 對應的匿名函數 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 時經過別名方式直接調用 $this->bindings['events'] 數組裏面 concrete 對應的匿名函數 Dispatcher 依賴 \Illuminate\Contracts\Container\Container public function __construct(ContainerContract $container = null) { $this->container = $container ?: new Container; } \Illuminate\Container\Container 的處理: make 時直接調用 $this->instances['Illuminate\Container\Container'] = Object(app) \Illuminate\Contracts\Container\Container 的處理: make 時調用別名直接調用 $this->instances['app'] = Object(app) 上面兩個同樣,沒有區別
注意:以上所列出的依賴關係,都直接委託給服務容器進行自動處理了,不須要怕怕app
對 $this->bindings['router'] 和 $this->bindings['events'] 綁定事件的處理,make 時將會直接調用數組鍵 concrete 對應的匿名函數。函數
make 時使用到的代碼片斷this
############################################## 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 對象,供下次直接調用。spa
$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 對象,供下次直接調用。code
注意:
kernel對象是融合了應用和路由的對象,路由又注入了IlluminateEventsDispatcher對象,此爲核心對象。router