三十三、逐行阅读Yii2.0.43_Yii框架文件ServiceLocator.php

ServiceLocator类继承Component类。 

一、属性

1. $_components 组件实例数组

2. $_definitions 组件定义数组


    // 组件实例
    private $_components = [];

    //组件定义
    private $_definitions = [];

 二、方法

1. __get方法,获取$name对应的值

    // 获取$name对应的值
    public function __get($name)
    {
        if ($this->has($name)) {
            return $this->get($name);
        }

        return parent::__get($name);
    }

 2. __isset方法,检查$name是否存在

    /**
     * 检查$name是否存在
     */
    public function __isset($name)
    {
        if ($this->has($name)) {
            return true;
        }

        return parent::__isset($name);
    }

3. has方法,检查是否存在组件实例或定义

    //检查是否存在组件实例或定义
    public function has($id, $checkInstance = false)
    {
        return $checkInstance ?
            isset($this->_components[$id]) :
            isset($this->_definitions[$id]);
    }

4. get方法,根据id返回组件

    /**
     * 根据id返回对应的组件实例
     */
    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;
            }

            return $this->_components[$id] = Yii::createObject($definition);
        } elseif ($throwException) {
            throw new InvalidConfigException("Unknown component ID: $id");
        }

        return null;
    }

5. set方法,注册组件

    // 注册组件
    public function set($id, $definition)
    {
        unset($this->_components[$id]);

        if ($definition === null) {
            unset($this->_definitions[$id]);
            return;
        }

        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'])) {
                $this->_definitions[$id] = $definition;
                $this->_definitions[$id]['class'] = $definition['__class'];
                unset($this->_definitions[$id]['__class']);
            } elseif (isset($definition['class'])) {
                $this->_definitions[$id] = $definition;
            } else {
                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));
        }
    }

6. clear方法,移除组件

    // 移除指定组件
    public function clear($id)
    {
        unset(
            $this->_definitions[$id],
            $this->_components[$id]
        );
    }

7. getComponents方法,返回组件实例或定义列表

    //返回组件定义或已加载的组件实例的列表。
    public function getComponents($returnDefinitions = true)
    {
        return $returnDefinitions ?
            $this->_definitions :
            $this->_components;
    }

8. setComponents方法,注册组件

    //注册组件
    public function setComponents($components)
    {
        foreach ($components as $id => $component) {
            $this->set($id, $component);
        }
    }

总结:

阅读了2个属性和8个方法:

  • $_components 组件实例数组
  • $_definitions 组件定义数组
  • __get方法,获取$name对应的值
  • __isset方法,检查$name是否存在
  • has方法,检查是否存在组件实例或定义
  • get方法,根据id返回组件
  • set方法,注册组件
  • clear方法,移除组件
  • getComponents方法,返回组件实例或定义列表
  • setComponents方法,注册组件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值