1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Traits; |
4
|
|
|
|
5
|
|
|
use AlibabaCloud\Client\AlibabaCloud; |
6
|
|
|
use AlibabaCloud\Client\Clients\AccessKeyClient; |
7
|
|
|
use AlibabaCloud\Client\Clients\BearerTokenClient; |
8
|
|
|
use AlibabaCloud\Client\Clients\Client; |
9
|
|
|
use AlibabaCloud\Client\Clients\EcsRamRoleClient; |
10
|
|
|
use AlibabaCloud\Client\Clients\RamRoleArnClient; |
11
|
|
|
use AlibabaCloud\Client\Clients\RsaKeyPairClient; |
12
|
|
|
use AlibabaCloud\Client\Clients\StsClient; |
13
|
|
|
use AlibabaCloud\Client\Credentials\CredentialsInterface; |
14
|
|
|
use AlibabaCloud\Client\Credentials\Ini\IniCredential; |
15
|
|
|
use AlibabaCloud\Client\Credentials\Providers\CredentialsProvider; |
16
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
17
|
|
|
use AlibabaCloud\Client\Signature\SignatureInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Trait of the manage clients. |
21
|
|
|
* |
22
|
|
|
* @package AlibabaCloud\Client\Traits |
23
|
|
|
* |
24
|
|
|
* @mixin AlibabaCloud |
25
|
|
|
*/ |
26
|
|
|
trait ClientTrait |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var array Containers of Clients |
30
|
|
|
*/ |
31
|
|
|
protected static $clients = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get the Client instance by name. |
35
|
|
|
* |
36
|
|
|
* @param string $clientName |
37
|
|
|
* |
38
|
|
|
* @return Client |
39
|
|
|
* @throws ClientException |
40
|
|
|
*/ |
41
|
116 |
|
public static function get($clientName) |
42
|
|
|
{ |
43
|
116 |
|
if (!$clientName) { |
44
|
1 |
|
throw new ClientException( |
45
|
1 |
|
'The argument $clientName cannot be empty', |
46
|
|
|
\ALIBABA_CLOUD_INVALID_ARGUMENT |
47
|
1 |
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
115 |
|
if (self::has($clientName)) { |
51
|
113 |
|
return self::$clients[\strtolower($clientName)]; |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
throw new ClientException( |
55
|
3 |
|
"Client '$clientName' not found", |
56
|
|
|
\ALIBABA_CLOUD_CLIENT_NOT_FOUND |
57
|
3 |
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $clientName |
62
|
|
|
* @param Client $client |
63
|
|
|
* |
64
|
|
|
* @return Client |
65
|
|
|
* @throws ClientException |
66
|
|
|
*/ |
67
|
130 |
|
public static function set($clientName, Client $client) |
68
|
|
|
{ |
69
|
130 |
|
if (!$clientName) { |
70
|
1 |
|
throw new ClientException( |
71
|
1 |
|
'The argument $clientName cannot be empty', |
72
|
|
|
\ALIBABA_CLOUD_INVALID_ARGUMENT |
73
|
1 |
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
129 |
|
return self::$clients[\strtolower($clientName)] = $client; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get all clients. |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
109 |
|
public static function all() |
85
|
|
|
{ |
86
|
109 |
|
return self::$clients; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Delete the client by specifying name. |
91
|
|
|
* |
92
|
|
|
* @param string $clientName |
93
|
|
|
* |
94
|
|
|
* @throws ClientException |
95
|
|
|
*/ |
96
|
59 |
|
public static function del($clientName) |
97
|
|
|
{ |
98
|
59 |
|
if (!$clientName) { |
99
|
1 |
|
throw new ClientException( |
100
|
1 |
|
'The argument $clientName cannot be empty', |
101
|
|
|
\ALIBABA_CLOUD_INVALID_ARGUMENT |
102
|
1 |
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
58 |
|
unset(self::$clients[\strtolower($clientName)]); |
106
|
58 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Delete all clients. |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
24 |
|
public static function flush() |
114
|
|
|
{ |
115
|
24 |
|
self::$clients = []; |
116
|
24 |
|
self::$defaultRegionId = null; |
|
|
|
|
117
|
24 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @codeCoverageIgnore |
121
|
|
|
* @deprecated |
122
|
|
|
* Get the global client. |
123
|
|
|
* |
124
|
|
|
* @return Client |
125
|
|
|
* @throws ClientException |
126
|
|
|
*/ |
127
|
|
|
public static function getGlobalClient() |
128
|
|
|
{ |
129
|
|
|
return self::getDefaultClient(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get the default client. |
134
|
|
|
* |
135
|
|
|
* @return Client |
136
|
|
|
* @throws ClientException |
137
|
|
|
*/ |
138
|
11 |
|
public static function getDefaultClient() |
139
|
|
|
{ |
140
|
11 |
|
return self::get(CredentialsProvider::getDefaultName()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Determine whether there is a client. |
145
|
|
|
* |
146
|
|
|
* @param string $clientName |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
* @throws ClientException |
150
|
|
|
*/ |
151
|
131 |
|
public static function has($clientName) |
152
|
|
|
{ |
153
|
131 |
|
if (!$clientName) { |
154
|
1 |
|
throw new ClientException( |
155
|
1 |
|
'The argument $clientName cannot be empty', |
156
|
|
|
\ALIBABA_CLOUD_INVALID_ARGUMENT |
157
|
1 |
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
130 |
|
return isset(self::$clients[\strtolower($clientName)]); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* A list of additional files to load. |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
* @throws ClientException when a file has a syntax error or does not exist or is not readable |
168
|
|
|
*/ |
169
|
32 |
|
public static function load() |
170
|
|
|
{ |
171
|
32 |
|
if (\func_get_args() === []) { |
172
|
2 |
|
return (new IniCredential())->load(); |
173
|
|
|
} |
174
|
30 |
|
$list = []; |
175
|
30 |
|
foreach (\func_get_args() as $filename) { |
176
|
30 |
|
$list[$filename] = (new IniCredential($filename))->load(); |
177
|
14 |
|
} |
178
|
|
|
|
179
|
14 |
|
return $list; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Custom Client. |
184
|
|
|
* |
185
|
|
|
* @param CredentialsInterface $credentials |
186
|
|
|
* @param SignatureInterface $signature |
187
|
|
|
* |
188
|
|
|
* @return Client |
189
|
|
|
*/ |
190
|
10 |
|
public static function client(CredentialsInterface $credentials, SignatureInterface $signature) |
191
|
|
|
{ |
192
|
10 |
|
return new Client($credentials, $signature); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Use the AccessKey to complete the authentication. |
197
|
|
|
* |
198
|
|
|
* @param string $accessKeyId |
199
|
|
|
* @param string $accessKeySecret |
200
|
|
|
* |
201
|
|
|
* @return AccessKeyClient |
202
|
|
|
* @throws ClientException |
203
|
|
|
*/ |
204
|
79 |
|
public static function accessKeyClient($accessKeyId, $accessKeySecret) |
205
|
|
|
{ |
206
|
79 |
|
if (!$accessKeyId) { |
207
|
1 |
|
throw new ClientException( |
208
|
1 |
|
'The argument $accessKeyId cannot be empty', |
209
|
|
|
\ALIBABA_CLOUD_INVALID_ARGUMENT |
210
|
1 |
|
); |
211
|
|
|
} |
212
|
|
|
|
213
|
78 |
|
if (!$accessKeySecret) { |
214
|
1 |
|
throw new ClientException( |
215
|
1 |
|
'The argument $accessKeySecret cannot be empty', |
216
|
|
|
\ALIBABA_CLOUD_INVALID_ARGUMENT |
217
|
1 |
|
); |
218
|
|
|
} |
219
|
|
|
|
220
|
77 |
|
return new AccessKeyClient($accessKeyId, $accessKeySecret); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Use the AssumeRole of the RAM account to complete the authentication. |
225
|
|
|
* |
226
|
|
|
* @param string $accessKeyId |
227
|
|
|
* @param string $accessKeySecret |
228
|
|
|
* @param string $roleArn |
229
|
|
|
* @param string $roleSessionName |
230
|
|
|
* |
231
|
|
|
* @return RamRoleArnClient |
232
|
|
|
* @throws ClientException |
233
|
|
|
*/ |
234
|
9 |
|
public static function ramRoleArnClient($accessKeyId, $accessKeySecret, $roleArn, $roleSessionName) |
235
|
|
|
{ |
236
|
9 |
|
if (!$accessKeyId) { |
237
|
1 |
|
throw new ClientException( |
238
|
1 |
|
'The argument $accessKeyId cannot be empty', |
239
|
|
|
\ALIBABA_CLOUD_INVALID_ARGUMENT |
240
|
1 |
|
); |
241
|
|
|
} |
242
|
|
|
|
243
|
8 |
|
if (!$accessKeySecret) { |
244
|
1 |
|
throw new ClientException( |
245
|
1 |
|
'The argument $accessKeySecret cannot be empty', |
246
|
|
|
\ALIBABA_CLOUD_INVALID_ARGUMENT |
247
|
1 |
|
); |
248
|
|
|
} |
249
|
|
|
|
250
|
7 |
|
return new RamRoleArnClient($accessKeyId, $accessKeySecret, $roleArn, $roleSessionName); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Use the RAM role of an ECS instance to complete the authentication. |
255
|
|
|
* |
256
|
|
|
* @param string $roleName |
257
|
|
|
* |
258
|
|
|
* @return EcsRamRoleClient |
259
|
|
|
* @throws ClientException |
260
|
|
|
*/ |
261
|
12 |
|
public static function ecsRamRoleClient($roleName) |
262
|
|
|
{ |
263
|
12 |
|
if (!$roleName) { |
264
|
1 |
|
throw new ClientException( |
265
|
1 |
|
'The argument $roleName cannot be empty', |
266
|
|
|
\ALIBABA_CLOUD_INVALID_ARGUMENT |
267
|
1 |
|
); |
268
|
|
|
} |
269
|
|
|
|
270
|
11 |
|
return new EcsRamRoleClient($roleName); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Use the Bearer Token to complete the authentication. |
275
|
|
|
* |
276
|
|
|
* @param string $bearerToken |
277
|
|
|
* |
278
|
|
|
* @return BearerTokenClient |
279
|
|
|
* @throws ClientException |
280
|
|
|
*/ |
281
|
18 |
|
public static function bearerTokenClient($bearerToken) |
282
|
|
|
{ |
283
|
18 |
|
if (!$bearerToken) { |
284
|
1 |
|
throw new ClientException( |
285
|
1 |
|
'The argument $bearerToken cannot be empty', |
286
|
|
|
\ALIBABA_CLOUD_INVALID_ARGUMENT |
287
|
1 |
|
); |
288
|
|
|
} |
289
|
|
|
|
290
|
17 |
|
return new BearerTokenClient($bearerToken); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Use the STS Token to complete the authentication. |
295
|
|
|
* |
296
|
|
|
* @param string $accessKeyId Access key ID |
297
|
|
|
* @param string $accessKeySecret Access Key Secret |
298
|
|
|
* @param string $securityToken Security Token |
299
|
|
|
* |
300
|
|
|
* @return StsClient |
301
|
|
|
* @throws ClientException |
302
|
|
|
*/ |
303
|
3 |
|
public static function stsClient($accessKeyId, $accessKeySecret, $securityToken = '') |
304
|
|
|
{ |
305
|
3 |
|
if (!$accessKeyId) { |
306
|
1 |
|
throw new ClientException( |
307
|
1 |
|
'The argument $accessKeyId cannot be empty', |
308
|
|
|
\ALIBABA_CLOUD_INVALID_ARGUMENT |
309
|
1 |
|
); |
310
|
|
|
} |
311
|
|
|
|
312
|
2 |
|
if (!$accessKeySecret) { |
313
|
1 |
|
throw new ClientException( |
314
|
1 |
|
'The argument $accessKeySecret cannot be empty', |
315
|
|
|
\ALIBABA_CLOUD_INVALID_ARGUMENT |
316
|
1 |
|
); |
317
|
|
|
} |
318
|
|
|
|
319
|
1 |
|
return new StsClient($accessKeyId, $accessKeySecret, $securityToken); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Use the RSA key pair to complete the authentication (supported only on Japanese site) |
324
|
|
|
* |
325
|
|
|
* @param string $publicKeyId |
326
|
|
|
* @param string $privateKeyFile |
327
|
|
|
* |
328
|
|
|
* @return RsaKeyPairClient |
329
|
|
|
* @throws ClientException |
330
|
|
|
*/ |
331
|
10 |
|
public static function rsaKeyPairClient($publicKeyId, $privateKeyFile) |
332
|
|
|
{ |
333
|
10 |
|
if (!$publicKeyId) { |
334
|
1 |
|
throw new ClientException( |
335
|
1 |
|
'The argument $publicKeyId cannot be empty', |
336
|
|
|
\ALIBABA_CLOUD_INVALID_ARGUMENT |
337
|
1 |
|
); |
338
|
|
|
} |
339
|
|
|
|
340
|
9 |
|
if (!$privateKeyFile) { |
341
|
1 |
|
throw new ClientException( |
342
|
1 |
|
'The argument $privateKeyFile cannot be empty', |
343
|
|
|
\ALIBABA_CLOUD_INVALID_ARGUMENT |
344
|
1 |
|
); |
345
|
|
|
} |
346
|
|
|
|
347
|
8 |
|
return new RsaKeyPairClient($publicKeyId, $privateKeyFile); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|