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