Application.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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->initRewriteMap($config);
  64. $this->childService = $config['services'];
  65. unset($config['services']);
  66. }
  67. // init yiiClassMap and fecRewriteMap
  68. public function initRewriteMap(&$config)
  69. {
  70. /**
  71. * yii class Map Custom
  72. */
  73. $yiiClassMap = isset($config['yiiClassMap']) ? $config['yiiClassMap'] : '';
  74. if(is_array($yiiClassMap) && !empty($yiiClassMap)){
  75. foreach($yiiClassMap as $namespace => $filePath){
  76. Yii::$classMap[$namespace] = $filePath;
  77. }
  78. }
  79. unset($config['yiiClassMap']);
  80. /**
  81. * Yii 重写block controller model等
  82. * 也就是说:除了compoent 和services,其他的用RewriteMap的方式来实现重写
  83. * 重写的类可以集成被重写的类
  84. */
  85. $fecRewriteMap = isset($config['fecRewriteMap']) ? $config['fecRewriteMap'] : '';
  86. if(is_array($fecRewriteMap) && !empty($fecRewriteMap)){
  87. Yii::$rewriteMap = $fecRewriteMap;
  88. }
  89. unset($config['fecRewriteMap']);
  90. }
  91. /**
  92. * 根据服务名字获取服务实例
  93. * Get service instance by service name.
  94. *
  95. * 用类似于 Yii2 的 component 原理,采用单例模式实现的服务功能,
  96. * 服务的配置文件位于 config/services 目录
  97. *
  98. * @var string $childServiceName
  99. * @return \fecshop\services\Service
  100. * @throws \yii\base\InvalidConfigException if the service is not found or the service is disabled
  101. */
  102. public function getChildService($childServiceName)
  103. {
  104. if (!isset($this->_childService[$childServiceName]) || !$this->_childService[$childServiceName]) {
  105. $childService = $this->childService;
  106. if (isset($childService[$childServiceName])) {
  107. $service = $childService[$childServiceName];
  108. if (!isset($service['enableService']) || $service['enableService']) {
  109. $this->_childService[$childServiceName] = Yii::createObject($service);
  110. } else {
  111. throw new InvalidConfigException('Child Service ['.$childServiceName.'] is disabled in '.get_called_class().', you must enable it! ');
  112. }
  113. } else {
  114. throw new InvalidConfigException('Child Service ['.$childServiceName.'] does not exist in '.get_called_class().', you must config it! ');
  115. }
  116. }
  117. return isset($this->_childService[$childServiceName]) ? $this->_childService[$childServiceName] : null;
  118. }
  119. /**
  120. * 魔术方法,当调用一个属性,对象不存在的时候就会执行该方法,然后
  121. * 根据构造方法注入的配置,实例化service对象。
  122. * @var string $serviceName service name
  123. * @return \fecshop\services\Service
  124. * @throws \yii\base\InvalidConfigException if the service does not exist or the service is disabled
  125. */
  126. public function __get($serviceName)
  127. {
  128. return $this->getChildService($serviceName);
  129. }
  130. }