ServiceContainer.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Libs\TikTok\Kernel;
  3. use App\Libs\TikTok\Kernel\Providers\CacheServiceProvider;
  4. use App\Libs\TikTok\Kernel\Providers\ConfigServiceProvider;
  5. use App\Libs\TikTok\Kernel\Providers\EventDispatcherServiceProvider;
  6. use App\Libs\TikTok\Kernel\Providers\HttpClientServiceProvider;
  7. use App\Libs\TikTok\Kernel\Providers\RequestServiceProvider;
  8. use GuzzleHttp\Client;
  9. use Pimple\Container;
  10. use Symfony\Component\EventDispatcher\EventDispatcher;
  11. /**
  12. * @property Config $config
  13. * @property Client $http_client
  14. * @property EventDispatcher $events
  15. * @since 2021-11-01
  16. * @author zhaoxiang <zhaoxiang051405@gmail.com>
  17. */
  18. class ServiceContainer extends Container {
  19. /**
  20. * @var string
  21. */
  22. protected $id;
  23. /**
  24. * @var array
  25. */
  26. protected $providers = [];
  27. /**
  28. * @var array
  29. */
  30. protected $defaultConfig = [];
  31. /**
  32. * @var array
  33. */
  34. protected $userConfig = [];
  35. /**
  36. * Constructor.
  37. *
  38. * @param array $config
  39. * @param array $prepends
  40. * @param string|null $id
  41. */
  42. public function __construct(array $config = [], array $prepends = [], string $id = '') {
  43. $this->userConfig = $config;
  44. parent::__construct($prepends);
  45. $this->registerProviders($this->getProviders());
  46. $this->id = $id;
  47. $this->events->dispatch(new Events\ApplicationInitialized($this));
  48. }
  49. /**
  50. * @return string
  51. */
  52. public function getId(): string {
  53. return $this->id ?? $this->id = md5(json_encode($this->userConfig));
  54. }
  55. /**
  56. * @return array
  57. */
  58. public function getConfig(): array {
  59. $base = [
  60. 'http' => [
  61. 'timeout' => 30.0
  62. ]
  63. ];
  64. return array_replace_recursive($base, $this->defaultConfig, $this->userConfig);
  65. }
  66. /**
  67. * Return all providers.
  68. *
  69. * @return array
  70. */
  71. public function getProviders(): array {
  72. return array_merge([
  73. CacheServiceProvider::class,
  74. ConfigServiceProvider::class,
  75. HttpClientServiceProvider::class,
  76. RequestServiceProvider::class,
  77. EventDispatcherServiceProvider::class
  78. ], $this->providers);
  79. }
  80. /**
  81. * @param string $id
  82. * @param mixed $value
  83. */
  84. public function rebind(string $id, $value): void {
  85. $this->offsetUnset($id);
  86. $this->offsetSet($id, $value);
  87. }
  88. /**
  89. * Magic get access.
  90. *
  91. * @param string $id
  92. *
  93. * @return mixed
  94. */
  95. public function __get(string $id) {
  96. return $this->offsetGet($id);
  97. }
  98. /**
  99. * Magic set access.
  100. *
  101. * @param string $id
  102. * @param mixed $value
  103. */
  104. public function __set(string $id, $value) {
  105. $this->offsetSet($id, $value);
  106. }
  107. /**
  108. * Just For ide
  109. * @param string $id
  110. * @author zhaoxiang <zhaoxiang051405@gmail.com>
  111. */
  112. public function __isset(string $id) {
  113. }
  114. /**
  115. *
  116. * @param array $providers
  117. * @author zhaoxiang <zhaoxiang051405@gmail.com>
  118. */
  119. public function registerProviders(array $providers): void {
  120. foreach ($providers as $provider) {
  121. $this->register(new $provider());
  122. }
  123. }
  124. }