容器类调用make方法时,如果没有已注册的key,那么会自动通过反射类实例化具体类

make

  1. /**
  2. * Resolve the given type from the container.
  3. *
  4. * @param string $abstract
  5. * @param array $parameters
  6. * @return mixed
  7. */
  8. public function make($abstract, array $parameters = [])
  9. {
  10. $abstract = $this->getAlias($abstract);
  11.  
  12. // If an instance of the type is currently being managed as a singleton we'll
  13. // just return an existing instance instead of instantiating new instances
  14. // so the developer can keep using the same objects instance every time.
  15. if (isset($this->instances[$abstract])) {
  16. return $this->instances[$abstract];
  17. }
  18.  
  19. $concrete = $this->getConcrete($abstract);
  20.  
  21. // We're ready to instantiate an instance of the concrete type registered for
  22. // the binding. This will instantiate the types, as well as resolve any of
  23. // its "nested" dependencies recursively until all have gotten resolved.
  24. if ($this->isBuildable($concrete, $abstract)) {
  25. $object = $this->build($concrete, $parameters);
  26. } else {
  27. $object = $this->make($concrete, $parameters);
  28. }
  29.  
  30. // If we defined any extenders for this type, we'll need to spin through them
  31. // and apply them to the object being built. This allows for the extension
  32. // of services, such as changing configuration or decorating the object.
  33. foreach ($this->getExtenders($abstract) as $extender) {
  34. $object = $extender($object, $this);
  35. }
  36.  
  37. // If the requested type is registered as a singleton we'll want to cache off
  38. // the instances in "memory" so we can return it later without creating an
  39. // entirely new instance of an object on each subsequent request for it.
  40. if ($this->isShared($abstract)) {
  41. $this->instances[$abstract] = $object;
  42. }
  43.  
  44. $this->fireResolvingCallbacks($abstract, $object);
  45.  
  46. $this->resolved[$abstract] = true;
  47.  
  48. return $object;
  49. }

getConcrete

  1. /**
  2. * Get the concrete type for a given abstract.
  3. *
  4. * @param string $abstract
  5. * @return mixed $concrete
  6. */
  7. protected function getConcrete($abstract)
  8. {
  9. if (! is_null($concrete = $this->getContextualConcrete($abstract))) {
  10. return $concrete;
  11. }
  12.  
  13. // If we don't have a registered resolver or concrete for the type, we'll just
  14. // assume each type is a concrete name and will attempt to resolve it as is
  15. // since the container should be able to resolve concretes automatically.
  16. if (! isset($this->bindings[$abstract])) {
  17. if ($this->missingLeadingSlash($abstract) &&
  18. isset($this->bindings['\\'.$abstract])) {
  19. $abstract = '\\'.$abstract;
  20. }
  21.  
  22. return $abstract;
  23. }
  24.  
  25. return $this->bindings[$abstract]['concrete'];
  26. }

build

  1. /**
  2. * Instantiate a concrete instance of the given type.
  3. *
  4. * @param string $concrete
  5. * @param array $parameters
  6. * @return mixed
  7. *
  8. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  9. */
  10. public function build($concrete, array $parameters = [])
  11. {
  12. // If the concrete type is actually a Closure, we will just execute it and
  13. // hand back the results of the functions, which allows functions to be
  14. // used as resolvers for more fine-tuned resolution of these objects.
  15. if ($concrete instanceof Closure) {
  16. return $concrete($this, $parameters);
  17. }
  18.  
  19. $reflector = new ReflectionClass($concrete);
  20.  
  21. // If the type is not instantiable, the developer is attempting to resolve
  22. // an abstract type such as an Interface of Abstract Class and there is
  23. // no binding registered for the abstractions so we need to bail out.
  24. if (! $reflector->isInstantiable()) {
  25. $message = "Target [$concrete] is not instantiable.";
  26.  
  27. throw new BindingResolutionContractException($message);
  28. }
  29.  
  30. $this->buildStack[] = $concrete;
  31.  
  32. $constructor = $reflector->getConstructor();
  33.  
  34. // If there are no constructors, that means there are no dependencies then
  35. // we can just resolve the instances of the objects right away, without
  36. // resolving any other types or dependencies out of these containers.
  37. if (is_null($constructor)) {
  38. array_pop($this->buildStack);
  39.  
  40. return new $concrete;
  41. }
  42.  
  43. $dependencies = $constructor->getParameters();
  44.  
  45. // Once we have all the constructor's parameters we can create each of the
  46. // dependency instances and then use the reflection instances to make a
  47. // new instance of this class, injecting the created dependencies in.
  48. $parameters = $this->keyParametersByArgument(
  49. $dependencies, $parameters
  50. );
  51.  
  52. $instances = $this->getDependencies(
  53. $dependencies, $parameters
  54. );
  55.  
  56. array_pop($this->buildStack);
  57.  
  58. return $reflector->newInstanceArgs($instances);
  59. }

