设为首页 加入收藏

TOP

laravel5.5源码笔记(一、入口应用的初始化)(三)
2019-08-23 00:34:14 】 浏览:103
Tags:laravel5.5 源码 笔记 入口 应用 初始
textualBuild
= ! empty($parameters) || ! is_null( $this->getContextualConcrete($abstract) ); // If an instance of the type is currently being managed as a singleton we'll // just return an existing instance instead of instantiating new instances // so the developer can keep using the same objects instance every time. //instances数组中有该类,并且不需要构建上下文的话,便直接返回该类实例 if (isset($this->instances[$abstract]) && ! $needsContextualBuild) { return $this->instances[$abstract]; } //将实例化类所需的参数存入数组 $this->with[] = $parameters; //获取该类闭包,若无则还是返回类名字符串 $concrete = $this->getConcrete($abstract); // We're ready to instantiate an instance of the concrete type registered for // the binding. This will instantiate the types, as well as resolve any of // its "nested" dependencies recursively until all have gotten resolved. //若当前所make的类没有上下文绑定,并且是一个闭包则直接进行构建,否则再次递归make方法获得契约所绑定类 if ($this->isBuildable($concrete, $abstract)) { $object = $this->build($concrete); } else { $object = $this->make($concrete); } // If we defined any extenders for this type, we'll need to spin through them // and apply them to the object being built. This allows for the extension // of services, such as changing configuration or decorating the object. foreach ($this->getExtenders($abstract) as $extender) { $object = $extender($object, $this); } // If the requested type is registered as a singleton we'll want to cache off // the instances in "memory" so we can return it later without creating an // entirely new instance of an object on each subsequent request for it. //若该类绑定时设置为共享,则缓存至instances单例数组 if ($this->isShared($abstract) && ! $needsContextualBuild) { $this->instances[$abstract] = $object; } $this->fireResolvingCallbacks($abstract, $object); // Before returning, we will also set the resolved flag to "true" and pop off // the parameter overrides for this build. After those two things are done // we will be ready to return back the fully constructed class instance. $this->resolved[$abstract] = true; array_pop($this->with); return $object; } View Code

还记得刚刚在app.php文件中有一个singleton函数将Illuminate\Contracts\Http\Kernel::class绑定为了App\Http\Kernel::class类吗?当build方法执行到kernel的构造函数时,跳转到其父类G:\wamp64\www\test\laravel55\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php看一看

 1     public function build($concrete)
 2     {
 3         // If the concrete type is actually a Closure, we will just execute it and
 4         // hand back the results of the functions, which allows functions to be
 5         // used as resolvers for more fine-tuned resolution of these objects.
 6         //若传入的是一个闭包则直接通过闭包实例化类,这种闭包一般由provider类在laravel应用初始化阶段通过bind方法进行绑定。
 7         if ($concrete instanceof Closure) {
 8             return $concrete($this, $this->getLastParameterOverride());
 9         }
10         //制造一个类反射
11         $reflector = new ReflectionClass($concrete);
12 
13         // If the type is not instantiable, the developer is attempting to resolve
14         // an abstract type such as an Interface of Abstract Class and there is
15         // no binding registered for the abstractions so we need to bail out.
16         if (! $reflector->isInstantiable()) {
17             return $this->notInstantiable($concrete);
18         }
19         //将当前所实例化的类存入栈
20         $this->buildStack[] = $concrete;
21         //获得该类构造方法
22         $con
首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇php 无限极分类 下一篇cgi、fastcgi及php-fpm分别是什么

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目