| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of Psy Shell.
|
|
|
5 |
*
|
|
|
6 |
* (c) 2012-2022 Justin Hileman
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Psy\Command;
|
|
|
13 |
|
|
|
14 |
use Psy\Formatter\TraceFormatter;
|
|
|
15 |
use Psy\Input\FilterOptions;
|
|
|
16 |
use Psy\Output\ShellOutput;
|
|
|
17 |
use Symfony\Component\Console\Input\InputInterface;
|
|
|
18 |
use Symfony\Component\Console\Input\InputOption;
|
|
|
19 |
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Show the current stack trace.
|
|
|
23 |
*/
|
|
|
24 |
class TraceCommand extends Command
|
|
|
25 |
{
|
|
|
26 |
protected $filter;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* {@inheritdoc}
|
|
|
30 |
*/
|
|
|
31 |
public function __construct($name = null)
|
|
|
32 |
{
|
|
|
33 |
$this->filter = new FilterOptions();
|
|
|
34 |
|
|
|
35 |
parent::__construct($name);
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* {@inheritdoc}
|
|
|
40 |
*/
|
|
|
41 |
protected function configure()
|
|
|
42 |
{
|
|
|
43 |
list($grep, $insensitive, $invert) = FilterOptions::getOptions();
|
|
|
44 |
|
|
|
45 |
$this
|
|
|
46 |
->setName('trace')
|
|
|
47 |
->setDefinition([
|
|
|
48 |
new InputOption('include-psy', 'p', InputOption::VALUE_NONE, 'Include Psy in the call stack.'),
|
|
|
49 |
new InputOption('num', 'n', InputOption::VALUE_REQUIRED, 'Only include NUM lines.'),
|
|
|
50 |
|
|
|
51 |
$grep,
|
|
|
52 |
$insensitive,
|
|
|
53 |
$invert,
|
|
|
54 |
])
|
|
|
55 |
->setDescription('Show the current call stack.')
|
|
|
56 |
->setHelp(
|
|
|
57 |
<<<'HELP'
|
|
|
58 |
Show the current call stack.
|
|
|
59 |
|
|
|
60 |
Optionally, include PsySH in the call stack by passing the <info>--include-psy</info> option.
|
|
|
61 |
|
|
|
62 |
e.g.
|
|
|
63 |
<return>> trace -n10</return>
|
|
|
64 |
<return>> trace --include-psy</return>
|
|
|
65 |
HELP
|
|
|
66 |
);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* {@inheritdoc}
|
|
|
71 |
*
|
|
|
72 |
* @return int 0 if everything went fine, or an exit code
|
|
|
73 |
*/
|
|
|
74 |
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
75 |
{
|
|
|
76 |
$this->filter->bind($input);
|
|
|
77 |
$trace = $this->getBacktrace(new \Exception(), $input->getOption('num'), $input->getOption('include-psy'));
|
|
|
78 |
$output->page($trace, ShellOutput::NUMBER_LINES);
|
|
|
79 |
|
|
|
80 |
return 0;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Get a backtrace for an exception or error.
|
|
|
85 |
*
|
|
|
86 |
* Optionally limit the number of rows to include with $count, and exclude
|
|
|
87 |
* Psy from the trace.
|
|
|
88 |
*
|
|
|
89 |
* @param \Throwable $e The exception or error with a backtrace
|
|
|
90 |
* @param int $count (default: PHP_INT_MAX)
|
|
|
91 |
* @param bool $includePsy (default: true)
|
|
|
92 |
*
|
|
|
93 |
* @return array Formatted stacktrace lines
|
|
|
94 |
*/
|
|
|
95 |
protected function getBacktrace(\Throwable $e, int $count = null, bool $includePsy = true): array
|
|
|
96 |
{
|
|
|
97 |
return TraceFormatter::formatTrace($e, $this->filter, $count, $includePsy);
|
|
|
98 |
}
|
|
|
99 |
}
|