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