laravel kernel实例化
$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\containerpublic 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\containerpublic 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\routerpublic 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对象,此为核心对象。
相关推荐:最新的五个laravel视频教程
以上就是一文讲解关于laravel kernel实例化的详细内容。
