1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Credentials\Ini; |
4
|
|
|
|
5
|
|
|
use AlibabaCloud\Client\SDK; |
6
|
|
|
use AlibabaCloud\Client\Clients\Client; |
7
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class IniCredential |
11
|
|
|
* |
12
|
|
|
* @package AlibabaCloud\Client\Credentials\Ini |
13
|
|
|
*/ |
14
|
|
|
class IniCredential |
15
|
|
|
{ |
16
|
|
|
use CreateTrait; |
17
|
|
|
use OptionsTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private static $hasLoaded; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $filename; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* IniCredential constructor. |
31
|
|
|
* |
32
|
|
|
* @param string $filename |
33
|
|
|
*/ |
34
|
70 |
|
public function __construct($filename = '') |
35
|
|
|
{ |
36
|
70 |
|
$this->filename = $filename ?: $this->getDefaultFile(); |
37
|
70 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the default credential file. |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
37 |
|
public function getDefaultFile() |
45
|
|
|
{ |
46
|
37 |
|
return self::getHomeDirectory() . DIRECTORY_SEPARATOR . '.alibabacloud' . DIRECTORY_SEPARATOR . 'credentials'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Gets the environment's HOME directory. |
51
|
|
|
* |
52
|
|
|
* @return null|string |
53
|
|
|
*/ |
54
|
38 |
|
private static function getHomeDirectory() |
55
|
|
|
{ |
56
|
38 |
|
if (getenv('HOME')) { |
57
|
37 |
|
return getenv('HOME'); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
return (getenv('HOMEDRIVE') && getenv('HOMEPATH')) |
61
|
1 |
|
? getenv('HOMEDRIVE') . getenv('HOMEPATH') |
62
|
1 |
|
: null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Clear credential cache. |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
24 |
|
public static function forgetLoadedCredentialsFile() |
71
|
|
|
{ |
72
|
24 |
|
self::$hasLoaded = []; |
73
|
24 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the credential file. |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
36 |
|
public function getFilename() |
81
|
|
|
{ |
82
|
36 |
|
return $this->filename; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param array $array |
87
|
|
|
* @param string $key |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
16 |
|
protected static function isNotEmpty(array $array, $key) |
92
|
|
|
{ |
93
|
16 |
|
return isset($array[$key]) && !empty($array[$key]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $key |
98
|
|
|
* @param string $clientName |
99
|
|
|
* |
100
|
|
|
* @throws ClientException |
101
|
|
|
*/ |
102
|
28 |
|
public function missingRequired($key, $clientName) |
103
|
|
|
{ |
104
|
28 |
|
throw new ClientException( |
105
|
28 |
|
"Missing required '$key' option for '$clientName' in " . $this->getFilename(), |
106
|
|
|
SDK::INVALID_CREDENTIAL |
107
|
28 |
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array|mixed |
112
|
|
|
* @throws ClientException |
113
|
|
|
*/ |
114
|
32 |
|
public function load() |
115
|
|
|
{ |
116
|
|
|
// If it has been loaded, assign the client directly. |
117
|
32 |
|
if (isset(self::$hasLoaded[$this->filename])) { |
118
|
|
|
/** |
119
|
|
|
* @var $client Client |
120
|
|
|
*/ |
121
|
5 |
|
foreach (self::$hasLoaded[$this->filename] as $projectName => $client) { |
122
|
5 |
|
$client->name($projectName); |
123
|
5 |
|
} |
124
|
|
|
|
125
|
5 |
|
return self::$hasLoaded[$this->filename]; |
126
|
|
|
} |
127
|
|
|
|
128
|
28 |
|
return $this->loadFile(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Exceptions will be thrown if the file is unreadable and not the default file. |
133
|
|
|
* |
134
|
|
|
* @return array|mixed |
135
|
|
|
* @throws ClientException |
136
|
|
|
*/ |
137
|
30 |
|
private function loadFile() |
138
|
|
|
{ |
139
|
30 |
|
if (!\AlibabaCloud\Client\inOpenBasedir($this->filename)) { |
140
|
4 |
|
return []; |
141
|
|
|
} |
142
|
|
|
|
143
|
26 |
|
if (!\is_readable($this->filename) || !\is_file($this->filename)) { |
144
|
3 |
|
if ($this->filename === $this->getDefaultFile()) { |
145
|
|
|
// @codeCoverageIgnoreStart |
146
|
|
|
return []; |
147
|
|
|
// @codeCoverageIgnoreEnd |
148
|
|
|
} |
149
|
2 |
|
throw new ClientException( |
150
|
2 |
|
'Credential file is not readable: ' . $this->getFilename(), |
151
|
|
|
SDK::INVALID_CREDENTIAL |
152
|
2 |
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
23 |
|
return $this->parseFile(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Decode the ini file into an array. |
160
|
|
|
* |
161
|
|
|
* @return array|mixed |
162
|
|
|
* @throws ClientException |
163
|
|
|
*/ |
164
|
25 |
|
private function parseFile() |
165
|
|
|
{ |
166
|
|
|
try { |
167
|
25 |
|
$file = \parse_ini_file($this->filename, true); |
168
|
25 |
|
if (\is_array($file) && $file !== []) { |
169
|
23 |
|
return $this->initClients($file); |
170
|
|
|
} |
171
|
2 |
|
throw new ClientException( |
172
|
2 |
|
'Format error: ' . $this->getFilename(), |
173
|
|
|
SDK::INVALID_CREDENTIAL |
174
|
2 |
|
); |
175
|
15 |
|
} catch (\Exception $e) { |
176
|
15 |
|
throw new ClientException( |
177
|
15 |
|
$e->getMessage(), |
178
|
15 |
|
SDK::INVALID_CREDENTIAL, |
179
|
|
|
$e |
180
|
15 |
|
); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Initialize clients. |
186
|
|
|
* |
187
|
|
|
* @param array $array |
188
|
|
|
* |
189
|
|
|
* @return array|mixed |
190
|
|
|
* @throws ClientException |
191
|
|
|
*/ |
192
|
23 |
|
private function initClients($array) |
193
|
|
|
{ |
194
|
23 |
|
foreach (\array_change_key_case($array) as $clientName => $configures) { |
195
|
23 |
|
$configures = \array_change_key_case($configures); |
196
|
23 |
|
$clientInstance = $this->createClient($clientName, $configures); |
197
|
10 |
|
if ($clientInstance instanceof Client) { |
198
|
9 |
|
self::$hasLoaded[$this->filename][$clientName] = $clientInstance; |
199
|
9 |
|
self::setClientAttributes($configures, $clientInstance); |
200
|
9 |
|
self::setCert($configures, $clientInstance); |
201
|
9 |
|
self::setProxy($configures, $clientInstance); |
202
|
9 |
|
} |
203
|
10 |
|
} |
204
|
|
|
|
205
|
10 |
|
return isset(self::$hasLoaded[$this->filename]) |
206
|
10 |
|
? self::$hasLoaded[$this->filename] |
207
|
10 |
|
: []; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|