1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the strays/baidu-ai. |
5
|
|
|
* |
6
|
|
|
* (c) strays <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Strays\BaiDuAi\Kernel; |
13
|
|
|
|
14
|
|
|
use Pimple\Container; |
15
|
|
|
use Strays\BaiDuAi\Kernel\Providers\ConfigServiceProvider; |
16
|
|
|
use Strays\BaiDuAi\Kernel\Providers\HttpClientServiceProvider; |
17
|
|
|
|
18
|
|
|
class ServiceContainer extends Container |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $providers = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $config = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $defaultConfig = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* ServiceContainer constructor. |
37
|
|
|
* |
38
|
|
|
* @param array $config |
39
|
|
|
* @param array $prepends |
40
|
|
|
*/ |
41
|
|
|
public function __construct(array $config = [], $prepends = []) |
42
|
|
|
{ |
43
|
|
|
$this->registerProviders($this->getProviders()); |
44
|
|
|
|
45
|
|
|
parent::__construct($prepends); |
46
|
|
|
|
47
|
|
|
$this->config = $config; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* 返回配置信息. |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function getConfig() |
56
|
|
|
{ |
57
|
|
|
$base = [ |
58
|
|
|
'http' => [ |
59
|
|
|
'timeout' => 3.14, |
60
|
|
|
'base_uri' => 'https://aip.baidubce.com', |
61
|
|
|
], |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
return array_replace_recursive($base, $this->defaultConfig, $this->config); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* 将核心服务注入劲容器. |
69
|
|
|
* |
70
|
|
|
* @param array $providers |
71
|
|
|
*/ |
72
|
|
|
public function registerProviders(array $providers) |
73
|
|
|
{ |
74
|
|
|
foreach ($providers as $provider) { |
75
|
|
|
parent::register(new $provider()); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* 将服务合并成一个数组. |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function getProviders() |
85
|
|
|
{ |
86
|
|
|
return array_merge([ |
87
|
|
|
ConfigServiceProvider::class, |
88
|
|
|
HttpClientServiceProvider::class, |
89
|
|
|
], $this->providers); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function __get($name) |
93
|
|
|
{ |
94
|
|
|
return $this->offsetGet($name); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function __set($name, $value) |
98
|
|
|
{ |
99
|
|
|
$this->offsetSet($name, $value); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|