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 (!$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('open_basedir restriction in effect. ' |
53
|
|
|
. "File($filename) is not within the allowed path(s): ($open_basedir)", 'SDK.InvalidPath'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
function isWindows() |
60
|
|
|
{ |
61
|
|
|
return PATH_SEPARATOR === ';'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return CLImate |
66
|
|
|
*/ |
67
|
|
|
function cliMate() |
68
|
|
|
{ |
69
|
|
|
return new CLImate(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $string |
74
|
|
|
* @param string|null $flank |
75
|
|
|
* @param string|null $char |
76
|
|
|
* @param int|null $length |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
function backgroundRed($string, $flank = null, $char = null, $length = null) |
81
|
|
|
{ |
82
|
|
|
cliMate()->br(); |
83
|
|
|
if ($flank !== null) { |
84
|
|
|
cliMate()->backgroundRed()->flank($flank, $char, $length); |
85
|
|
|
cliMate()->br(); |
86
|
|
|
} |
87
|
|
|
cliMate()->backgroundRed($string); |
88
|
|
|
cliMate()->br(); |
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 backgroundGreen($string, $flank = null, $char = null, $length = null) |
100
|
|
|
{ |
101
|
|
|
cliMate()->br(); |
102
|
|
|
if ($flank !== null) { |
103
|
|
|
cliMate()->backgroundGreen()->flank($flank, $char, $length); |
104
|
|
|
} |
105
|
|
|
cliMate()->backgroundGreen($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 backgroundBlue($string, $flank = null, $char = null, $length = null) |
118
|
|
|
{ |
119
|
|
|
cliMate()->br(); |
120
|
|
|
if ($flank !== null) { |
121
|
|
|
cliMate()->backgroundBlue()->flank($flank, $char, $length); |
122
|
|
|
} |
123
|
|
|
cliMate()->backgroundBlue($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 backgroundMagenta($string, $flank = null, $char = null, $length = null) |
136
|
|
|
{ |
137
|
|
|
cliMate()->br(); |
138
|
|
|
if ($flank !== null) { |
139
|
|
|
cliMate()->backgroundMagenta()->flank($flank, $char, $length); |
140
|
|
|
} |
141
|
|
|
cliMate()->backgroundMagenta($string); |
142
|
|
|
cliMate()->br(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param array $array |
147
|
|
|
*/ |
148
|
|
|
function json(array $array) |
149
|
|
|
{ |
150
|
|
|
cliMate()->br(); |
151
|
|
|
cliMate()->backgroundGreen()->json($array); |
152
|
|
|
cliMate()->br(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param array $array |
157
|
|
|
* |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
|
|
function redTable($array) |
161
|
|
|
{ |
162
|
|
|
/** |
163
|
|
|
* @noinspection PhpUndefinedMethodInspection |
164
|
|
|
*/ |
165
|
|
|
cliMate()->redTable($array); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param mixed $result |
170
|
|
|
* @param string $title |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
function block($result, $title) |
175
|
|
|
{ |
176
|
|
|
cliMate()->backgroundGreen()->flank($title, '--', 20); |
177
|
|
|
dump($result); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param array $arrays |
182
|
|
|
* |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
|
|
function arrayMerge(array $arrays) |
186
|
|
|
{ |
187
|
|
|
$result = []; |
188
|
|
|
foreach ($arrays as $array) { |
189
|
|
|
foreach ($array as $key => $value) { |
190
|
|
|
if (is_int($key)) { |
191
|
|
|
$result[] = $value; |
192
|
|
|
continue; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if (isset($result[$key]) && is_array($result[$key])) { |
196
|
|
|
$result[$key] = arrayMerge( |
197
|
|
|
[$result[$key], $value] |
198
|
|
|
); |
199
|
|
|
continue; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$result[$key] = $value; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $result; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Gets the value of an environment variable. |
211
|
|
|
* |
212
|
|
|
* @param string $key |
213
|
|
|
* @param mixed $default |
214
|
|
|
* |
215
|
|
|
* @return mixed |
216
|
|
|
*/ |
217
|
|
|
function env($key, $default = null) |
218
|
|
|
{ |
219
|
|
|
$value = getenv($key); |
220
|
|
|
|
221
|
|
|
if ($value === false) { |
222
|
|
|
return value($default); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
switch (strtolower($value)) { |
226
|
|
|
case 'true': |
227
|
|
|
case '(true)': |
228
|
|
|
return true; |
229
|
|
|
case 'false': |
230
|
|
|
case '(false)': |
231
|
|
|
return false; |
232
|
|
|
case 'empty': |
233
|
|
|
case '(empty)': |
234
|
|
|
return ''; |
235
|
|
|
case 'null': |
236
|
|
|
case '(null)': |
237
|
|
|
return null; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
if (envSubstr($value)) { |
241
|
|
|
return substr($value, 1, -1); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $value; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param $value |
249
|
|
|
* |
250
|
|
|
* @return bool |
251
|
|
|
*/ |
252
|
|
|
function envSubstr($value) |
253
|
|
|
{ |
254
|
|
|
return ($valueLength = strlen($value)) > 1 && strpos($value, '"') === 0 && $value[$valueLength - 1] === '"'; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Return the default value of the given value. |
259
|
|
|
* |
260
|
|
|
* @param mixed $value |
261
|
|
|
* |
262
|
|
|
* @return mixed |
263
|
|
|
*/ |
264
|
|
|
function value($value) |
265
|
|
|
{ |
266
|
|
|
return $value instanceof Closure ? $value() : $value; |
267
|
|
|
} |
268
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.