Application.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services;
  10. use Yii;
  11. use yii\base\InvalidConfigException;
  12. /**
  13. * 此对象就是Yii::$service,通过魔术方法__get , 得到服务对象,服务对象是单例模式。
  14. * @see http://www.fecshop.com/doc/fecshop-guide/develop/cn-1.0/guide-fecshop-service-abc.html
  15. *
  16. * @property \fecshop\services\Admin $admin admin service
  17. * @property \fecshop\services\AdminUser $adminUser adminUser service
  18. * @property \fecshop\services\Cache $cache cache service
  19. * @property \fecshop\services\Cart $cart cart service
  20. * @property \fecshop\services\Category $category category service
  21. * @property \fecshop\services\Cms $cms cms service
  22. * @property \fecshop\services\Coupon $coupon coupon service
  23. * @property \fecshop\services\Customer $customer customer service
  24. * @property \fecshop\services\Email $email email service
  25. * @property \fecshop\services\Event $event event service
  26. * @property \fecshop\services\Fecshoplang $fecshopLang fecshopLang service
  27. * @property \fecshop\services\Helper $helper helper service
  28. * @property \fecshop\services\Image $image image service
  29. * @property \fecshop\services\Order $order order service
  30. * @property \fecshop\services\Page $page page service
  31. * @property \fecshop\services\Payment $payment payment service
  32. * @property \fecshop\services\Point $point point service
  33. * @property \fecshop\services\Product $product product service
  34. * @property \fecshop\services\Request $request request service
  35. * @property \fecshop\services\Search $search search service
  36. * @property \fecshop\services\Session $session session service
  37. * @property \fecshop\services\Shipping $shipping shipping service
  38. * @property \fecshop\services\Sitemap $sitemap sitemap service
  39. * @property \fecshop\services\Store $store store service
  40. * @property \fecshop\services\Url $url url service
  41. *
  42. * @author Terry Zhao <2358269014@qq.com>
  43. * @since 1.0
  44. */
  45. class Application
  46. {
  47. /**
  48. * @var array 服务的配置数组
  49. */
  50. public $childService;
  51. /**
  52. * @var array 实例化过的服务数组
  53. */
  54. public $_childService;
  55. /**
  56. * @var array $config 注入的配置数组
  57. * 在 @app/web/index.php 入口文件处。会调用 new fecshop\services\Application($config['services']);
  58. * Yii::$service 就是该类实例化的对象,注入的配置保存到 $this->childService 中
  59. */
  60. public function __construct($config = [])
  61. {
  62. Yii::$service = $this;
  63. $this->childService = $config;
  64. }
  65. /**
  66. * 根据服务名字获取服务实例
  67. * Get service instance by service name.
  68. *
  69. * 用类似于 Yii2 的 component 原理,采用单例模式实现的服务功能,
  70. * 服务的配置文件位于 config/services 目录
  71. *
  72. * @var string $childServiceName
  73. * @return \fecshop\services\Service
  74. * @throws \yii\base\InvalidConfigException if the service is not found or the service is disabled
  75. */
  76. public function getChildService($childServiceName)
  77. {
  78. if (!isset($this->_childService[$childServiceName]) || !$this->_childService[$childServiceName]) {
  79. $childService = $this->childService;
  80. if (isset($childService[$childServiceName])) {
  81. $service = $childService[$childServiceName];
  82. if (!isset($service['enableService']) || $service['enableService']) {
  83. $this->_childService[$childServiceName] = Yii::createObject($service);
  84. } else {
  85. throw new InvalidConfigException('Child Service ['.$childServiceName.'] is disabled in '.get_called_class().', you must enable it! ');
  86. }
  87. } else {
  88. throw new InvalidConfigException('Child Service ['.$childServiceName.'] does not exist in '.get_called_class().', you must config it! ');
  89. }
  90. }
  91. return isset($this->_childService[$childServiceName]) ? $this->_childService[$childServiceName] : null;
  92. }
  93. /**
  94. * 魔术方法,当调用一个属性,对象不存在的时候就会执行该方法,然后
  95. * 根据构造方法注入的配置,实例化service对象。
  96. * @var string $serviceName service name
  97. * @return \fecshop\services\Service
  98. * @throws \yii\base\InvalidConfigException if the service does not exist or the service is disabled
  99. */
  100. public function __get($serviceName)
  101. {
  102. return $this->getChildService($serviceName);
  103. }
  104. }