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\Filter\ClientFilter; |
18
|
|
|
use AlibabaCloud\Client\Signature\SignatureInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Trait of the manage clients. |
22
|
|
|
* |
23
|
|
|
* @package AlibabaCloud\Client\Traits |
24
|
|
|
* |
25
|
|
|
* @mixin AlibabaCloud |
26
|
|
|
*/ |
27
|
|
|
trait ClientTrait |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array Containers of Clients |
31
|
|
|
*/ |
32
|
|
|
protected static $clients = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get the Client instance by name. |
36
|
|
|
* |
37
|
|
|
* @param string $clientName |
38
|
|
|
* |
39
|
|
|
* @return Client |
40
|
|
|
* @throws ClientException |
41
|
|
|
*/ |
42
|
121 |
|
public static function get($clientName) |
43
|
|
|
{ |
44
|
121 |
|
ClientFilter::clientName($clientName); |
45
|
|
|
|
46
|
119 |
|
if (self::has($clientName)) { |
47
|
117 |
|
return self::$clients[\strtolower($clientName)]; |
48
|
|
|
} |
49
|
|
|
|
50
|
3 |
|
throw new ClientException( |
51
|
3 |
|
"Client '$clientName' not found", |
52
|
|
|
\ALIBABA_CLOUD_CLIENT_NOT_FOUND |
53
|
3 |
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $clientName |
58
|
|
|
* @param Client $client |
59
|
|
|
* |
60
|
|
|
* @return Client |
61
|
|
|
* @throws ClientException |
62
|
|
|
*/ |
63
|
137 |
|
public static function set($clientName, Client $client) |
64
|
|
|
{ |
65
|
137 |
|
ClientFilter::clientName($clientName); |
66
|
|
|
|
67
|
135 |
|
return self::$clients[\strtolower($clientName)] = $client; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get all clients. |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
113 |
|
public static function all() |
76
|
|
|
{ |
77
|
113 |
|
return self::$clients; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Delete the client by specifying name. |
82
|
|
|
* |
83
|
|
|
* @param string $clientName |
84
|
|
|
* |
85
|
|
|
* @throws ClientException |
86
|
|
|
*/ |
87
|
60 |
|
public static function del($clientName) |
88
|
|
|
{ |
89
|
60 |
|
ClientFilter::clientName($clientName); |
90
|
|
|
|
91
|
58 |
|
unset(self::$clients[\strtolower($clientName)]); |
92
|
58 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Delete all clients. |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
24 |
|
public static function flush() |
100
|
|
|
{ |
101
|
24 |
|
self::$clients = []; |
102
|
24 |
|
self::$defaultRegionId = null; |
|
|
|
|
103
|
24 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @codeCoverageIgnore |
107
|
|
|
* @deprecated |
108
|
|
|
* Get the global client. |
109
|
|
|
* |
110
|
|
|
* @return Client |
111
|
|
|
* @throws ClientException |
112
|
|
|
*/ |
113
|
|
|
public static function getGlobalClient() |
114
|
|
|
{ |
115
|
|
|
return self::getDefaultClient(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the default client. |
120
|
|
|
* |
121
|
|
|
* @return Client |
122
|
|
|
* @throws ClientException |
123
|
|
|
*/ |
124
|
11 |
|
public static function getDefaultClient() |
125
|
|
|
{ |
126
|
11 |
|
return self::get(CredentialsProvider::getDefaultName()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Determine whether there is a client. |
131
|
|
|
* |
132
|
|
|
* @param string $clientName |
133
|
|
|
* |
134
|
|
|
* @return bool |
135
|
|
|
* @throws ClientException |
136
|
|
|
*/ |
137
|
136 |
|
public static function has($clientName) |
138
|
|
|
{ |
139
|
136 |
|
ClientFilter::clientName($clientName); |
140
|
|
|
|
141
|
134 |
|
return isset(self::$clients[\strtolower($clientName)]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* A list of additional files to load. |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
* @throws ClientException when a file has a syntax error or does not exist or is not readable |
149
|
|
|
*/ |
150
|
32 |
|
public static function load() |
151
|
|
|
{ |
152
|
32 |
|
if (\func_get_args() === []) { |
153
|
2 |
|
return (new IniCredential())->load(); |
154
|
|
|
} |
155
|
30 |
|
$list = []; |
156
|
30 |
|
foreach (\func_get_args() as $filename) { |
157
|
30 |
|
$list[$filename] = (new IniCredential($filename))->load(); |
158
|
14 |
|
} |
159
|
|
|
|
160
|
14 |
|
return $list; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Custom Client. |
165
|
|
|
* |
166
|
|
|
* @param CredentialsInterface $credentials |
167
|
|
|
* @param SignatureInterface $signature |
168
|
|
|
* |
169
|
|
|
* @return Client |
170
|
|
|
*/ |
171
|
12 |
|
public static function client(CredentialsInterface $credentials, SignatureInterface $signature) |
172
|
|
|
{ |
173
|
12 |
|
return new Client($credentials, $signature); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Use the AccessKey to complete the authentication. |
178
|
|
|
* |
179
|
|
|
* @param string $accessKeyId |
180
|
|
|
* @param string $accessKeySecret |
181
|
|
|
* |
182
|
|
|
* @return AccessKeyClient |
183
|
|
|
* @throws ClientException |
184
|
|
|
*/ |
185
|
86 |
|
public static function accessKeyClient($accessKeyId, $accessKeySecret) |
186
|
|
|
{ |
187
|
86 |
|
return new AccessKeyClient($accessKeyId, $accessKeySecret); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Use the AssumeRole of the RAM account to complete the authentication. |
192
|
|
|
* |
193
|
|
|
* @param string $accessKeyId |
194
|
|
|
* @param string $accessKeySecret |
195
|
|
|
* @param string $roleArn |
196
|
|
|
* @param string $roleSessionName |
197
|
|
|
* |
198
|
|
|
* @return RamRoleArnClient |
199
|
|
|
* @throws ClientException |
200
|
|
|
*/ |
201
|
14 |
|
public static function ramRoleArnClient($accessKeyId, $accessKeySecret, $roleArn, $roleSessionName) |
202
|
|
|
{ |
203
|
14 |
|
return new RamRoleArnClient($accessKeyId, $accessKeySecret, $roleArn, $roleSessionName); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Use the RAM role of an ECS instance to complete the authentication. |
208
|
|
|
* |
209
|
|
|
* @param string $roleName |
210
|
|
|
* |
211
|
|
|
* @return EcsRamRoleClient |
212
|
|
|
* @throws ClientException |
213
|
|
|
*/ |
214
|
19 |
|
public static function ecsRamRoleClient($roleName) |
215
|
|
|
{ |
216
|
19 |
|
return new EcsRamRoleClient($roleName); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Use the Bearer Token to complete the authentication. |
221
|
|
|
* |
222
|
|
|
* @param string $bearerToken |
223
|
|
|
* |
224
|
|
|
* @return BearerTokenClient |
225
|
|
|
* @throws ClientException |
226
|
|
|
*/ |
227
|
21 |
|
public static function bearerTokenClient($bearerToken) |
228
|
|
|
{ |
229
|
21 |
|
return new BearerTokenClient($bearerToken); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Use the STS Token to complete the authentication. |
234
|
|
|
* |
235
|
|
|
* @param string $accessKeyId Access key ID |
236
|
|
|
* @param string $accessKeySecret Access Key Secret |
237
|
|
|
* @param string $securityToken Security Token |
238
|
|
|
* |
239
|
|
|
* @return StsClient |
240
|
|
|
* @throws ClientException |
241
|
|
|
*/ |
242
|
5 |
|
public static function stsClient($accessKeyId, $accessKeySecret, $securityToken = '') |
243
|
|
|
{ |
244
|
5 |
|
return new StsClient($accessKeyId, $accessKeySecret, $securityToken); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Use the RSA key pair to complete the authentication (supported only on Japanese site) |
249
|
|
|
* |
250
|
|
|
* @param string $publicKeyId |
251
|
|
|
* @param string $privateKeyFile |
252
|
|
|
* |
253
|
|
|
* @return RsaKeyPairClient |
254
|
|
|
* @throws ClientException |
255
|
|
|
*/ |
256
|
14 |
|
public static function rsaKeyPairClient($publicKeyId, $privateKeyFile) |
257
|
|
|
{ |
258
|
14 |
|
return new RsaKeyPairClient($publicKeyId, $privateKeyFile); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|