Service.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace common\components;
  3. use Yii;
  4. use yii\base\Component;
  5. use yii\base\InvalidConfigException;
  6. use common\traits\BaseAction;
  7. /**
  8. * Class Service
  9. * @package common\components
  10. * @author jianyan74 <751393839@qq.com>
  11. */
  12. class Service extends Component
  13. {
  14. use BaseAction;
  15. /**
  16. * 子服务
  17. *
  18. * @var
  19. */
  20. public $childService;
  21. /**
  22. * 已实例化的子服务
  23. *
  24. * @var
  25. */
  26. protected $_childService;
  27. /**
  28. * 获取 services 里面配置的子服务 childService 的实例
  29. *
  30. * @param $childServiceName
  31. * @return mixed
  32. * @throws InvalidConfigException
  33. */
  34. protected function getChildService($childServiceName)
  35. {
  36. if (!isset($this->_childService[$childServiceName])) {
  37. $childService = $this->childService;
  38. if (isset($childService[$childServiceName])) {
  39. $service = $childService[$childServiceName];
  40. $this->_childService[$childServiceName] = Yii::createObject($service);
  41. } else {
  42. throw new InvalidConfigException('Child Service [' . $childServiceName . '] is not find in ' . get_called_class() . ', you must config it! ');
  43. }
  44. }
  45. return $this->_childService[$childServiceName] ?? null;
  46. }
  47. /**
  48. * @param string $attr
  49. * @return mixed
  50. * @throws InvalidConfigException
  51. */
  52. public function __get($attr)
  53. {
  54. return $this->getChildService($attr);
  55. }
  56. }
粤ICP备19079148号