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