laravel容器类make方法解释
容器类调用make方法时,如果没有已注册的key,那么会自动通过反射类实例化具体类
make
- /**
- * Resolve the given type from the container.
- *
- * @param string $abstract
- * @param array $parameters
- * @return mixed
- */
- public function make($abstract, array $parameters = [])
- {
- $abstract = $this->getAlias($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.
- if (isset($this->instances[$abstract])) {
- return $this->instances[$abstract];
- }
- $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.
- if ($this->isBuildable($concrete, $abstract)) {
- $object = $this->build($concrete, $parameters);
- } else {
- $object = $this->make($concrete, $parameters);
- }
- // 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.
- if ($this->isShared($abstract)) {
- $this->instances[$abstract] = $object;
- }
- $this->fireResolvingCallbacks($abstract, $object);
- $this->resolved[$abstract] = true;
- return $object;
- }
getConcrete
- /**
- * Get the concrete type for a given abstract.
- *
- * @param string $abstract
- * @return mixed $concrete
- */
- protected function getConcrete($abstract)
- {
- if (! is_null($concrete = $this->getContextualConcrete($abstract))) {
- return $concrete;
- }
- // If we don't have a registered resolver or concrete for the type, we'll just
- // assume each type is a concrete name and will attempt to resolve it as is
- // since the container should be able to resolve concretes automatically.
- if (! isset($this->bindings[$abstract])) {
- if ($this->missingLeadingSlash($abstract) &&
- isset($this->bindings['\\'.$abstract])) {
- $abstract = '\\'.$abstract;
- }
- return $abstract;
- }
- return $this->bindings[$abstract]['concrete'];
- }
build
- /**
- * Instantiate a concrete instance of the given type.
- *
- * @param string $concrete
- * @param array $parameters
- * @return mixed
- *
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function build($concrete, array $parameters = [])
- {
- // If the concrete type is actually a Closure, we will just execute it and
- // hand back the results of the functions, which allows functions to be
- // used as resolvers for more fine-tuned resolution of these objects.
- if ($concrete instanceof Closure) {
- return $concrete($this, $parameters);
- }
- $reflector = new ReflectionClass($concrete);
- // If the type is not instantiable, the developer is attempting to resolve
- // an abstract type such as an Interface of Abstract Class and there is
- // no binding registered for the abstractions so we need to bail out.
- if (! $reflector->isInstantiable()) {
- $message = "Target [$concrete] is not instantiable.";
- throw new BindingResolutionContractException($message);
- }
- $this->buildStack[] = $concrete;
- $constructor = $reflector->getConstructor();
- // If there are no constructors, that means there are no dependencies then
- // we can just resolve the instances of the objects right away, without
- // resolving any other types or dependencies out of these containers.
- if (is_null($constructor)) {
- array_pop($this->buildStack);
- return new $concrete;
- }
- $dependencies = $constructor->getParameters();
- // Once we have all the constructor's parameters we can create each of the
- // dependency instances and then use the reflection instances to make a
- // new instance of this class, injecting the created dependencies in.
- $parameters = $this->keyParametersByArgument(
- $dependencies, $parameters
- );
- $instances = $this->getDependencies(
- $dependencies, $parameters
- );
- array_pop($this->buildStack);
- return $reflector->newInstanceArgs($instances);
- }
laravel容器类make方法解释的更多相关文章
- Info.plist和pch文件的作用,UIApplication,iOS程序的启动过程,AppDelegate 方法解释,UIWindow,生命周期方法
Info.plist常见的设置 建立一个工程后,会在Supporting files文件夹下看到一个“工程名-Info.plist”的文件,该文件对工程做一些运行期的配置,非常重要,不能删除 注:在旧 ...
- es6的map()方法解释
es6的map()方法解释 map方法的作用不难理解,即“映射”,也就是原数组被“映射”成对应新数组.下面这个例子是数值项求平方: var data = [1, 2, 3, 4]; var arr ...
- Laravel Route Resource 方法
新增的 resource 方法将遵从 RESTful 架构为用户资源生成路由.该方法接收两个参数,第一个参数为资源名称,第二个参数为控制器名称. Route::resource('users', 'U ...
- laravel 自定义常量方法
laravel 自定义常量方法 版本5以上验证OK 常量定义方案A: step1 按路径生成constants文件app/config/constants.php step2 constants文件里 ...
- vue第六单元(vue的实例和组件-vue实例的相关属性和方法-解释vue的原理-创建vue的组件)
第六单元(vue的实例和组件-vue实例的相关属性和方法-解释vue的原理-创建vue的组件) #课程目标 掌握vue实例的相关属性和方法的含义和使用 了解vue的数据响应原理 熟悉创建组件,了解全局 ...
- Laravel [1045] 解决方法 Access denied for user 'homestead'@'localhost'
这几天学习Laravel框架遇到了数据库方面的问题. PDOException in Connector.php line 55:SQLSTATE[HY000] [1045] Access denie ...
- laravel框架少见方法详解
1.whereDate() 方法 $q->where('created_at', '>=', date('Y-m-d').' 00:00:00')); 以前查数据时,直接用where条件来 ...
- laravel多种安装方法
首先请确保环境为 PHP >= 5.5.9 OpenSSL PHP 扩展 PDO PHP 扩展 Mbstring PHP 扩展 Tokenizer PHP 扩展 方法一: 直接下载安装好的lar ...
- laravel中with()方法,has()方法和whereHas()方法的区别
with() with()方法是用作"渴求式加载"的,那主要意味着,laravel将会伴随着主要模型预加载出确切的的关联关系.这就对那些如果你想加在一个模型的所有关联关系非常有帮助 ...
随机推荐
- 微信H5支付时用户有微信分身停留5秒后未选择哪个微信分身,也未支付就被动回调到商户支付是否完成的页面
微信H5支付时用户有微信分身停留5秒后未选择哪个微信分身,也未支付就被动回调到商户支付是否完成的页面 微信支付中间页调起微信收银台后超过5秒 安卓H5支付设置了redirect_url后调起微信收银台 ...
- EditPlus编译运行java文件
ok ---------------两张图完成
- 利用Linux自带的logrotate管理日志
日常运维中,经常要对各类日志进行管理,清理,监控,尤其是因为应用bug,在1小时内就能写几十个G日志,导致磁盘爆满,系统挂掉. nohup.out,access.log,catalina.out 本文 ...
- 判断变量是否不为空,函数isset()、!empty()与!is_null()的比较
转载:https://blog.csdn.net/qq_38812954/article/details/79581785 判断变量的值,尤其是判断他们是否不为空,我们有以下4种方法: if(isse ...
- vue 监听子组件事件及组件上使用v-model
- 一次vaccum导致的事故
1. 问题出现 晚上9点,现场报系统查询慢,运维查询zabbix后发现postgres最近几天的IOWait很大 2. 追踪问题 查询数据库,发现很多SQL堵住了 原因是真正创建index,导致表锁住 ...
- 带有EXE参数的启动
(一).先制作一个带启动参数的EXE文件. 步骤: 1.定义全局私有变量:private string[] s = new string[1]; //这里为了简单起见,只做一个 ...
- ES6学习记录(一)
Class类 Class的静态方法 类相当于实例的原型,所有在类中定义的方法,都会被实例继承.如果在一个方法前,加上static关键字,就表示该方法不会被实例继承,而是直接通过类来调用,这就称为“静态 ...
- element案例大杂烩
修改表头字体粗细? <el-table :data="list" header-row-class-name="tableHead"> 自定义即可 ...
- Laravel 6.0 Schedule Preventing Task Overlaps 测试
1 目的 1.1 测试 Laravel 6.0 任务执行机制 2 意义 2.1 在日常开发中,有的任务比较复杂,在两次任务的调度周期间隔中无法完成. 2.2 为了防止重复任务的持续生成和反复调用,对服 ...