酒店预订平台
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

168 rader
3.7 KiB

  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. * @param array $config
  53. * @param array $prepends
  54. * @param string|null $id
  55. */
  56. public function __construct(array $config = [], array $prepends = [], string $id = null)
  57. {
  58. $this->registerProviders($this->getProviders());
  59. parent::__construct($prepends);
  60. $this->userConfig = $config;
  61. $this->id = $id;
  62. $this->aggregate();
  63. $this->events->dispatch(new Events\ApplicationInitialized($this));
  64. }
  65. /**
  66. * @return string
  67. */
  68. public function getId()
  69. {
  70. return $this->id ?? $this->id = md5(json_encode($this->userConfig));
  71. }
  72. /**
  73. * @return array
  74. */
  75. public function getConfig()
  76. {
  77. $base = [
  78. // http://docs.guzzlephp.org/en/stable/request-options.html
  79. 'http' => [
  80. 'timeout' => 30.0,
  81. 'base_uri' => 'https://api.weixin.qq.com/',
  82. ],
  83. ];
  84. return array_replace_recursive($base, $this->defaultConfig, $this->userConfig);
  85. }
  86. /**
  87. * Return all providers.
  88. *
  89. * @return array
  90. */
  91. public function getProviders()
  92. {
  93. return array_merge([
  94. ConfigServiceProvider::class,
  95. LogServiceProvider::class,
  96. RequestServiceProvider::class,
  97. HttpClientServiceProvider::class,
  98. ExtensionServiceProvider::class,
  99. EventDispatcherServiceProvider::class,
  100. ], $this->providers);
  101. }
  102. /**
  103. * @param string $id
  104. * @param mixed $value
  105. */
  106. public function rebind($id, $value)
  107. {
  108. $this->offsetUnset($id);
  109. $this->offsetSet($id, $value);
  110. }
  111. /**
  112. * Magic get access.
  113. *
  114. * @param string $id
  115. *
  116. * @return mixed
  117. */
  118. public function __get($id)
  119. {
  120. if ($this->shouldDelegate($id)) {
  121. return $this->delegateTo($id);
  122. }
  123. return $this->offsetGet($id);
  124. }
  125. /**
  126. * Magic set access.
  127. *
  128. * @param string $id
  129. * @param mixed $value
  130. */
  131. public function __set($id, $value)
  132. {
  133. $this->offsetSet($id, $value);
  134. }
  135. /**
  136. * @param array $providers
  137. */
  138. public function registerProviders(array $providers)
  139. {
  140. foreach ($providers as $provider) {
  141. parent::register(new $provider());
  142. }
  143. }
  144. }