$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
#實例化Kernel
在應用實例化時,已經初始化了很多的基礎操作,所以下面的建構方法將會直接使用服務容器的依賴注入來解決類別之間的依賴關係。
// \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) 上面两个一样,没有区别
注意:以上所列出的依賴關係,都直接委託給服務容器進行自動處理了,不需要害怕
對$this-> bindings['router'] 和$this->bindings['events'] 綁定事件的處理,make 時將會直接呼叫數組鍵concrete 對應的匿名函數。
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 對象,讓下次直接調用。
$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 對象,以便下次直接呼叫。
注意:
kernel物件是融合了應用和路由的對象,路由又注入了IlluminateEventsDispatcher對象,此為核心對象。