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
|
|
|
|
67
|
|
|
if (0 === strpos($filename, $dir)) { |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
function isWindows() |
79
|
|
|
{ |
80
|
|
|
return PATH_SEPARATOR === ';'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return CLImate |
85
|
|
|
*/ |
86
|
|
|
function cliMate() |
87
|
|
|
{ |
88
|
|
|
return new CLImate(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $string |
93
|
|
|
* @param string|null $flank |
94
|
|
|
* @param string|null $char |
95
|
|
|
* @param int|null $length |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
function backgroundRed($string, $flank = null, $char = null, $length = null) |
100
|
|
|
{ |
101
|
|
|
cliMate()->br(); |
102
|
|
|
if ($flank !== null) { |
103
|
|
|
cliMate()->backgroundRed()->flank($flank, $char, $length); |
104
|
|
|
cliMate()->br(); |
105
|
|
|
} |
106
|
|
|
cliMate()->backgroundRed($string); |
107
|
|
|
cliMate()->br(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $string |
112
|
|
|
* @param string|null $flank |
113
|
|
|
* @param string|null $char |
114
|
|
|
* @param int|null $length |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
function backgroundGreen($string, $flank = null, $char = null, $length = null) |
119
|
|
|
{ |
120
|
|
|
cliMate()->br(); |
121
|
|
|
if ($flank !== null) { |
122
|
|
|
cliMate()->backgroundGreen()->flank($flank, $char, $length); |
123
|
|
|
} |
124
|
|
|
cliMate()->backgroundGreen($string); |
125
|
|
|
cliMate()->br(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $string |
130
|
|
|
* @param string|null $flank |
131
|
|
|
* @param string|null $char |
132
|
|
|
* @param int|null $length |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
function backgroundBlue($string, $flank = null, $char = null, $length = null) |
137
|
|
|
{ |
138
|
|
|
cliMate()->br(); |
139
|
|
|
if ($flank !== null) { |
140
|
|
|
cliMate()->backgroundBlue()->flank($flank, $char, $length); |
141
|
|
|
} |
142
|
|
|
cliMate()->backgroundBlue($string); |
143
|
|
|
cliMate()->br(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $string |
148
|
|
|
* @param string|null $flank |
149
|
|
|
* @param string|null $char |
150
|
|
|
* @param int|null $length |
151
|
|
|
* |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
function backgroundMagenta($string, $flank = null, $char = null, $length = null) |
155
|
|
|
{ |
156
|
|
|
cliMate()->br(); |
157
|
|
|
if ($flank !== null) { |
158
|
|
|
cliMate()->backgroundMagenta()->flank($flank, $char, $length); |
159
|
|
|
} |
160
|
|
|
cliMate()->backgroundMagenta($string); |
161
|
|
|
cliMate()->br(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param array $array |
166
|
|
|
*/ |
167
|
|
|
function json(array $array) |
168
|
|
|
{ |
169
|
|
|
cliMate()->br(); |
170
|
|
|
cliMate()->backgroundGreen()->json($array); |
171
|
|
|
cliMate()->br(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param array $array |
176
|
|
|
* |
177
|
|
|
* @return void |
178
|
|
|
*/ |
179
|
|
|
function redTable($array) |
180
|
|
|
{ |
181
|
|
|
/** |
182
|
|
|
* @noinspection PhpUndefinedMethodInspection |
183
|
|
|
*/ |
184
|
|
|
cliMate()->redTable($array); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param mixed $result |
189
|
|
|
* @param string $title |
190
|
|
|
* |
191
|
|
|
* @return void |
192
|
|
|
*/ |
193
|
|
|
function block($result, $title) |
194
|
|
|
{ |
195
|
|
|
cliMate()->backgroundGreen()->flank($title, '--', 20); |
196
|
|
|
dump($result); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param array $arrays |
201
|
|
|
* |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
|
|
function arrayMerge(array $arrays) |
205
|
|
|
{ |
206
|
|
|
$result = []; |
207
|
|
|
foreach ($arrays as $array) { |
208
|
|
|
foreach ($array as $key => $value) { |
209
|
|
|
if (is_int($key)) { |
210
|
|
|
$result[] = $value; |
211
|
|
|
continue; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
if (isset($result[$key]) && is_array($result[$key])) { |
215
|
|
|
$result[$key] = arrayMerge( |
216
|
|
|
[$result[$key], $value] |
217
|
|
|
); |
218
|
|
|
continue; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$result[$key] = $value; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $result; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Gets the value of an environment variable. |
230
|
|
|
* |
231
|
|
|
* @param string $key |
232
|
|
|
* @param mixed $default |
233
|
|
|
* |
234
|
|
|
* @return mixed |
235
|
|
|
*/ |
236
|
|
|
function env($key, $default = null) |
237
|
|
|
{ |
238
|
|
|
$value = getenv($key); |
239
|
|
|
|
240
|
|
|
if ($value === false) { |
241
|
|
|
return value($default); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if (envSubstr($value)) { |
245
|
|
|
return substr($value, 1, -1); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return envConversion($value); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param $value |
253
|
|
|
* |
254
|
|
|
* @return bool|string|null |
255
|
|
|
*/ |
256
|
|
|
function envConversion($value) |
257
|
|
|
{ |
258
|
|
|
$key = strtolower($value); |
259
|
|
|
|
260
|
|
|
if ($key === 'null' || $key === '(null)') { |
261
|
|
|
return null; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$list = [ |
265
|
|
|
'true' => true, |
266
|
|
|
'(true)' => true, |
267
|
|
|
'false' => false, |
268
|
|
|
'(false)' => false, |
269
|
|
|
'empty' => '', |
270
|
|
|
'(empty)' => '', |
271
|
|
|
]; |
272
|
|
|
|
273
|
|
|
return isset($list[$key]) ? $list[$key] : $value; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param $key |
278
|
|
|
* |
279
|
|
|
* @return bool|mixed |
280
|
|
|
* @throws ClientException |
281
|
|
|
*/ |
282
|
|
|
function envNotEmpty($key) |
283
|
|
|
{ |
284
|
|
|
$value = env($key, false); |
285
|
|
|
if ($value !== false && !$value) { |
286
|
|
|
throw new ClientException( |
287
|
|
|
"Environment variable '$key' cannot be empty", |
288
|
|
|
\ALIBABA_CLOUD_INVALID_ARGUMENT |
289
|
|
|
); |
290
|
|
|
} |
291
|
|
|
if ($value) { |
292
|
|
|
return $value; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return false; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @param $value |
300
|
|
|
* |
301
|
|
|
* @return bool |
302
|
|
|
*/ |
303
|
|
|
function envSubstr($value) |
304
|
|
|
{ |
305
|
|
|
return ($valueLength = strlen($value)) > 1 && strpos($value, '"') === 0 && $value[$valueLength - 1] === '"'; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Return the default value of the given value. |
310
|
|
|
* |
311
|
|
|
* @param mixed $value |
312
|
|
|
* |
313
|
|
|
* @return mixed |
314
|
|
|
*/ |
315
|
|
|
function value($value) |
316
|
|
|
{ |
317
|
|
|
return $value instanceof Closure ? $value() : $value; |
318
|
|
|
} |
319
|
|
|
|