-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhb-functions.inc.php
552 lines (499 loc) · 13.1 KB
/
hb-functions.inc.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
<?php
declare(strict_types=1);
/*
* This file is part of Homebase 2 PHP Framework - https://github.com/homebase/hb-core
*/
namespace hb2;
use hbc\core\StrX;
class Exception extends \Exception
{
/** @var mixed[] */
public array $payload; // optional hash payload
/**
* @param mixed[] $payload
*/
function __construct(string $msg, int $code = 0, array $payload = [])
{
$this->payload = $payload;
parent::__construct($msg, $code);
}
}
// Non-recoverable Error
// thrown by error_if, error_unless
class Error extends \Error
{
/** @var mixed[] */
public array $payload; // optional hash payload
/**
* @param mixed[] $payload
*/
function __construct(string $msg, int $code = 0, array $payload = [])
{
$this->payload = $payload;
parent::__construct($msg, $code);
}
}
// anything to ~ PHP string with unprintable characters replaced
// ATTENTION: may/will intentionally lose data !!
// will try to fit result in ~200 characters
function x2s(mixed $x, int $deep = 0, int $cut = 200): string
{
return StrX::x2s($x, $deep, $cut);
}
// caller file & line as string
function caller(int $level = 1): string
{
// "file:line"
$t = debug_backtrace()[$level];
return implode(':', [$t['file'] ?? '?', $t['line'] ?? '?']);
}
/**
* Return Number-of-Missed-Events once during $timeout (for all php processes)
* statistics kept in APC.
*
* Useful for throttling error messages.
* $key can be omitted, in this case "$filename:$line" will be used as a key
*
* Example 1:
* while(1) {
* if(once("event-name", 5))
* echo "text will be printed once, every 5 seconds";
* usleep(100000);
* }
* Example 2:
* if ($c = once($key, 10, 5))
* I::Log()->error("$c events occurred for key=$key in last 10 seconds"); // only cases with 6+ events !!
* Example 3: - log once every 10+ seconds
* if ($cnt = once())
* i('log')->error("$cnt errors in last 10 seconds");
*/
function once(string $key = '', int $timeout = 10, int $skip_events = 0): bool|int
{
if (!$key) {
$key = caller();
}
$data = apcu_fetch($key);
$now = time();
// first time
if (!$data) {
return (int) apcu_add($key, [1, $now], $timeout) && !$skip_events; // return 1
}
// increment
if ($data[1] + $timeout >= $now || ($skip_events && $skip_events > $data[0])) {
return !apcu_store($key, [++$data[0], $data[1]], $timeout); // return false
}
// expired
apcu_store($key, [1, $now], $timeout);
return $data[0]; // return inc
}
/**
* Perls qw ( Quote Words ) alike
* non-string input returned w/o processing
* supports hash definition.
*
* entry_delimiter - entry delimiter
* key_value_delimiter - key/value delimiter
*
* example: qw("a b c:Data") == ["a", "b" , "c" => "Data"]
*
* @param string|string[] $data
* @param non-empty-string $entry_delimiter
* @param non-empty-string $key_value_delimiter
*
* @return mixed[]
*/
function qw(array|string $data, string $entry_delimiter = ' ', string $key_value_delimiter = ':'): array
{
if (!\is_string($data)) {
return $data;
}
if (!$data) {
return [];
}
$res = ' ' === $entry_delimiter ? preg_split('/\s+/', trim($data)) : explode($entry_delimiter, $data);
if (!strpos($data, $key_value_delimiter)) {
return $res;
}
$ret = [];
foreach ($res as $r) {
if ($p = strpos($r, $key_value_delimiter)) {
$ret[substr($r, 0, $p)] = substr($r, $p + 1);
} else {
$ret[] = $r;
}
}
return $ret;
}
/**
* qw like function, Quote Keys
* example: qk("a b c:Data") == array( "a" => true, "b"=> true , "c" => "Data").
*
* @param mixed[]|string $data
* @param non-empty-string $entry_delimiter
* @param non-empty-string $key_value_delimiter
*
* @return mixed[]
*
* @noinspection PhpUnused
*/
function qk(array|string $data, string $entry_delimiter = ' ', string $key_value_delimiter = ':'): array
{
if (!\is_string($data)) {
return $data;
}
// hash
if (!$data) {
return [];
}
$res = ' ' === $entry_delimiter ? preg_split('/\s+/', trim($data)) : explode($entry_delimiter, $data);
$ret = [];
foreach ($res as $r) {
if ($p = strpos($r, $key_value_delimiter)) {
$ret[substr($r, 0, $p)] = substr($r, $p + 1);
} else {
$ret[$r] = true;
}
}
return $ret;
}
/**
* is_admin - is web-visitor is admin
* return "cli" for cli clients.
*
* use default method specified in
*
* In order to use admin methods -
* configure existing is_admin methods
* or provide your method
*
* @see config "is_admin" node
*
* TODO - PROVIDE SAMPLE IMPLEMENTATION FOR IS_ADMIN
* a) Specific IPs / IP blocks
* b) Cookie
* c) HTTP-HEADER
* d) client HTTPS certificate (recommended) - http://nategood.com/client-side-certificate-authentication-in-ngi
*/
function is_admin(string $name = ''): string
{
// "current-admin-name" | ""
if ($name) {
return $name === is_admin() ? $name : '';
}
\hb2\todo();
/*
$a = &HB::$CONFIG['.is_admin'];
if (null !== $a) {
return $a; // 99%
}
// if (! @HB::$CONFIG['is_admin'])
// return; // still initializing
if (\PHP_SAPI === 'cli') { // already set in HB::initCli
return $a = 'cli';
}
// $m = (string) C("is_admin.method", ""); // "Class::method"
$m = (string) @HB::$CONFIG['is_admin']['method']; // "Class::method" - is_admin can be called before CONFIG init
return $a = $m ? $m() : '';
*/
return ''; // php-stan
}
class TODO_Exception extends Error {}
function todo(string $str = ''): void
{
throw new TODO_Exception($str);
}
/**
* COLORED sprintf for (mostly) for CLI mode
*
* @ see i('cli')
* Ex:
* \hb\e("{red}{bold}Sample {bg_green}{white}$text{/}") << as is, no sprintf
* \hb\e("{red}{bold}Sample {bg_green}{white}%s{/}", $text) << use sprintf
*
* @param mixed $args
*/
function e(string $format, ...$args): void
{
// @todo("implement stylish array presentation");
if (\PHP_SAPI === 'cli') {
\hb2\todo();
// i('cli')->e($format."\n", ...$args);
return;
}
if (!is_admin()) {
return;
}
$text = $args ? sprintf($format, ...$args) : $format;
$text = preg_replace('!\{[\w\/]+\}!', ' ', $text);
echo "\n<div class=admin>{$text}</div>\n";
}
/**
* COLORED STDERR sprintf for CLI mode
* i(CLI) wrapper.
*
* @param mixed $args
*
*@see \hb2\e(..), i('cli')
* Ex:
* \hb\err("{red}{bold}Error Condition: $error{/}") << as is, no sprintf
* \hb\err("{red}{bold}Error Condition: %s{/}", $a) << use sprintf
*/
function err(string $format, ...$args): void
{
// STDERR
// @todo("implement stylish array presenation");
if (\PHP_SAPI === 'cli') {
\hb2\todo();
// i('cli')->err($format."\n", ...$args);
return;
}
if (!is_admin()) {
return;
}
// todo - add Profiler::error()
$text = $args ? sprintf($format, ...$args) : $format;
$text = preg_replace('!\{[\w\/]+\}!', ' ', $text);
echo "\n<div class=admin style='background: #f00; color: #fff'>$text</div>\n";
}
/**
* HTML Escape
* if $text is array - join it
*
* @param string|string[] $text
*/
function h(array|string $text): string
{
// escaped text
return htmlspecialchars(\is_array($text) ? implode('', $text) : $text, ENT_QUOTES, 'utf-8', false);
}
/**
* json_encode + default params
* "@json(...)"" - return "" on error (no exception)
*/
function json(mixed $data): string
{
$r = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
if ($r === false) {
if (isSuppressed()) {
return '';
}
error("can't make json");
}
return $r;
}
/**
* if value is a closure - resolve it.
*
* @param \Closure|mixed $value
*
* @return mixed
*/
function value($value)
{
// resolved Closure
return $value instanceof \Closure ? $value() : $value;
}
/**
* short function helper
* usage: $m = fn($a,$b) => \hb\then($acc+=$a, $b)
*
* @param mixed $a
* @param mixed $b
*
* @return mixed
*/
function then($a, $b)
{
return $b;
}
/**
* remove key from hash.
*
* @legacy
*
* @param mixed[] $hash
*
* @return mixed removed-value
*/
function hash_unset(array &$hash, string $key): mixed
{
$vl = $hash[$key] ?? null;
unset($hash[$key]);
return $vl;
}
/**
* Time-to-Live calculation. used by different cache implementations
* supported ttl: (int) seconds, [(int) seconds, (int) randomize-prc].
*
* @param int|int[] $ttl
*/
function ttl(array|int $ttl = [3600, 33]): int
{
// ttl .. ttl+rnd(%)
if (\is_array($ttl)) {
# /** @psalm-suppress RedundantConditionGivenDocblockType */
# error_unless(\is_int($ttl[0]), 'ttl-part must be int'); - gives psalm error
return $ttl[0] + random_int(0, (int) ($ttl[0] * $ttl[1] / 100));
}
return $ttl;
}
/**
* Build "<a href>" tag + escaping.
*
* @param string|string[] $url
* @param string|string[] $args_or_text
* @param string $html extra html
*
* @example a("url", ['param' => 'value'], "text") ; a("url", "text")
*/
function a(array|string $url, array|string $args_or_text = '', string $text = '', string $html = ''): string
{
// "<a href=.."
if (\is_array($args_or_text)) {
$url = url($url, $args_or_text); // args
} else {
error_if($text, 'cant combine string-args and text');
return a($url, [], $args_or_text, $html);
}
return "<a href=\"{$url}\"".($html ? ' '.$html : '').'>'.h($text).'</a>';
}
/**
* Build URL (Safe)
*
* @param string|string[] $url ; "url" or ["url", ...$args]
* @param array<string, int|string> $args
*/
function url(array|string $url, array $args = []): string
{
if (\is_array($url)) {
error_unless($url[0] ?? 0, 'expecting url as [0=>url, ...args]');
$u = $url[0];
unset($url[0]);
$args = $url + $args;
$url = $u;
}
// @todo $url is '@XXX' << use URL-aliaser
return $args ? $url.'?'.http_build_query($args) : $url;
}
/**
* somewhat DEPRECATED:
* use $a ?: "default" instead
* oracle NVL - first non empty value | null
* returns first-empty value or last-argument
* nvl($a, $b, "default");
* nvl($a, $b, "0") // return $a ? $a : ($b ? $b : "0");
*
* @param mixed $args
*/
function nvl(...$args): mixed
{
// non-empty-value | last-argument
if (\count($args) < 2) {
throw new Error('NVL(...) - 2+ args expected');
}
foreach ($args as $a) {
if ($a) {
return $a;
}
$l = $a;
}
return $l;
}
/**
* is value between $from .. $to (inclusive)
*/
function between(mixed $v, mixed $from, mixed $to): bool
{
return $v >= $from && $v <= $to;
}
/**
* benchmark function
*
* @param \Closure $fn [description]
* @param int $seconds [description]
* @param array|mixed $fn_params
*
* @return (float|int)[]
*
* @psalm-return array{'μs': float, count: 0|positive-int}
*/
function benchmark(\Closure $fn, int $seconds = 3, mixed $fn_params = []): array // [$time_per_iteration, iterations]
{
$start = microtime(true);
$end = $start + $seconds;
$cnt = 0;
while (microtime(true) < $end) {
$fn($fn_params);
$cnt++;
}
$end = microtime(true);
return ['μs' => round(($end - $start) / $cnt * 1000000, 1), 'count' => $cnt];
}
// is @method called
function isSuppressed(): bool
{
$t = error_reporting();
// as of php8 default suppressed reporting value is
// E_USER_NOTICE | E_ERROR | E_WARNING | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_USER_DEPRECATED
return 0 === $t || 4437 === $t;
}
/**
* non-recoverable Error - developer uses Code Incorrect Way
* throw \hb\Error exception if ...
*/
function error_if(mixed $boolean, string $message): void
{
if ($boolean) {
throw new Error($message); // \Error descendant
}
}
/**
* non recoverable Error - developer uses Code Incorrect Way
* throw \hb\Error exception if ...
*/
function error_unless(mixed $boolean, string $message): void
{
if (!$boolean) {
throw new Error($message); // \Error descendant
}
}
/**
* @return never
*/
function error(string $message): void
{
throw new Error($message); // \Error descendant
}
/**
* throw exception if ...
* Copied from laravel: https://laravel-news.com/throw_if-throw_unless
*
* @psalm-suppress InvalidThrow
*
* @param Exception|string $exception [description]
*
* @throws Exception
*/
function throw_if(mixed $boolean, Exception|string $exception, string $message = ''): void
{
if ($boolean) {
throw \is_string($exception) ? new $exception($message) : $exception;
}
}
/**
* throw exception if ...
* Copied from laravel: https://laravel-news.com/throw_if-throw_unless
*
* @psalm-suppress InvalidThrow
*
* @param Exception|string $exception [description]
* @param mixed $boolean
*
* @throws Exception
*/
function throw_unless($boolean, $exception, string $message = ''): void
{
if (!$boolean) {
throw \is_string($exception) ? new $exception($message) : $exception;
}
}