Skip to content

Commit c74836b

Browse files
committed
Split \DumpBacktrace in 3 classes : \DebugBacktrace, \DebugBacktraceHtml and \DebugBacktraceConsole
1 parent a664200 commit c74836b

File tree

8 files changed

+231
-145
lines changed

8 files changed

+231
-145
lines changed

DebugBacktrace.php

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

DebugBacktraceConsole.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
use \Symfony\Component\Console\Output\OutputInterface;
4+
use \Symfony\Component\Console\Helper\Table;
5+
6+
class DebugBacktraceConsole extends \DebugBacktrace
7+
{
8+
/**
9+
* @param OutputInterface $output
10+
* @param int $offset
11+
* @param int|null $limit
12+
*/
13+
public static function dump(OutputInterface $output, $offset = 0, $limit = null)
14+
{
15+
static::writeCaller($output);
16+
17+
$table = new Table($output);
18+
$table->setHeaders(['#', 'file', 'line', 'call']);
19+
foreach (static::getBacktraces($offset, $limit) as $index => $backtrace) {
20+
static::writeBacktraceDump($table, $backtrace, $index);
21+
}
22+
$table->render();
23+
}
24+
25+
/**
26+
* @param OutputInterface $output
27+
* @param int $offset
28+
* @param int|null $limit
29+
*/
30+
public static function eDump(OutputInterface $output, $offset = 0, $limit = null)
31+
{
32+
static::dump($output, $offset + 1, $limit);
33+
exit();
34+
}
35+
36+
/**
37+
* @param Table $table
38+
* @param array $backtrace
39+
* @param int $index
40+
*/
41+
protected static function writeBacktraceDump(Table $table, array $backtrace, $index)
42+
{
43+
$table->addRow([
44+
$index + 1,
45+
static::getFilePath($backtrace['file']),
46+
$backtrace['line'],
47+
$backtrace['call']
48+
]);
49+
}
50+
51+
/** @param OutputInterface $output */
52+
protected static function writeCaller(OutputInterface $output)
53+
{
54+
$caller = static::getCaller();
55+
56+
if (is_array($caller)) {
57+
$output->writeln(
58+
'From '
59+
. '<info>' . $caller['file'] . '</info>'
60+
. '<comment>#' . $caller['line'] . '</comment>'
61+
);
62+
} else {
63+
$output->writeln('<error>Unkonw caller</error>');
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)