1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the wannanbigpig/alipay-sdk-php. |
4
|
|
|
* |
5
|
|
|
* (c) wannanbigpig <[email protected]> |
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
|
|
|
|
11
|
|
|
namespace EasyAlipay\Kernel; |
12
|
|
|
|
13
|
|
|
use EasyAlipay\Kernel\Contracts\App; |
14
|
|
|
use EasyAlipay\Kernel\Exceptions\InvalidConfigException; |
15
|
|
|
use EasyAlipay\Kernel\Providers\AppServiceProvider; |
16
|
|
|
use EasyAlipay\Kernel\Providers\ConfigServiceProvider; |
17
|
|
|
use EasyAlipay\Kernel\Providers\HttpClientServiceProvider; |
18
|
|
|
use EasyAlipay\Kernel\Providers\LogServiceProvider; |
19
|
|
|
use EasyAlipay\Kernel\Providers\RequestServiceProvider; |
20
|
|
|
use Pimple\Container; |
21
|
|
|
use WannanBigPig\Supports\Exceptions\RuntimeException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class ServiceContainer |
25
|
|
|
* |
26
|
|
|
* @property \WannanBigPig\Supports\Config $config |
27
|
|
|
* @property \Symfony\Component\HttpFoundation\Request $request |
28
|
|
|
* @property \GuzzleHttp\Client $http_client |
29
|
|
|
* @property \Monolog\Logger $logger |
30
|
|
|
* |
31
|
|
|
* @author liuml <[email protected]> |
32
|
|
|
* @DateTime 2019-07-18 16:13 |
33
|
|
|
*/ |
34
|
|
|
class ServiceContainer extends Container implements App |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
const NORMAL_ENV = 'normal'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
const DEV_ENV = 'dev'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $gateway = [ |
50
|
|
|
self::NORMAL_ENV => 'https://openapi.alipay.com/gateway.do', |
51
|
|
|
self::DEV_ENV => 'https://openapi.alipaydev.com/gateway.do', |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected $providers = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
protected $userConfig = []; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected $defaultConfig = []; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Application constructor. |
71
|
|
|
* |
72
|
|
|
* @param array $config |
73
|
|
|
*/ |
74
|
62 |
|
public function __construct(array $config = []) |
75
|
|
|
{ |
76
|
62 |
|
parent::__construct(['app_client_providers' => $this->providers]); |
77
|
62 |
|
$this->init($config); |
78
|
62 |
|
$this->registerProviders($this->getProviders()); |
79
|
62 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* __get. |
83
|
|
|
* |
84
|
|
|
* @param $name |
85
|
|
|
* |
86
|
|
|
* @return mixed |
87
|
|
|
* |
88
|
|
|
* @throws \WannanBigPig\Supports\Exceptions\RuntimeException |
89
|
|
|
*/ |
90
|
26 |
|
public function __get($name) |
91
|
|
|
{ |
92
|
26 |
|
if (isset($this[$name])) { |
93
|
26 |
|
return $this[$name]; |
94
|
|
|
} |
95
|
|
|
throw new RuntimeException(sprintf('Identifier "%s" is not defined', $name)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* init application config. |
100
|
|
|
* |
101
|
|
|
* @param array $config |
102
|
|
|
*/ |
103
|
62 |
|
public function init(array $config) |
104
|
|
|
{ |
105
|
62 |
|
$this->userConfig = $config; |
106
|
62 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* getConfig. |
110
|
|
|
* |
111
|
|
|
* @return array|mixed |
112
|
|
|
*/ |
113
|
62 |
|
public function getConfig() |
114
|
|
|
{ |
115
|
|
|
$base = [ |
116
|
|
|
'http' => [ |
117
|
62 |
|
'timeout' => 6.0, |
118
|
62 |
|
'base_uri' => $this->getGateway(), |
119
|
62 |
|
'connect_timeout' => 6.0, |
120
|
62 |
|
'log_template' => "\n>>>>>>>>request\n--------\n{request}\n--------\n>>>>>>>>response\n--------\n{response}\n--------\n>>>>>>>>error\n--------\n{error}\n--------\n", |
121
|
|
|
], |
122
|
|
|
]; |
123
|
|
|
|
124
|
62 |
|
return array_replace_recursive($base, $this->defaultConfig, $this->userConfig); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the common request parameters of Alipay interface. |
129
|
|
|
* |
130
|
|
|
* @param $endpoint |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
14 |
|
public function apiCommonConfig(string $endpoint): array |
135
|
|
|
{ |
136
|
14 |
|
$this->config->set('api_method', $endpoint); |
137
|
|
|
|
138
|
14 |
|
return array_merge([ |
139
|
14 |
|
'app_id' => $this->config['sys_params.app_id'], |
140
|
14 |
|
'method' => $endpoint, |
141
|
14 |
|
'format' => 'JSON', |
142
|
14 |
|
'charset' => $this->config->get('sys_params.charset', 'utf-8'), |
143
|
14 |
|
'sign_type' => $this->config->get('sys_params.sign_type', 'RSA2'), |
144
|
14 |
|
'sign' => '', |
145
|
14 |
|
'timestamp' => date('Y-m-d H:i:s'), |
146
|
14 |
|
'version' => '1.0', |
147
|
14 |
|
'app_auth_token' => $this->config->get('sys_params.app_auth_token', ''), |
148
|
14 |
|
], $this->config->get($endpoint.'config', [])); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* setEndpointConfig. |
153
|
|
|
* |
154
|
|
|
* @param string $endpoint |
155
|
|
|
* @param array $config |
156
|
|
|
* |
157
|
|
|
* @return $this |
158
|
|
|
*/ |
159
|
10 |
|
public function setEndpointConfig(string $endpoint, array $config) |
160
|
|
|
{ |
161
|
10 |
|
$this->config->set($endpoint.'config', $config); |
162
|
|
|
|
163
|
10 |
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Acquisition of development environment. |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
62 |
|
public function getEnv(): bool |
172
|
|
|
{ |
173
|
62 |
|
return (bool)(isset($this->userConfig['sandbox']) ? $this->userConfig['sandbox'] : false); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get Alipay gateway address. |
178
|
|
|
* |
179
|
|
|
* @return mixed |
180
|
|
|
*/ |
181
|
62 |
|
public function getGateway() |
182
|
|
|
{ |
183
|
62 |
|
if ($this->getEnv()) { |
184
|
|
|
return $this->gateway[self::DEV_ENV]; |
185
|
|
|
} else { |
186
|
62 |
|
return $this->gateway[self::NORMAL_ENV]; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* setAppAuthToken. |
192
|
|
|
* |
193
|
|
|
* @param $appAuthToken |
194
|
|
|
* |
195
|
|
|
* @return $this |
196
|
|
|
*/ |
197
|
2 |
|
public function setAppAuthToken($appAuthToken) |
198
|
|
|
{ |
199
|
2 |
|
$this->config->set('sys_params.app_auth_token', $appAuthToken); |
200
|
|
|
|
201
|
2 |
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Return all providers. |
206
|
|
|
* |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
62 |
|
public function getProviders() |
210
|
|
|
{ |
211
|
62 |
|
return array_merge([ |
212
|
62 |
|
ConfigServiceProvider::class, |
213
|
|
|
HttpClientServiceProvider::class, |
214
|
|
|
LogServiceProvider::class, |
215
|
|
|
RequestServiceProvider::class, |
216
|
|
|
AppServiceProvider::class, |
217
|
|
|
]); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param array $providers |
222
|
|
|
*/ |
223
|
62 |
|
public function registerProviders(array $providers) |
224
|
|
|
{ |
225
|
62 |
|
foreach ($providers as $provider) { |
226
|
62 |
|
parent::register(new $provider()); |
227
|
|
|
} |
228
|
62 |
|
} |
229
|
|
|
} |
230
|
|
|
|