-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.php
351 lines (280 loc) · 8.86 KB
/
helpers.php
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
declare(strict_types=1);
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use SebastianBergmann\Timer\ResourceUsageFormatter;
use SebastianBergmann\Timer\Timer;
if (! function_exists('environment')) {
function environment(): string
{
if (defined('STDIN')) {
return 'cli';
}
if ('cli' === PHP_SAPI) {
return 'cli';
}
if (stripos(PHP_SAPI, 'cgi') !== false && getenv('TERM')) {
return 'cli';
}
if (empty($_SERVER['REMOTE_ADDR']) && ! isset($_SERVER['HTTP_USER_AGENT']) && count($_SERVER['argv']) > 0) {
return 'cli';
}
return 'web';
}
}
if (! function_exists('format_bits')) {
function format_bits(int $bits, $precision = 2, $suffix = true)
{
if ($bits > 0) {
$i = floor(log($bits) / log(1000));
if (! $suffix) {
return round($bits / (1000 ** $i), $precision);
}
$sizes = ['B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb'];
return sprintf('%.02F', round($bits / (1000 ** $i), $precision)) * 1 .' '.@$sizes[$i];
}
return 0;
}
}
if (! function_exists('format_bytes')) {
function format_bytes(int $bytes, $precision = 2)
{
if ($bytes > 0) {
$i = floor(log($bytes) / log(1024));
$sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
return sprintf('%.02F', round($bytes / (1024 ** $i), $precision)) * 1 .' '.@$sizes[$i];
}
return 0;
}
}
if (! function_exists('bytes_to_bits')) {
function bytes_to_bits(int $bytes)
{
if ($bytes > 0) {
return $bytes * 8;
}
return 0;
}
}
if (! function_exists('memoize')) {
function memoize(callable $function): callable
{
return function () use ($function) {
static $cache = [];
$args = func_get_args();
$key = serialize($args);
$cached = true;
if (! isset($cache[$key])) {
$cache[$key] = $function(...$args);
$cached = false;
}
return ['result' => $cache[$key], 'cached' => $cached];
};
}
}
if (! function_exists('once')) {
function once(callable $function): callable
{
return function (...$args) use ($function) {
static $called = false;
if ($called) {
return;
}
$called = true;
return $function(...$args);
};
}
}
if (! function_exists('is_json')) {
/**
* If the string is valid JSON, return true, otherwise return false
*
* @param string $str the string to check
* @return bool the function is_json() is returning a boolean value
*/
function is_json(string $str): bool
{
json_decode($str);
return json_last_error() === JSON_ERROR_NONE;
}
}
if (! function_exists('validate')) {
function validate(array $data = [], array $rules = [], array $messages = [], array $customAttributes = []): array
{
return validator($data, $rules, $messages, $customAttributes)->validate();
}
}
if (! function_exists('call')) {
function call($callback, array $parameters = [], ?string $defaultMethod = null): void
{
app()->call($callback, $parameters, $defaultMethod);
}
}
if (! function_exists('catch_resource_usage')) {
function catch_resource_usage($callback, ...$parameter): string
{
$timer = new Timer();
$timer->start();
app()->call($callback, $parameter);
return (new ResourceUsageFormatter())->resourceUsage($timer->stop());
}
}
if (! function_exists('resolve_class_from')) {
function resolve_class_from(string $path, ?string $vendorPath = null, ?string $vendorNamespace = null): string
{
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
$vendorPath = $vendorPath ? realpath($vendorPath) : app_path();
$vendorNamespace ??= config('modules.namespace'); // App\
return str(realpath($path))
->replaceFirst($vendorPath, $vendorNamespace)
->replaceLast('.php', '')
->replace(DIRECTORY_SEPARATOR, '\\')
->replace('\\\\', '\\')
->start('\\')
->toString();
}
}
/*
* 写入日志
*
* @note single 创建单个日志文件 daily 定期清理日志文件
*/
if (! function_exists('write_log')) {
function write_log($path, $data, $context = [], $config = []): void
{
if (empty($config['log_file'])) {
$log_file = Carbon\Carbon::now()->format($config['format'] ?? 'Ymd').'.log';
} else {
$log_file = $config['log_file'];
}
$path = storage_path('logs'.DIRECTORY_SEPARATOR.$path.DIRECTORY_SEPARATOR.$log_file);
if (! is_string($data)) {
$data = (string) json_encode(\Illuminate\Support\Arr::transform($data), JSON_UNESCAPED_UNICODE);
}
Log::build(array_merge(['driver' => 'single', 'path' => $path], $config))->info($data, $context);
}
}
if (! function_exists('catch_query_log')) {
function catch_query_log($callback, ...$parameter): array
{
return (new Pipeline(app()))
->send($callback)
->through(function ($callback, Closure $next) {
DB::enableQueryLog();
DB::flushQueryLog();
$queryLog = $next($callback);
DB::disableQueryLog();
return $queryLog;
})
->then(function ($callback) use ($parameter) {
app()->call($callback, $parameter);
return DB::getQueryLog();
});
}
}
if (! function_exists('dump_to_array')) {
function dump_to_array(...$vars): void
{
foreach ($vars as $var) {
($var instanceof Arrayable or method_exists($var, 'toArray')) ? dump($var->toArray()) : dump($var);
}
}
}
if (! function_exists('generate_random')) {
function generate_random($type = 'alnum', $len = 8)
{
switch ($type) {
case 'alpha':
case 'alnum':
case 'numeric':
case 'nozero':
$pool = '';
switch ($type) {
case 'alpha':
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'alnum':
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'numeric':
$pool = '0123456789';
break;
case 'nozero':
$pool = '123456789';
break;
}
return substr(str_shuffle(str_repeat($pool, (int) ceil($len / strlen($pool)))), 0, $len);
case 'unique':
case 'md5':
return md5(uniqid(mt_rand(), true));
case 'encrypt':
case 'sha1':
return sha1(uniqid(mt_rand(), true));
}
return '';
}
}
if (! function_exists('dd_to_array')) {
function dd_to_array(...$vars): void
{
dump_to_array(...$vars);
exit(1);
}
}
if (! function_exists('amis')) {
function amis($type = null)
{
if (filled($type)) {
return \Modules\Core\Renderers\Component::make()->setType($type);
}
return \Modules\Core\Renderers\Amis::make();
}
}
if (! function_exists('amisMake')) {
function amisMake()
{
return \Modules\Core\Renderers\Amis::make();
}
}
if (! function_exists('pd')) {
function pd(...$vars): void
{
pp(...$vars);
exit(1);
}
}
if (! function_exists('pp')) {
function pp(...$vars): void
{
foreach ($vars as $var) {
/** @noinspection DebugFunctionUsageInspection */
highlight_string(sprintf("\n<?php\n\$var = %s;\n?>\n", var_export($var, true)));
}
}
}
if (! function_exists('export_csv')) {
function export_csv($list, $filename): bool
{
if (!is_array($list)) {
return false;
}
ob_start();
$fp = fopen($filename, 'a');
if (!empty($list)) {
$size = ceil(count($list) / 500);
for ($i = 0; $i < $size; $i++) {
$buffer = array_slice($list, $i * 500, 500);
foreach ($buffer as $k => $row) {
fputcsv($fp, array_values(\Illuminate\Support\Arr::transform($row)));
unset($data, $buffer[$k]);
}
}
}
fclose($fp);
ob_end_clean();
return true;
}
}