ClientTrait.php 6.9 KB

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