1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
4
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
5
|
|
|
use Symfony\Component\Console\Helper\Table; |
6
|
|
|
|
7
|
|
|
class DebugBacktraceConsole extends \DebugBacktrace |
8
|
|
|
{ |
9
|
|
|
/** @var ?string */ |
10
|
|
|
protected static $callerRemoveFile = __FILE__; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param OutputInterface $output |
14
|
|
|
* @param int $offset |
15
|
|
|
* @param int|null $limit |
16
|
|
|
*/ |
17
|
|
|
public static function dump(OutputInterface $output = null, $offset = 0, $limit = null) |
18
|
|
|
{ |
19
|
|
|
$output = static::getOutput($output); |
|
|
|
|
20
|
|
|
|
21
|
|
|
static::writeCaller($output); |
22
|
|
|
|
23
|
|
|
$table = new Table($output); |
24
|
|
|
$table->setHeaders(['#', 'file', 'line', 'call']); |
25
|
|
|
foreach (static::getBacktraces($offset, $limit) as $index => $backtrace) { |
26
|
|
|
static::writeBacktraceDump($table, $backtrace, $index); |
27
|
|
|
} |
28
|
|
|
$table->render(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param OutputInterface $output |
33
|
|
|
* @param int $offset |
34
|
|
|
* @param int|null $limit |
35
|
|
|
*/ |
36
|
|
|
public static function eDump(OutputInterface $output = null, $offset = 0, $limit = null) |
37
|
|
|
{ |
38
|
|
|
static::dump($output, $offset + 1, $limit); |
39
|
|
|
exit(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Table $table |
44
|
|
|
* @param array $backtrace |
45
|
|
|
* @param int $index |
46
|
|
|
*/ |
47
|
|
|
protected static function writeBacktraceDump(Table $table, array $backtrace, $index) |
48
|
|
|
{ |
49
|
|
|
$table->addRow([ |
50
|
|
|
$index + 1, |
51
|
|
|
static::getFilePath($backtrace['file']), |
52
|
|
|
$backtrace['line'], |
53
|
|
|
$backtrace['call'] |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** @param OutputInterface $output */ |
58
|
|
|
protected static function writeCaller(OutputInterface $output) |
59
|
|
|
{ |
60
|
|
|
$caller = static::getCaller(); |
61
|
|
|
|
62
|
|
|
if (is_array($caller)) { |
63
|
|
|
$output->writeln( |
64
|
|
|
'From ' |
65
|
|
|
. '<info>' . $caller['file'] . '</info>' |
66
|
|
|
. '<comment>#' . $caller['line'] . '</comment>' |
67
|
|
|
); |
68
|
|
|
} else { |
69
|
|
|
$output->writeln('<error>Unkonw caller</error>'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** @eturn OutputInterface */ |
74
|
|
|
protected static function getOutput(OutputInterface $output) |
75
|
|
|
{ |
76
|
|
|
return $output instanceof OutputInterface ? $output : new ConsoleOutput(); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: