| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace common\components;
- use Yii;
- use yii\base\Component;
- use yii\base\InvalidConfigException;
- use common\traits\BaseAction;
- /**
- * 自定义服务组件
- * @Author bing
- * @DateTime 2021-08-11 16:43:49
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- class Service extends Component
- {
- // 子服务
- public $childService;
- // 已实例化的子服务
- protected $_childService;
-
- /**
- * 获取 services 里面配置的子服务 childService 的实例
- * @Author bing
- * @DateTime 2021-08-11 16:44:58
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param [type] $childServiceName
- * @throws InvalidConfigException
- * @return mixed
- */
- protected function getChildService($childServiceName)
- {
- if (!isset($this->_childService[$childServiceName])) {
- $childService = $this->childService;
- if (isset($childService[$childServiceName])) {
- $service = $childService[$childServiceName];
- $this->_childService[$childServiceName] = Yii::createObject($service);
- } else {
- throw new InvalidConfigException('未找到 [' . $childServiceName . '] 此子模块 ' . get_called_class() . ', 需要先进行配置! ');
- }
- }
- return $this->_childService[$childServiceName] ?? null;
- }
- /**
- * 魔术方法获取子服务
- * @Author bing
- * @DateTime 2021-08-11 16:45:57
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param [type] $attr
- * @return mixed
- * @throws InvalidConfigException
- */
- public function __get($attr)
- {
- return $this->getChildService($attr);
- }
- }
|