ServiceContainer.php 3.6 KB

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