博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
yii2框架随笔27
阅读量:4575 次
发布时间:2019-06-08

本文共 4838 字,大约阅读时间需要 16 分钟。

getDependencies($class); // 用传入的参数去替换掉默认的参数 foreach ($params as $index => $param) { $dependencies[$index] = $param; } // 将Instance实例转化成相应的id中存储的类名的实例,即参数的真正的实例 $dependencies = $this->resolveDependencies($dependencies, $reflection); if (empty($config)) { // ReflectionClass::newInstanceArgs — 从给出的参数创建一个新的类实例 // 如果$config是空,直接创建一个新的类的实例返回 return $reflection->newInstanceArgs($dependencies); } // ReflectionClass::implementsInterface — 检查它是否实现了一个接口(interface) // config不为空 if (!empty($dependencies) && $reflection->implementsInterface('yii\base\Configurable')) { // 依赖不为空而且class实现了 yii\base\Configurable 的接口 // 实现了 yii\base\Configurable 的接口,就意味着类的构造函数的最后一个参数是$config // set $config as the last parameter (existing one will be overwritten) $dependencies[count($dependencies) - 1] = $config; return $reflection->newInstanceArgs($dependencies); } else { $object = $reflection->newInstanceArgs($dependencies); // 循环$config,将$config的键值对赋给相应的类的实例 foreach ($config as $name => $value) { $object->$name = $value; } return $object; } } /** * Merges the user-specified constructor parameters with the ones registered via [[set()]]. * 将用户指定的构造函数参数通过[set()]注册。 * @param string $class class name, interface name or alias name * @param array $params the constructor parameters * @return array the merged parameters */ protected function mergeParams($class, $params) { if (empty($this->_params[$class])) { // 如果_params中不存在,就直接返回$params return $params; } elseif (empty($params)) { // 如果_params中存在,且params为空,就返回_params中的内容 return $this->_params[$class]; } else { // 如果都不为空,就将两者合并起来,以$params为主 $ps = $this->_params[$class]; foreach ($params as $index => $value) { $ps[$index] = $value; } return $ps; } } /** * Returns the dependencies of the specified class. * 返回指定的类的依赖关系。 * @param string $class class name, interface name or alias name * @return array the dependencies of the specified class. */ protected function getDependencies($class) { if (isset($this->_reflections[$class])) { return [$this->_reflections[$class], $this->_dependencies[$class]]; } $dependencies = []; $reflection = new ReflectionClass($class); // 获取类的构造方法 $constructor = $reflection->getConstructor(); if ($constructor !== null) { // 获取构造方法的参数,并将其实例化 foreach ($constructor->getParameters() as $param) { if ($param->isDefaultValueAvailable()) { // 如果有defalut值,就直接将默认值放入依赖中 $dependencies[] = $param->getDefaultValue(); } else { // 否则,就先获取param的ReflectionClass $c = $param->getClass(); // 然后通过getName获取类的名称(带namespace),生成一个Instance的实例 // Instance中有个id属性,标记了类的名称 $dependencies[] = Instance::of($c === null ? null : $c->getName()); } } } // 将类的反射实例和参数依赖的实例分别缓存到_reflections和_dependencies数组中 $this->_reflections[$class] = $reflection; $this->_dependencies[$class] = $dependencies; return [$reflection, $dependencies]; } /** * Resolves dependencies by replacing them with the actual object instances. * 将Instance的实例替换成参数本身应该的实例 * @param array $dependencies the dependencies * @param ReflectionClass $reflection the class reflection associated with the dependencies * @return array the resolved dependencies * @throws InvalidConfigException if a dependency cannot be resolved or if a dependency cannot be fulfilled. */ protected function resolveDependencies($dependencies, $reflection = null) { foreach ($dependencies as $index => $dependency) { // 依赖是Instance的实例,才做处理 if ($dependency instanceof Instance) { if ($dependency->id !== null) { // id不为空,就使用get方法去获取它的实例 $dependencies[$index] = $this->get($dependency->id); } elseif ($reflection !== null) { // 找出缺少的参数名称,以及其所在的类名 $name = $reflection->getConstructor()->getParameters()[$index]->getName(); $class = $reflection->getName(); throw new InvalidConfigException("Missing required parameter \"$name\" when instantiating \"$class\"."); } } } return $dependencies; }}

 

转载于:https://www.cnblogs.com/taokai/p/5470333.html

你可能感兴趣的文章
SampleManager(赛默飞)
查看>>
堆排序
查看>>
我的面试问题记录
查看>>
函数PARSENAME使用和截取字符串
查看>>
关乎性能的判断,请作出果断选择
查看>>
判断是否包含指定的字符
查看>>
[Html5] HTML5 开发手机应用
查看>>
[工具] 各种主流 SQLServer 迁移到 MySQL 工具对比
查看>>
(二)Maven 基本概念——依赖、生命周期、仓库管理、聚合&继承
查看>>
py4CV例子3Mnist识别和ANN
查看>>
【4Opencv】如何识别出轮廓准确的长和宽
查看>>
现货黄金交易计划摸索
查看>>
Django中国|Django中文社区——python、django爱好者交流社区
查看>>
java中的toArray()
查看>>
java数据库之JDBC
查看>>
C语言 strcpy,memcpy,memmove,memccpy函数
查看>>
SqlSession 内部运行
查看>>
C语言一个小程序的bug疑问 数组相关[已解决]
查看>>
空指针与野指针的区别
查看>>
Ubuntu的root用户问题
查看>>