Laravel kernel实例化

Laravel Kernel实例化

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

实例化 Kernel

  1. 在应用进行实例化时,已经初始化了很多的基础操作,所以下面的构造方法将会直接使用服务容器的依赖注入来解决类之间的依赖关系。

    // \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)
    上面两个一样,没有区别
    

    注意:以上所列出的依赖关系,都直接委托给服务容器进行自动处理了,不需要怕怕

  2. 对 $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对象,此为核心对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值