1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Utility; |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | final class Debug extends AbstractUtility |
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: | public static function getCaller(int $depth = 0): array |
53: | { |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | |
60: | $frames = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, $depth + 3); |
61: | $file = $frames[$depth + 1]['file'] ?? null; |
62: | $line = $frames[$depth + 1]['line'] ?? null; |
63: | $beforeLine = $line !== null ? ':' : null; |
64: | |
65: | if (isset($frames[$depth + 2]['function'])) { |
66: | $frame = $frames[$depth + 2]; |
67: | if (isset($frame['class'])) { |
68: | $namespace = Get::namespace($frame['class']); |
69: | $class = Get::basename($frame['class']); |
70: | } else { |
71: | $namespace = Get::namespace($frame['function']); |
72: | $class = ''; |
73: | } |
74: | |
75: | |
76: | $function = Get::basename($frame['function']); |
77: | if ($namespace !== '') { |
78: | $namespace .= '\\'; |
79: | } |
80: | if ($class !== '' || $namespace !== '') { |
81: | $file = null; |
82: | } |
83: | return Arr::whereNotEmpty([ |
84: | 'namespace' => $namespace, |
85: | 'class' => $class, |
86: | 'file' => $file, |
87: | $frame['type'] ?? ($file !== null ? '::' : null), |
88: | 'function' => $function, |
89: | $beforeLine, |
90: | 'line' => $line, |
91: | ]); |
92: | } |
93: | |
94: | if (isset($frames[$depth + 1])) { |
95: | return Arr::whereNotEmpty([ |
96: | 'file' => $file, |
97: | $beforeLine, |
98: | 'line' => $line, |
99: | ]); |
100: | } |
101: | |
102: | return []; |
103: | } |
104: | } |
105: | |