laravel容器类make方法解释的更多相关文章

  1. Info.plist和pch文件的作用,UIApplication,iOS程序的启动过程,AppDelegate 方法解释,UIWindow,生命周期方法

    Info.plist常见的设置 建立一个工程后,会在Supporting files文件夹下看到一个“工程名-Info.plist”的文件,该文件对工程做一些运行期的配置,非常重要,不能删除 注:在旧 ...

  2. es6的map()方法解释

    es6的map()方法解释   map方法的作用不难理解,即“映射”,也就是原数组被“映射”成对应新数组.下面这个例子是数值项求平方: var data = [1, 2, 3, 4]; var arr ...

  3. Laravel Route Resource 方法

    新增的 resource 方法将遵从 RESTful 架构为用户资源生成路由.该方法接收两个参数,第一个参数为资源名称,第二个参数为控制器名称. Route::resource('users', 'U ...

  4. laravel 自定义常量方法

    laravel 自定义常量方法 版本5以上验证OK 常量定义方案A: step1 按路径生成constants文件app/config/constants.php step2 constants文件里 ...

  5. vue第六单元(vue的实例和组件-vue实例的相关属性和方法-解释vue的原理-创建vue的组件)

    第六单元(vue的实例和组件-vue实例的相关属性和方法-解释vue的原理-创建vue的组件) #课程目标 掌握vue实例的相关属性和方法的含义和使用 了解vue的数据响应原理 熟悉创建组件,了解全局 ...

  6. Laravel [1045] 解决方法 Access denied for user 'homestead'@'localhost'

    这几天学习Laravel框架遇到了数据库方面的问题. PDOException in Connector.php line 55:SQLSTATE[HY000] [1045] Access denie ...

  7. laravel框架少见方法详解

    1.whereDate() 方法 $q->where('created_at', '>=', date('Y-m-d').' 00:00:00')); 以前查数据时,直接用where条件来 ...

  8. laravel多种安装方法

    首先请确保环境为 PHP >= 5.5.9 OpenSSL PHP 扩展 PDO PHP 扩展 Mbstring PHP 扩展 Tokenizer PHP 扩展 方法一: 直接下载安装好的lar ...

  9. laravel中with()方法,has()方法和whereHas()方法的区别

    with() with()方法是用作"渴求式加载"的,那主要意味着,laravel将会伴随着主要模型预加载出确切的的关联关系.这就对那些如果你想加在一个模型的所有关联关系非常有帮助 ...

随机推荐

  1. 微信H5支付时用户有微信分身停留5秒后未选择哪个微信分身,也未支付就被动回调到商户支付是否完成的页面

    微信H5支付时用户有微信分身停留5秒后未选择哪个微信分身,也未支付就被动回调到商户支付是否完成的页面 微信支付中间页调起微信收银台后超过5秒 安卓H5支付设置了redirect_url后调起微信收银台 ...

  2. EditPlus编译运行java文件

    ok ---------------两张图完成

  3. 利用Linux自带的logrotate管理日志

    日常运维中,经常要对各类日志进行管理,清理,监控,尤其是因为应用bug,在1小时内就能写几十个G日志,导致磁盘爆满,系统挂掉. nohup.out,access.log,catalina.out 本文 ...

  4. 判断变量是否不为空,函数isset()、!empty()与!is_null()的比较

    转载:https://blog.csdn.net/qq_38812954/article/details/79581785 判断变量的值,尤其是判断他们是否不为空,我们有以下4种方法: if(isse ...

  5. vue 监听子组件事件及组件上使用v-model

  6. 一次vaccum导致的事故

    1. 问题出现 晚上9点,现场报系统查询慢,运维查询zabbix后发现postgres最近几天的IOWait很大 2. 追踪问题 查询数据库,发现很多SQL堵住了 原因是真正创建index,导致表锁住 ...

  7. 带有EXE参数的启动

    (一).先制作一个带启动参数的EXE文件. 步骤:            1.定义全局私有变量:private string[] s = new string[1];  //这里为了简单起见,只做一个 ...

  8. ES6学习记录(一)

    Class类 Class的静态方法 类相当于实例的原型,所有在类中定义的方法,都会被实例继承.如果在一个方法前,加上static关键字,就表示该方法不会被实例继承,而是直接通过类来调用,这就称为“静态 ...

  9. element案例大杂烩

    修改表头字体粗细? <el-table :data="list" header-row-class-name="tableHead"> 自定义即可 ...

  10. Laravel 6.0 Schedule Preventing Task Overlaps 测试

    1 目的 1.1 测试 Laravel 6.0 任务执行机制 2 意义 2.1 在日常开发中,有的任务比较复杂,在两次任务的调度周期间隔中无法完成. 2.2 为了防止重复任务的持续生成和反复调用,对服 ...