freyo /
mta-h5
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Freyo\MtaH5\Kernel; |
||
| 4 | |||
| 5 | use Freyo\MtaH5\Kernel\Providers\BaseClientServiceProvider; |
||
| 6 | use Freyo\MtaH5\Kernel\Providers\ConfigServiceProvider; |
||
| 7 | use Freyo\MtaH5\Kernel\Providers\HttpClientServiceProvider; |
||
| 8 | use Freyo\MtaH5\Kernel\Providers\LogServiceProvider; |
||
| 9 | use Freyo\MtaH5\Kernel\Providers\RequestServiceProvider; |
||
| 10 | use Pimple\Container; |
||
| 11 | |||
| 12 | class ServiceContainer extends Container |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | protected $providers = []; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $defaultConfig = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $userConfig = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Constructor. |
||
| 31 | * |
||
| 32 | * @param array $config |
||
| 33 | * @param array $prepends |
||
| 34 | */ |
||
| 35 | public function __construct(array $config = [], array $prepends = []) |
||
| 36 | { |
||
| 37 | $this->registerProviders($this->getProviders()); |
||
| 38 | |||
| 39 | parent::__construct($prepends); |
||
| 40 | |||
| 41 | $this->userConfig = $config; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Return all providers. |
||
| 46 | * |
||
| 47 | * @return array |
||
| 48 | */ |
||
| 49 | public function getProviders() |
||
| 50 | { |
||
| 51 | return array_merge([ |
||
| 52 | ConfigServiceProvider::class, |
||
| 53 | LogServiceProvider::class, |
||
| 54 | RequestServiceProvider::class, |
||
| 55 | HttpClientServiceProvider::class, |
||
| 56 | BaseClientServiceProvider::class, |
||
| 57 | ], $this->providers); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param array $providers |
||
| 62 | */ |
||
| 63 | public function registerProviders(array $providers) |
||
| 64 | { |
||
| 65 | foreach ($providers as $provider) { |
||
| 66 | parent::register(new $provider()); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Magic get access. |
||
| 72 | * |
||
| 73 | * @param string $id |
||
| 74 | * |
||
| 75 | * @return mixed |
||
| 76 | */ |
||
| 77 | public function __get($id) |
||
| 78 | { |
||
| 79 | return $this->offsetGet($id); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Magic set access. |
||
| 84 | * |
||
| 85 | * @param string $id |
||
| 86 | * @param mixed $value |
||
| 87 | */ |
||
| 88 | public function __set($id, $value) |
||
| 89 | { |
||
| 90 | $this->offsetSet($id, $value); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function getId() |
||
| 97 | { |
||
| 98 | return $this->id ?: ($this->id = md5(json_encode($this->userConfig))); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return array |
||
| 103 | */ |
||
| 104 | public function getConfig() |
||
| 105 | { |
||
| 106 | $base = [ |
||
| 107 | // http://docs.guzzlephp.org/en/stable/request-options.html |
||
| 108 | 'http' => [ |
||
| 109 | 'timeout' => 5.0, |
||
| 110 | 'base_uri' => 'https://mta.qq.com/h5/api/', |
||
| 111 | ], |
||
| 112 | ]; |
||
| 113 | |||
| 114 | return array_replace_recursive($base, $this->defaultConfig, $this->userConfig); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getSecretKey() |
||
| 121 | { |
||
| 122 | return $this->config->get('secret_key'); |
||
|
0 ignored issues
–
show
The property
config does not exist on Freyo\MtaH5\Kernel\ServiceContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | public function getAppId() |
||
| 129 | { |
||
| 130 | return $this->config->get('app_id'); |
||
|
0 ignored issues
–
show
The property
config does not exist on Freyo\MtaH5\Kernel\ServiceContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 131 | } |
||
| 132 | } |
||
| 133 |