设为首页 加入收藏

TOP

laravel5.5源码笔记(一、入口应用的初始化)(四)
2019-08-23 00:34:14 】 浏览:101
Tags:laravel5.5 源码 笔记 入口 应用 初始
structor
= $reflector->getConstructor(); 23 24 // If there are no constructors, that means there are no dependencies then 25 // we can just resolve the instances of the objects right away, without 26 // resolving any other types or dependencies out of these containers. 27 //构造函数没有参数则直接实例化 28 if (is_null($constructor)) { 29 array_pop($this->buildStack); 30 31 return new $concrete; 32 } 33 //若有构造函数则获取其参数 34 $dependencies = $constructor->getParameters(); 35 36 // Once we have all the constructor's parameters we can create each of the 37 // dependency instances and then use the reflection instances to make a 38 // new instance of this class, injecting the created dependencies in. 39 //运行构造函数,并解决依赖 40 $instances = $this->resolveDependencies( 41 $dependencies 42 ); 43 //解决完依赖,出栈 44 array_pop($this->buildStack); 45 46 return $reflector->newInstanceArgs($instances); 47 } View Code

 

    use Illuminate\Routing\Router;

    public function __construct(Application $app, Router $router)
    {
        $this->app = $app;
        //路由类实例,由容器自动加载依赖而来
        $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);
        }
    }

可以看到,laravel在实例化出kernel对象的同时,通过kernel的构造函数加载了系统中间件,依赖了application与route两个对象。并将自身的$middlewareGroups、routeMiddleware数组解析进了route对象里,在路由进行调用的时候就会把路由方法上绑定的中间件名在这里解析出实例来调用了,其中routeMiddleware为别名所用。,随后在index.php文件中马上就利用kernel的handle方法,传入了一个request对象,来处理这次的网页url请求。

    public function handle($request)
    {
        try {
            //启用http方法覆盖参数
            $request->enableHttpMethodParameterOverride();
            //通过路由发送请求
            $response = $this->sendRequestThroughRouter($request);
        } catch (Exception $e) {
            $this->reportException($e);

            $response = $this->renderException($request, $e);
        } catch (Throwable $e) {
            $this->reportException($e = new FatalThrowableError($e));

            $response = $this->renderException($request, $e);
        }

        $this->app['events']->dispatch(
            new Events\RequestHandled($request, $response)
        );

        return $response;
    }

    protected function sendRequestThroughRouter($request)
    {
        //将请求存入容器
        $this->app->instance('request', $request);
        //清除facade门面
        Facade::clearResolvedInstance('request');
        //初始化引导
        $this->bootstrap();
        //让请求进入中间件
        return (new Pipeline($this->app))
                    ->send($request)
                    ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
                    ->then($this->dispatchToRouter());
    }
    //引导数组
    protected $bootstrappers = [
        \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,
        \Illuminate\Foundation\Bootstrap\LoadConfiguration::class,
        \Illuminate\Foundation\Bootstrap\HandleExceptions::class,
        \Illuminate\Foundation\Bootstrap\RegisterFacades::class,
        \Illuminate\Foundation\Bootstrap\RegisterProviders::class,
        \Illuminate\Foundation\Bootstrap\BootProviders::class,
    ];

上面bootstrap中会分别执行每一个bootstrapper的bootstrap方法来引导启动应用程序的各个部分
1. DetectEnvironment 检查环境
2. LoadCon

首页 上一页 1 2 3 4 5 下一页 尾页 4/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇php 无限极分类 下一篇cgi、fastcgi及php-fpm分别是什么

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目