1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client; |
4
|
|
|
|
5
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
6
|
|
|
use Closure; |
7
|
|
|
use League\CLImate\CLImate; |
8
|
|
|
use Stringy\Stringy; |
9
|
|
|
|
10
|
|
|
/* |
11
|
|
|
|-------------------------------------------------------------------------- |
12
|
|
|
| Global Functions for Alibaba Cloud |
13
|
|
|
|-------------------------------------------------------------------------- |
14
|
|
|
| |
15
|
|
|
| Some common global functions are defined here. |
16
|
|
|
| This file will be automatically loaded. |
17
|
|
|
| |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param $filename |
22
|
|
|
* @param bool $throwException |
23
|
|
|
* |
24
|
|
|
* @return bool |
25
|
|
|
* @throws ClientException |
26
|
|
|
*/ |
27
|
|
|
function inOpenBasedir($filename, $throwException = false) |
28
|
|
|
{ |
29
|
|
|
$open_basedir = ini_get('open_basedir'); |
30
|
|
|
if (!$open_basedir) { |
31
|
|
|
return true; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$dirs = explode(PATH_SEPARATOR, $open_basedir); |
35
|
|
|
if (empty($dirs)) { |
36
|
|
|
return true; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (inDir($filename, $dirs)) { |
40
|
|
|
return true; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if ($throwException === false) { |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
throw new ClientException( |
48
|
|
|
'open_basedir restriction in effect. ' |
49
|
|
|
. "File($filename) is not within the allowed path(s): ($open_basedir)", |
50
|
|
|
'SDK.InvalidPath' |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $filename |
56
|
|
|
* @param array $dirs |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
function inDir($filename, array $dirs) |
61
|
|
|
{ |
62
|
|
|
foreach ($dirs as $dir) { |
63
|
|
|
if (!Stringy::create($dir)->endsWith(DIRECTORY_SEPARATOR)) { |
64
|
|
|
$dir .= DIRECTORY_SEPARATOR; |
65
|
|
|
} |
66
|
|
|
if (false !== strpos($filename, $dir)) { |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
function isWindows() |
78
|
|
|
{ |
79
|
|
|
return PATH_SEPARATOR === ';'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return CLImate |
84
|
|
|
*/ |
85
|
|
|
function cliMate() |
86
|
|
|
{ |
87
|
|
|
return new CLImate(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $string |
92
|
|
|
* @param string|null $flank |
93
|
|
|
* @param string|null $char |
94
|
|
|
* @param int|null $length |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
function backgroundRed($string, $flank = null, $char = null, $length = null) |
99
|
|
|
{ |
100
|
|
|
cliMate()->br(); |
101
|
|
|
if ($flank !== null) { |
102
|
|
|
cliMate()->backgroundRed()->flank($flank, $char, $length); |
103
|
|
|
cliMate()->br(); |
104
|
|
|
} |
105
|
|
|
cliMate()->backgroundRed($string); |
106
|
|
|
cliMate()->br(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $string |
111
|
|
|
* @param string|null $flank |
112
|
|
|
* @param string|null $char |
113
|
|
|
* @param int|null $length |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
function backgroundGreen($string, $flank = null, $char = null, $length = null) |
118
|
|
|
{ |
119
|
|
|
cliMate()->br(); |
120
|
|
|
if ($flank !== null) { |
121
|
|
|
cliMate()->backgroundGreen()->flank($flank, $char, $length); |
122
|
|
|
} |
123
|
|
|
cliMate()->backgroundGreen($string); |
124
|
|
|
cliMate()->br(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $string |
129
|
|
|
* @param string|null $flank |
130
|
|
|
* @param string|null $char |
131
|
|
|
* @param int|null $length |
132
|
|
|
* |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
|
|
function backgroundBlue($string, $flank = null, $char = null, $length = null) |
136
|
|
|
{ |
137
|
|
|
cliMate()->br(); |
138
|
|
|
if ($flank !== null) { |
139
|
|
|
cliMate()->backgroundBlue()->flank($flank, $char, $length); |
140
|
|
|
} |
141
|
|
|
cliMate()->backgroundBlue($string); |
142
|
|
|
cliMate()->br(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $string |
147
|
|
|
* @param string|null $flank |
148
|
|
|
* @param string|null $char |
149
|
|
|
* @param int|null $length |
150
|
|
|
* |
151
|
|
|
* @return void |
152
|
|
|
*/ |
153
|
|
|
function backgroundMagenta($string, $flank = null, $char = null, $length = null) |
154
|
|
|
{ |
155
|
|
|
cliMate()->br(); |
156
|
|
|
if ($flank !== null) { |
157
|
|
|
cliMate()->backgroundMagenta()->flank($flank, $char, $length); |
158
|
|
|
} |
159
|
|
|
cliMate()->backgroundMagenta($string); |
160
|
|
|
cliMate()->br(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param array $array |
165
|
|
|
*/ |
166
|
|
|
function json(array $array) |
167
|
|
|
{ |
168
|
|
|
cliMate()->br(); |
169
|
|
|
cliMate()->backgroundGreen()->json($array); |
170
|
|
|
cliMate()->br(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param array $array |
175
|
|
|
* |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
function redTable($array) |
179
|
|
|
{ |
180
|
|
|
/** |
181
|
|
|
* @noinspection PhpUndefinedMethodInspection |
182
|
|
|
*/ |
183
|
|
|
cliMate()->redTable($array); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param mixed $result |
188
|
|
|
* @param string $title |
189
|
|
|
* |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
function block($result, $title) |
193
|
|
|
{ |
194
|
|
|
cliMate()->backgroundGreen()->flank($title, '--', 20); |
195
|
|
|
dump($result); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param array $arrays |
200
|
|
|
* |
201
|
|
|
* @return array |
202
|
|
|
*/ |
203
|
|
|
function arrayMerge(array $arrays) |
204
|
|
|
{ |
205
|
|
|
$result = []; |
206
|
|
|
foreach ($arrays as $array) { |
207
|
|
|
foreach ($array as $key => $value) { |
208
|
|
|
if (is_int($key)) { |
209
|
|
|
$result[] = $value; |
210
|
|
|
continue; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if (isset($result[$key]) && is_array($result[$key])) { |
214
|
|
|
$result[$key] = arrayMerge( |
215
|
|
|
[$result[$key], $value] |
216
|
|
|
); |
217
|
|
|
continue; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$result[$key] = $value; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $result; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Gets the value of an environment variable. |
229
|
|
|
* |
230
|
|
|
* @param string $key |
231
|
|
|
* @param mixed $default |
232
|
|
|
* |
233
|
|
|
* @return mixed |
234
|
|
|
*/ |
235
|
|
|
function env($key, $default = null) |
236
|
|
|
{ |
237
|
|
|
$value = getenv($key); |
238
|
|
|
|
239
|
|
|
if ($value === false) { |
240
|
|
|
return value($default); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
if (envSubstr($value)) { |
244
|
|
|
return substr($value, 1, -1); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return envConversion($value); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param $value |
252
|
|
|
* |
253
|
|
|
* @return bool|string|null |
254
|
|
|
*/ |
255
|
|
|
function envConversion($value) |
256
|
|
|
{ |
257
|
|
|
$key = strtolower($value); |
258
|
|
|
|
259
|
|
|
if ($key === 'null' || $key === '(null)') { |
260
|
|
|
return null; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
$list = [ |
264
|
|
|
'true' => true, |
265
|
|
|
'(true)' => true, |
266
|
|
|
'false' => false, |
267
|
|
|
'(false)' => false, |
268
|
|
|
'empty' => '', |
269
|
|
|
'(empty)' => '', |
270
|
|
|
]; |
271
|
|
|
|
272
|
|
|
return isset($list[$key]) ? $list[$key] : $value; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param $key |
277
|
|
|
* |
278
|
|
|
* @return bool|mixed |
279
|
|
|
* @throws ClientException |
280
|
|
|
*/ |
281
|
|
|
function envNotEmpty($key) |
282
|
|
|
{ |
283
|
|
|
$value = env($key, false); |
284
|
|
|
if ($value !== false && !$value) { |
285
|
|
|
throw new ClientException( |
286
|
|
|
"Environment variable '$key' cannot be empty", |
287
|
|
|
\ALIBABA_CLOUD_INVALID_ARGUMENT |
288
|
|
|
); |
289
|
|
|
} |
290
|
|
|
if ($value) { |
291
|
|
|
return $value; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return false; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @param $value |
299
|
|
|
* |
300
|
|
|
* @return bool |
301
|
|
|
*/ |
302
|
|
|
function envSubstr($value) |
303
|
|
|
{ |
304
|
|
|
return ($valueLength = strlen($value)) > 1 && strpos($value, '"') === 0 && $value[$valueLength - 1] === '"'; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Return the default value of the given value. |
309
|
|
|
* |
310
|
|
|
* @param mixed $value |
311
|
|
|
* |
312
|
|
|
* @return mixed |
313
|
|
|
*/ |
314
|
|
|
function value($value) |
315
|
|
|
{ |
316
|
|
|
return $value instanceof Closure ? $value() : $value; |
317
|
|
|
} |
318
|
|
|
|