• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

yii2 随笔(七)依赖注入——(4)服务定位器

php 搞代码 3年前 (2022-01-22) 14次浏览 已收录 0个评论

服务定位器定位器是依赖注入的一种解决方式,它包含依赖注入,在解决了依赖注入后,如果服务使用者和服务提供者不是用一个人,那么使用者要了解提供服务的必须参数,这样才能保证依赖的正确性,这就耦合了使用者和提供者,服务定位器就是解耦这部分的,服务提供者在 ServiceLocator 中注册服务(同时注册了依赖),仅仅告诉服务使用者那些服务的名称或者别名,那么对于服务提供者和使用者都是好的,使用者只需要知道提供者提供的什么服务,而不必知道依赖什么,服务提供者也不必为使用者“胡乱使用”服务而导致的bug所困扰。

那么yii2是怎么使用ServiceLocator呢?其实很简单如下

//魔术方法,public function __get($name){    //得到某个注册的方法    if ($this->has($name)) {        return $this->get($name);    } else {        return parent::__get($name);    }}//魔术方法查看某个服务是否存在,源码<mark style="color:transparent">本文来源gaodaimacom搞#^代%!码&网*</mark><pre>搞代gaodaima码

略public function __isset($name){}//__isset()中调用,查看某个服务是否存在,源码略public function has($id, $checkInstance = false){}//得到某个服务public function get($id, $throwException = true){ if (isset($this->_components[$id])) {//如果是已经处理的服务,就直接返回 return $this->_components[$id]; } if (isset($this->_definitions[$id])) {//如定义了该服务 $definition = $this->_definitions[$id];//得到服务的定义 //如果服务是一个闭包,则把闭包注册到已经实例化的服务中,并且返回闭包 if (is_object($definition) && !$definition instanceof Closure) { return $this->_components[$id] = $definition; } else {//其他的情况下通过依赖注入生成对象,并且注册为已处理,返回对象 return $this->_components[$id] = Yii::createObject($definition); } } elseif ($throwException) {//如果抛出异常,则抛出异常 throw new InvalidConfigException("Unknown component ID: $id"); } else {//其他返回null return null; }}//注册一个服务public function set($id, $definition){ if ($definition === null) {//如果该服务的定义为null,则删除已经实例化的服务,返回空,用于注销已经实例化的并且保存过的服务的定义 unset($this->_components[$id], $this->_definitions[$id]); return; } //清空已经实例化的服务 unset($this->_components[$id]); //如果该服务的定义为一个对象,并且是一个可调用的结构 if (is_object($definition) || is_callable($definition, true)) { // an object, a class name, or a PHP callable $this->_definitions[$id] = $definition; } elseif (is_array($definition)) {//如果该服务是一个配置数组 // a configuration array if (isset($definition['class'])) {//如果有class键值,则直接注册为一个服务的定义 $this->_definitions[$id] = $definition; } else {//是配置数组,但是没有指定class,则抛出异常 throw new InvalidConfigException("The configuration for the \"$id\" component must contain a \"class\" element."); } } else {//什么都不是,抛出异常,非法注册服务 throw new InvalidConfigException("Unexpected configuration type for the \"$id\" component: " . gettype($definition)); }}//清空已经实例化过的服务和定义,代码略public function clear($id){}//得到已经实例化后的服务,或者得到可用的服务配置public function getComponents($returnDefinitions = true){}//注册所有的服务,这里的$components,就是你在config里写的 $config['components']值public function setComponents($components){}


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:yii2 随笔(七)依赖注入——(4)服务定位器
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址