CredentialsProvider.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace AlibabaCloud\Client\Credentials\Providers;
  3. use Closure;
  4. use AlibabaCloud\Client\SDK;
  5. use AlibabaCloud\Client\AlibabaCloud;
  6. use AlibabaCloud\Client\Exception\ClientException;
  7. /**
  8. * Class CredentialsProvider
  9. *
  10. * @package AlibabaCloud\Client\Credentials\Providers
  11. */
  12. class CredentialsProvider
  13. {
  14. /**
  15. * @var array
  16. */
  17. private static $customChains;
  18. /**
  19. * @throws ClientException
  20. */
  21. public static function chain()
  22. {
  23. $providers = func_get_args();
  24. if (empty($providers)) {
  25. throw new ClientException('No providers in chain', SDK::INVALID_ARGUMENT);
  26. }
  27. foreach ($providers as $provider) {
  28. if (!$provider instanceof Closure) {
  29. throw new ClientException('Providers must all be Closures', SDK::INVALID_ARGUMENT);
  30. }
  31. }
  32. self::$customChains = $providers;
  33. }
  34. /**
  35. * Forget the custom providers chain.
  36. */
  37. public static function flush()
  38. {
  39. self::$customChains = [];
  40. }
  41. /**
  42. * @return bool
  43. */
  44. public static function hasCustomChain()
  45. {
  46. return (bool)self::$customChains;
  47. }
  48. /**
  49. * @param string $clientName
  50. *
  51. * @throws ClientException
  52. */
  53. public static function customProvider($clientName)
  54. {
  55. foreach (self::$customChains as $provider) {
  56. $provider();
  57. if (AlibabaCloud::has($clientName)) {
  58. break;
  59. }
  60. }
  61. }
  62. /**
  63. * @param string $clientName
  64. *
  65. * @throws ClientException
  66. */
  67. public static function defaultProvider($clientName)
  68. {
  69. $providers = [
  70. self::env(),
  71. self::ini(),
  72. self::instance(),
  73. ];
  74. foreach ($providers as $provider) {
  75. $provider();
  76. if (AlibabaCloud::has($clientName)) {
  77. break;
  78. }
  79. }
  80. }
  81. /**
  82. * @return Closure
  83. */
  84. public static function env()
  85. {
  86. return static function () {
  87. $accessKeyId = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_ACCESS_KEY_ID');
  88. $accessKeySecret = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
  89. if ($accessKeyId && $accessKeySecret) {
  90. AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)->asDefaultClient();
  91. }
  92. };
  93. }
  94. /**
  95. * @return Closure
  96. */
  97. public static function ini()
  98. {
  99. return static function () {
  100. $ini = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_CREDENTIALS_FILE');
  101. if ($ini) {
  102. AlibabaCloud::load($ini);
  103. } else {
  104. // @codeCoverageIgnoreStart
  105. AlibabaCloud::load();
  106. // @codeCoverageIgnoreEnd
  107. }
  108. self::compatibleWithGlobal();
  109. };
  110. }
  111. /**
  112. * @codeCoverageIgnore
  113. *
  114. * Compatible with global
  115. *
  116. * @throws ClientException
  117. */
  118. private static function compatibleWithGlobal()
  119. {
  120. if (AlibabaCloud::has('global') && !AlibabaCloud::has(self::getDefaultName())) {
  121. AlibabaCloud::get('global')->name(self::getDefaultName());
  122. }
  123. }
  124. /**
  125. * @return array|false|string
  126. * @throws ClientException
  127. */
  128. public static function getDefaultName()
  129. {
  130. $name = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_PROFILE');
  131. if ($name) {
  132. return $name;
  133. }
  134. return 'default';
  135. }
  136. /**
  137. * @return Closure
  138. */
  139. public static function instance()
  140. {
  141. return static function () {
  142. $instance = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_ECS_METADATA');
  143. if ($instance) {
  144. AlibabaCloud::ecsRamRoleClient($instance)->asDefaultClient();
  145. }
  146. };
  147. }
  148. }