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