Skip to content

Commit a9c9380

Browse files
committed
add new func and methods
1 parent b919656 commit a9c9380

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
"autoload": {
2727
"psr-4": {
2828
"Toolkit\\Stdlib\\": "src/"
29-
}
29+
},
30+
"files": [
31+
"src/func.php"
32+
]
3033
},
3134
"suggest": {
3235
"inhere/php-validate": "Very lightweight data validate tool"

src/Helper/PhpHelper.php

+25
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,31 @@ public static function runtime($startTime, $startMem, array $info = [], $realUsa
171171
return $info;
172172
}
173173

174+
/**
175+
* Usage:
176+
*
177+
* $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
178+
* $position = PhpHelper::formatBacktrace($backtrace, 2);
179+
*
180+
* @param array $traces
181+
* @param int $index
182+
*
183+
* @return string
184+
*/
185+
public static function formatBacktrace(array $traces, int $index): string
186+
{
187+
$position = 'UNKNOWN';
188+
189+
if (isset($traces[$index+1])) {
190+
$tInfo = $traces[$index];
191+
$prev = $traces[$index+1];
192+
193+
$position = sprintf('%s.%s:%d', $prev['class'], $prev['function'] ?? 'UNKNOWN', $tInfo['line']);
194+
}
195+
196+
return $position;
197+
}
198+
174199
/**
175200
* dump vars
176201
*

src/func.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
if (!function_exists('vdump')) {
4+
/**
5+
* Dump data like var_dump
6+
*
7+
* @param mixed ...$vars
8+
*/
9+
function vdump(...$vars)
10+
{
11+
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
12+
13+
$line = $trace[0]['line'];
14+
$pos = $trace[1]['class'] ?? $trace[0]['file'];
15+
16+
if ($pos) {
17+
echo "CALL ON $pos($line):\n";
18+
}
19+
20+
echo Toolkit\Stdlib\Php::dumpVars(...$vars), PHP_EOL;
21+
}
22+
}
23+
24+
if (!function_exists('ddump')) {
25+
/**
26+
* Dump data like var_dump, will call exit on print after.
27+
*
28+
* @param mixed ...$vars
29+
*/
30+
function ddump(...$vars)
31+
{
32+
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
33+
34+
$line = $trace[0]['line'];
35+
$pos = $trace[1]['class'] ?? $trace[0]['file'];
36+
37+
if ($pos) {
38+
echo "CALL ON $pos($line):\n";
39+
}
40+
41+
echo Toolkit\Stdlib\Php::dumpVars(...$vars), PHP_EOL;
42+
exit(0);
43+
}
44+
}

0 commit comments

Comments
 (0)