| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- /*
- * This file is part of the overtrue/wechat.
- *
- * (c) overtrue <i@overtrue.me>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace ByteDance\Kernel;
- use ByteDance\Kernel\Providers\ConfigServiceProvider;
- use ByteDance\Kernel\Providers\HttpClientServiceProvider;
- use ByteDance\Kernel\Providers\LogServiceProvider;
- use ByteDance\Kernel\Providers\RequestServiceProvider;
- use EasyWeChat\Kernel\Providers\EventDispatcherServiceProvider;
- use EasyWeChat\Kernel\Providers\ExtensionServiceProvider;
- use Pimple\Container;
- /**
- * Class ServiceContainer.
- *
- * @author overtrue <i@overtrue.me>
- *
- * @property \ByteDance\Kernel\Config $config
- * @property \Symfony\Component\HttpFoundation\Request $request
- * @property \GuzzleHttp\Client $http_client
- * @property \Monolog\Logger $logger
- * @property \Symfony\Component\EventDispatcher\EventDispatcher $events
- */
- class ServiceContainer extends Container
- {
- /**
- * @var string
- */
- protected $id;
- /**
- * @var array
- */
- protected $providers = [];
- /**
- * @var array
- */
- protected $defaultConfig = [];
- /**
- * @var array
- */
- protected $userConfig = [];
- /**
- * Constructor.
- *
- * @param array $config
- * @param array $prepends
- * @param string|null $id
- */
- public function __construct(array $config = [], array $prepends = [], string $id = null)
- {
- $this->registerProviders($this->getProviders());
- parent::__construct($prepends);
- $this->userConfig = $config;
- $this->id = $id;
- }
- /**
- * @return string
- */
- public function getId()
- {
- return $this->id ?? $this->id = md5(json_encode($this->userConfig));
- }
- /**
- * @return array
- */
- public function getConfig()
- {
- $base = [
- // http://docs.guzzlephp.org/en/stable/request-options.html
- 'http' => [
- 'timeout' => 30.0,
- 'base_uri' => 'https://developer.toutiao.com/',
- ],
- ];
- return array_replace_recursive($base, $this->defaultConfig, $this->userConfig);
- }
- /**
- * Return all providers.
- *
- * @return array
- */
- public function getProviders()
- {
- return array_merge([
- ConfigServiceProvider::class,
- LogServiceProvider::class,
- RequestServiceProvider::class,
- HttpClientServiceProvider::class,
- ExtensionServiceProvider::class,
- EventDispatcherServiceProvider::class,
- ], $this->providers);
- }
- /**
- * @param string $id
- * @param mixed $value
- */
- public function rebind($id, $value)
- {
- $this->offsetUnset($id);
- $this->offsetSet($id, $value);
- }
- /**
- * Magic get access.
- *
- * @param string $id
- *
- * @return mixed
- */
- public function __get($id)
- {
- return $this->offsetGet($id);
- }
- /**
- * Magic set access.
- *
- * @param string $id
- * @param mixed $value
- */
- public function __set($id, $value)
- {
- $this->offsetSet($id, $value);
- }
- /**
- * @param array $providers
- */
- public function registerProviders(array $providers)
- {
- foreach ($providers as $provider) {
- parent::register(new $provider());
- }
- }
- }
|