|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class DebugBacktrace |
|
4
|
|
|
{ |
|
5
|
|
|
/** @var bool|string */ |
|
6
|
|
|
protected static $removePathPrefix = true; |
|
7
|
|
|
|
|
8
|
|
|
/** @var ?string */ |
|
9
|
|
|
protected static $callerRemoveFile = __FILE__; |
|
10
|
|
|
|
|
11
|
|
|
/** @param bool|string $remove */ |
|
12
|
|
|
public static function setRemovePathPrefix($remove) |
|
13
|
|
|
{ |
|
14
|
|
|
static::$removePathPrefix = $remove; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param int $offset |
|
19
|
|
|
* @param int|null $limit |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function getBacktraces($offset = 0, $limit = null) |
|
23
|
|
|
{ |
|
24
|
|
|
if ($limit !== null) { |
|
25
|
|
|
$limit += $offset; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$filteredBacktraces = []; |
|
29
|
|
|
foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $limit) as $dumpIndex => $backtrace) { |
|
30
|
|
|
if ($dumpIndex > $offset) { |
|
31
|
|
|
$filteredBacktrace = [ |
|
32
|
|
|
'file' => isset($backtrace['file']) ? $backtrace['file'] : null, |
|
33
|
|
|
'line' => isset($backtrace['line']) ? $backtrace['line'] : null, |
|
34
|
|
|
]; |
|
35
|
|
|
if (isset($backtrace['class'])) { |
|
36
|
|
|
$filteredBacktrace['call'] = |
|
37
|
|
|
$backtrace['class'] . $backtrace['type'] . $backtrace['function'] . '()'; |
|
38
|
|
|
} elseif (isset($backtrace['function'])) { |
|
39
|
|
|
$filteredBacktrace['call'] = $backtrace['function'] . '()'; |
|
40
|
|
|
} else { |
|
41
|
|
|
$filteredBacktrace['call'] = '\Closure'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$filteredBacktraces[] = $filteredBacktrace; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return array_reverse($filteredBacktraces); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** @return array|null */ |
|
52
|
|
|
public static function getCaller() |
|
53
|
|
|
{ |
|
54
|
|
|
$backtraces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 10); |
|
55
|
|
|
$nextIsCaller = false; |
|
56
|
|
|
$caller = null; |
|
57
|
|
|
foreach ($backtraces as $backtrace) { |
|
58
|
|
|
if ( |
|
59
|
|
|
isset($backtrace['file']) |
|
60
|
|
|
&& strpos($backtrace['file'], static::$callerRemoveFile) !== false |
|
61
|
|
|
) { |
|
62
|
|
|
$nextIsCaller = true; |
|
63
|
|
|
} elseif ( |
|
64
|
|
|
$nextIsCaller |
|
65
|
|
|
&& ( |
|
66
|
|
|
(isset($backtrace['file']) && strpos($backtrace['file'], static::$callerRemoveFile) === false) |
|
67
|
|
|
|| isset($backtrace['file']) === false |
|
68
|
|
|
) |
|
69
|
|
|
) { |
|
70
|
|
|
$caller = $backtrace; |
|
71
|
|
|
break; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
if ($nextIsCaller === false && count($backtraces) > 0) { |
|
75
|
|
|
$caller = $backtraces[0]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $caller; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $code |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
protected static function highlightCode($code) |
|
86
|
|
|
{ |
|
87
|
|
|
$highlight = highlight_string('<?php ' . $code, true); |
|
88
|
|
|
$highlight = str_replace('><?php ', null, $highlight); |
|
89
|
|
|
|
|
90
|
|
|
return $highlight; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $path |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
protected static function getFilePath($path) |
|
98
|
|
|
{ |
|
99
|
|
|
$path = realpath($path); |
|
100
|
|
|
|
|
101
|
|
|
if (static::$removePathPrefix === false) { |
|
102
|
|
|
$return = $path; |
|
103
|
|
|
} else { |
|
104
|
|
|
// assume that we are in vendor/ dir |
|
105
|
|
|
$prefix = (static::$removePathPrefix === true) |
|
106
|
|
|
? realpath(__DIR__ . '/../../../') |
|
107
|
|
|
: static::$removePathPrefix; |
|
108
|
|
|
$return = (substr($path, 0, strlen($prefix)) === $prefix) ? substr($path, strlen($prefix) + 1) : $path; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $return; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|