ServiceContainer.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * This file is part of the overtrue/wechat.
  9. *
  10. * (c) overtrue <i@overtrue.me>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. namespace ByteDance\Kernel;
  16. use ByteDance\Kernel\Providers\ConfigServiceProvider;
  17. use ByteDance\Kernel\Providers\HttpClientServiceProvider;
  18. use ByteDance\Kernel\Providers\LogServiceProvider;
  19. use ByteDance\Kernel\Providers\RequestServiceProvider;
  20. use EasyWeChat\Kernel\Providers\EventDispatcherServiceProvider;
  21. use EasyWeChat\Kernel\Providers\ExtensionServiceProvider;
  22. use Pimple\Container;
  23. /**
  24. * Class ServiceContainer.
  25. *
  26. * @author overtrue <i@overtrue.me>
  27. *
  28. * @property \ByteDance\Kernel\Config $config
  29. * @property \Symfony\Component\HttpFoundation\Request $request
  30. * @property \GuzzleHttp\Client $http_client
  31. * @property \Monolog\Logger $logger
  32. * @property \Symfony\Component\EventDispatcher\EventDispatcher $events
  33. */
  34. class ServiceContainer extends Container
  35. {
  36. /**
  37. * @var string
  38. */
  39. protected $id;
  40. /**
  41. * @var array
  42. */
  43. protected $providers = [];
  44. /**
  45. * @var array
  46. */
  47. protected $defaultConfig = [];
  48. /**
  49. * @var array
  50. */
  51. protected $userConfig = [];
  52. /**
  53. * Constructor.
  54. *
  55. * @param array $config
  56. * @param array $prepends
  57. * @param string|null $id
  58. */
  59. public function __construct(array $config = [], array $prepends = [], string $id = null)
  60. {
  61. $this->registerProviders($this->getProviders());
  62. parent::__construct($prepends);
  63. $this->userConfig = $config;
  64. $this->id = $id;
  65. }
  66. /**
  67. * @return string
  68. */
  69. public function getId()
  70. {
  71. return $this->id ?? $this->id = md5(json_encode($this->userConfig));
  72. }
  73. /**
  74. * @return array
  75. */
  76. public function getConfig()
  77. {
  78. $base = [
  79. // http://docs.guzzlephp.org/en/stable/request-options.html
  80. 'http' => [
  81. 'timeout' => 30.0,
  82. 'base_uri' => 'https://developer.toutiao.com/',
  83. ],
  84. ];
  85. return array_replace_recursive($base, $this->defaultConfig, $this->userConfig);
  86. }
  87. /**
  88. * Return all providers.
  89. *
  90. * @return array
  91. */
  92. public function getProviders()
  93. {
  94. return array_merge([
  95. ConfigServiceProvider::class,
  96. LogServiceProvider::class,
  97. RequestServiceProvider::class,
  98. HttpClientServiceProvider::class,
  99. ExtensionServiceProvider::class,
  100. EventDispatcherServiceProvider::class,
  101. ], $this->providers);
  102. }
  103. /**
  104. * @param string $id
  105. * @param mixed $value
  106. */
  107. public function rebind($id, $value)
  108. {
  109. $this->offsetUnset($id);
  110. $this->offsetSet($id, $value);
  111. }
  112. /**
  113. * Magic get access.
  114. *
  115. * @param string $id
  116. *
  117. * @return mixed
  118. */
  119. public function __get($id)
  120. {
  121. return $this->offsetGet($id);
  122. }
  123. /**
  124. * Magic set access.
  125. *
  126. * @param string $id
  127. * @param mixed $value
  128. */
  129. public function __set($id, $value)
  130. {
  131. $this->offsetSet($id, $value);
  132. }
  133. /**
  134. * @param array $providers
  135. */
  136. public function registerProviders(array $providers)
  137. {
  138. foreach ($providers as $provider) {
  139. parent::register(new $provider());
  140. }
  141. }
  142. }