1 | <?php |
||||
2 | |||||
3 | namespace AlibabaCloud\Client\Credentials\Providers; |
||||
4 | |||||
5 | use Closure; |
||||
6 | use AlibabaCloud\Client\SDK; |
||||
7 | use AlibabaCloud\Client\AlibabaCloud; |
||||
8 | use AlibabaCloud\Client\Exception\ClientException; |
||||
9 | |||||
10 | /** |
||||
11 | * Class CredentialsProvider |
||||
12 | * |
||||
13 | * @package AlibabaCloud\Client\Credentials\Providers |
||||
14 | */ |
||||
15 | class CredentialsProvider |
||||
16 | { |
||||
17 | /** |
||||
18 | * @var array |
||||
19 | */ |
||||
20 | private static $customChains; |
||||
21 | |||||
22 | /** |
||||
23 | * @throws ClientException |
||||
24 | */ |
||||
25 | 6 | public static function chain() |
|||
26 | { |
||||
27 | 6 | $providers = func_get_args(); |
|||
28 | |||||
29 | 6 | if (empty($providers)) { |
|||
30 | 1 | throw new ClientException('No providers in chain', SDK::INVALID_ARGUMENT); |
|||
31 | } |
||||
32 | |||||
33 | 5 | foreach ($providers as $provider) { |
|||
34 | 5 | if (!$provider instanceof Closure) { |
|||
35 | 1 | throw new ClientException('Providers must all be Closures', SDK::INVALID_ARGUMENT); |
|||
36 | } |
||||
37 | 5 | } |
|||
38 | |||||
39 | 4 | self::$customChains = $providers; |
|||
40 | 4 | } |
|||
41 | |||||
42 | /** |
||||
43 | * Forget the custom providers chain. |
||||
44 | */ |
||||
45 | 5 | public static function flush() |
|||
46 | { |
||||
47 | 5 | self::$customChains = []; |
|||
48 | 5 | } |
|||
49 | |||||
50 | /** |
||||
51 | * @return bool |
||||
52 | */ |
||||
53 | 7 | public static function hasCustomChain() |
|||
54 | { |
||||
55 | 7 | return (bool)self::$customChains; |
|||
56 | } |
||||
57 | |||||
58 | /** |
||||
59 | * @param string $clientName |
||||
60 | * |
||||
61 | * @throws ClientException |
||||
62 | */ |
||||
63 | 3 | public static function customProvider($clientName) |
|||
64 | { |
||||
65 | 3 | foreach (self::$customChains as $provider) { |
|||
66 | 3 | $provider(); |
|||
67 | 3 | if (AlibabaCloud::has($clientName)) { |
|||
68 | 2 | break; |
|||
69 | } |
||||
70 | 3 | } |
|||
71 | 3 | } |
|||
72 | |||||
73 | /** |
||||
74 | * @param string $clientName |
||||
75 | * |
||||
76 | * @throws ClientException |
||||
77 | */ |
||||
78 | 4 | public static function defaultProvider($clientName) |
|||
79 | { |
||||
80 | $providers = [ |
||||
81 | 4 | self::env(), |
|||
82 | 4 | self::ini(), |
|||
83 | 4 | self::instance(), |
|||
84 | 4 | ]; |
|||
85 | |||||
86 | 4 | foreach ($providers as $provider) { |
|||
87 | 4 | $provider(); |
|||
88 | 4 | if (AlibabaCloud::has($clientName)) { |
|||
89 | 3 | break; |
|||
90 | } |
||||
91 | 4 | } |
|||
92 | 4 | } |
|||
93 | |||||
94 | /** |
||||
95 | * @return Closure |
||||
96 | */ |
||||
97 | 9 | public static function env() |
|||
98 | { |
||||
99 | return static function () { |
||||
100 | 7 | $accessKeyId = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_ACCESS_KEY_ID'); |
|||
101 | 6 | $accessKeySecret = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_ACCESS_KEY_SECRET'); |
|||
102 | |||||
103 | 5 | if ($accessKeyId && $accessKeySecret) { |
|||
104 | 5 | AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)->asDefaultClient(); |
|||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
It seems like
$accessKeyId can also be of type true ; however, parameter $accessKeyId of AlibabaCloud\Client\Alib...loud::accessKeyClient() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
105 | 5 | } |
|||
106 | 9 | }; |
|||
107 | } |
||||
108 | |||||
109 | /** |
||||
110 | * @return Closure |
||||
111 | */ |
||||
112 | 11 | public static function ini() |
|||
113 | { |
||||
114 | return static function () { |
||||
115 | 7 | $ini = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_CREDENTIALS_FILE'); |
|||
116 | |||||
117 | 6 | if ($ini) { |
|||
118 | 6 | AlibabaCloud::load($ini); |
|||
119 | 6 | } else { |
|||
120 | // @codeCoverageIgnoreStart |
||||
121 | AlibabaCloud::load(); |
||||
122 | // @codeCoverageIgnoreEnd |
||||
123 | } |
||||
124 | |||||
125 | 6 | self::compatibleWithGlobal(); |
|||
126 | 11 | }; |
|||
127 | } |
||||
128 | |||||
129 | /** |
||||
130 | * @codeCoverageIgnore |
||||
131 | * |
||||
132 | * Compatible with global |
||||
133 | * |
||||
134 | * @throws ClientException |
||||
135 | */ |
||||
136 | private static function compatibleWithGlobal() |
||||
137 | { |
||||
138 | if (AlibabaCloud::has('global') && !AlibabaCloud::has(self::getDefaultName())) { |
||||
139 | AlibabaCloud::get('global')->name(self::getDefaultName()); |
||||
140 | } |
||||
141 | } |
||||
142 | |||||
143 | /** |
||||
144 | * @return array|false|string |
||||
145 | * @throws ClientException |
||||
146 | */ |
||||
147 | 221 | public static function getDefaultName() |
|||
148 | { |
||||
149 | 221 | $name = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_PROFILE'); |
|||
150 | |||||
151 | 220 | if ($name) { |
|||
152 | 163 | return $name; |
|||
0 ignored issues
–
show
|
|||||
153 | } |
||||
154 | |||||
155 | 57 | return 'default'; |
|||
156 | } |
||||
157 | |||||
158 | /** |
||||
159 | * @return Closure |
||||
160 | */ |
||||
161 | public static function instance() |
||||
162 | { |
||||
163 | 6 | return static function () { |
|||
164 | 3 | $instance = \AlibabaCloud\Client\envNotEmpty('ALIBABA_CLOUD_ECS_METADATA'); |
|||
165 | 3 | if ($instance) { |
|||
166 | 3 | AlibabaCloud::ecsRamRoleClient($instance)->asDefaultClient(); |
|||
0 ignored issues
–
show
It seems like
$instance can also be of type true ; however, parameter $roleName of AlibabaCloud\Client\Alib...oud::ecsRamRoleClient() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
167 | 3 | } |
|||
168 | 6 | }; |
|||
169 | } |
||||
170 | } |
||||
171 |