Chris@13
|
1 <?php
|
Chris@13
|
2
|
Chris@13
|
3 /*
|
Chris@13
|
4 * This file is part of Psy Shell.
|
Chris@13
|
5 *
|
Chris@13
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@13
|
7 *
|
Chris@13
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@13
|
9 * file that was distributed with this source code.
|
Chris@13
|
10 */
|
Chris@13
|
11
|
Chris@13
|
12 namespace Psy\Command;
|
Chris@13
|
13
|
Chris@13
|
14 use Psy\Input\FilterOptions;
|
Chris@13
|
15 use Psy\Output\ShellOutput;
|
Chris@13
|
16 use Symfony\Component\Console\Formatter\OutputFormatter;
|
Chris@13
|
17 use Symfony\Component\Console\Input\InputInterface;
|
Chris@13
|
18 use Symfony\Component\Console\Input\InputOption;
|
Chris@13
|
19 use Symfony\Component\Console\Output\OutputInterface;
|
Chris@13
|
20
|
Chris@13
|
21 /**
|
Chris@13
|
22 * Show the current stack trace.
|
Chris@13
|
23 */
|
Chris@13
|
24 class TraceCommand extends Command
|
Chris@13
|
25 {
|
Chris@13
|
26 protected $filter;
|
Chris@13
|
27
|
Chris@13
|
28 /**
|
Chris@13
|
29 * {@inheritdoc}
|
Chris@13
|
30 */
|
Chris@13
|
31 public function __construct($name = null)
|
Chris@13
|
32 {
|
Chris@13
|
33 $this->filter = new FilterOptions();
|
Chris@13
|
34
|
Chris@13
|
35 parent::__construct($name);
|
Chris@13
|
36 }
|
Chris@13
|
37
|
Chris@13
|
38 /**
|
Chris@13
|
39 * {@inheritdoc}
|
Chris@13
|
40 */
|
Chris@13
|
41 protected function configure()
|
Chris@13
|
42 {
|
Chris@13
|
43 list($grep, $insensitive, $invert) = FilterOptions::getOptions();
|
Chris@13
|
44
|
Chris@13
|
45 $this
|
Chris@13
|
46 ->setName('trace')
|
Chris@13
|
47 ->setDefinition([
|
Chris@13
|
48 new InputOption('include-psy', 'p', InputOption::VALUE_NONE, 'Include Psy in the call stack.'),
|
Chris@13
|
49 new InputOption('num', 'n', InputOption::VALUE_REQUIRED, 'Only include NUM lines.'),
|
Chris@13
|
50
|
Chris@13
|
51 $grep,
|
Chris@13
|
52 $insensitive,
|
Chris@13
|
53 $invert,
|
Chris@13
|
54 ])
|
Chris@13
|
55 ->setDescription('Show the current call stack.')
|
Chris@13
|
56 ->setHelp(
|
Chris@13
|
57 <<<'HELP'
|
Chris@13
|
58 Show the current call stack.
|
Chris@13
|
59
|
Chris@13
|
60 Optionally, include PsySH in the call stack by passing the <info>--include-psy</info> option.
|
Chris@13
|
61
|
Chris@13
|
62 e.g.
|
Chris@13
|
63 <return>> trace -n10</return>
|
Chris@13
|
64 <return>> trace --include-psy</return>
|
Chris@13
|
65 HELP
|
Chris@13
|
66 );
|
Chris@13
|
67 }
|
Chris@13
|
68
|
Chris@13
|
69 /**
|
Chris@13
|
70 * {@inheritdoc}
|
Chris@13
|
71 */
|
Chris@13
|
72 protected function execute(InputInterface $input, OutputInterface $output)
|
Chris@13
|
73 {
|
Chris@13
|
74 $this->filter->bind($input);
|
Chris@13
|
75 $trace = $this->getBacktrace(new \Exception(), $input->getOption('num'), $input->getOption('include-psy'));
|
Chris@13
|
76 $output->page($trace, ShellOutput::NUMBER_LINES);
|
Chris@13
|
77 }
|
Chris@13
|
78
|
Chris@13
|
79 /**
|
Chris@13
|
80 * Get a backtrace for an exception.
|
Chris@13
|
81 *
|
Chris@13
|
82 * Optionally limit the number of rows to include with $count, and exclude
|
Chris@13
|
83 * Psy from the trace.
|
Chris@13
|
84 *
|
Chris@13
|
85 * @param \Exception $e The exception with a backtrace
|
Chris@13
|
86 * @param int $count (default: PHP_INT_MAX)
|
Chris@13
|
87 * @param bool $includePsy (default: true)
|
Chris@13
|
88 *
|
Chris@13
|
89 * @return array Formatted stacktrace lines
|
Chris@13
|
90 */
|
Chris@13
|
91 protected function getBacktrace(\Exception $e, $count = null, $includePsy = true)
|
Chris@13
|
92 {
|
Chris@17
|
93 if ($cwd = \getcwd()) {
|
Chris@17
|
94 $cwd = \rtrim($cwd, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
|
Chris@13
|
95 }
|
Chris@13
|
96
|
Chris@13
|
97 if ($count === null) {
|
Chris@13
|
98 $count = PHP_INT_MAX;
|
Chris@13
|
99 }
|
Chris@13
|
100
|
Chris@13
|
101 $lines = [];
|
Chris@13
|
102
|
Chris@13
|
103 $trace = $e->getTrace();
|
Chris@17
|
104 \array_unshift($trace, [
|
Chris@13
|
105 'function' => '',
|
Chris@13
|
106 'file' => $e->getFile() !== null ? $e->getFile() : 'n/a',
|
Chris@13
|
107 'line' => $e->getLine() !== null ? $e->getLine() : 'n/a',
|
Chris@13
|
108 'args' => [],
|
Chris@13
|
109 ]);
|
Chris@13
|
110
|
Chris@13
|
111 if (!$includePsy) {
|
Chris@17
|
112 for ($i = \count($trace) - 1; $i >= 0; $i--) {
|
Chris@13
|
113 $thing = isset($trace[$i]['class']) ? $trace[$i]['class'] : $trace[$i]['function'];
|
Chris@17
|
114 if (\preg_match('/\\\\?Psy\\\\/', $thing)) {
|
Chris@17
|
115 $trace = \array_slice($trace, $i + 1);
|
Chris@13
|
116 break;
|
Chris@13
|
117 }
|
Chris@13
|
118 }
|
Chris@13
|
119 }
|
Chris@13
|
120
|
Chris@17
|
121 for ($i = 0, $count = \min($count, \count($trace)); $i < $count; $i++) {
|
Chris@13
|
122 $class = isset($trace[$i]['class']) ? $trace[$i]['class'] : '';
|
Chris@13
|
123 $type = isset($trace[$i]['type']) ? $trace[$i]['type'] : '';
|
Chris@13
|
124 $function = $trace[$i]['function'];
|
Chris@13
|
125 $file = isset($trace[$i]['file']) ? $this->replaceCwd($cwd, $trace[$i]['file']) : 'n/a';
|
Chris@13
|
126 $line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a';
|
Chris@13
|
127
|
Chris@13
|
128 // Leave execution loop out of the `eval()'d code` lines
|
Chris@17
|
129 if (\preg_match("#/src/Execution(?:Loop)?Closure.php\(\d+\) : eval\(\)'d code$#", \str_replace('\\', '/', $file))) {
|
Chris@13
|
130 $file = "eval()'d code";
|
Chris@13
|
131 }
|
Chris@13
|
132
|
Chris@13
|
133 // Skip any lines that don't match our filter options
|
Chris@17
|
134 if (!$this->filter->match(\sprintf('%s%s%s() at %s:%s', $class, $type, $function, $file, $line))) {
|
Chris@13
|
135 continue;
|
Chris@13
|
136 }
|
Chris@13
|
137
|
Chris@17
|
138 $lines[] = \sprintf(
|
Chris@13
|
139 ' <class>%s</class>%s%s() at <info>%s:%s</info>',
|
Chris@13
|
140 OutputFormatter::escape($class),
|
Chris@13
|
141 OutputFormatter::escape($type),
|
Chris@13
|
142 OutputFormatter::escape($function),
|
Chris@13
|
143 OutputFormatter::escape($file),
|
Chris@13
|
144 OutputFormatter::escape($line)
|
Chris@13
|
145 );
|
Chris@13
|
146 }
|
Chris@13
|
147
|
Chris@13
|
148 return $lines;
|
Chris@13
|
149 }
|
Chris@13
|
150
|
Chris@13
|
151 /**
|
Chris@13
|
152 * Replace the given directory from the start of a filepath.
|
Chris@13
|
153 *
|
Chris@13
|
154 * @param string $cwd
|
Chris@13
|
155 * @param string $file
|
Chris@13
|
156 *
|
Chris@13
|
157 * @return string
|
Chris@13
|
158 */
|
Chris@13
|
159 private function replaceCwd($cwd, $file)
|
Chris@13
|
160 {
|
Chris@13
|
161 if ($cwd === false) {
|
Chris@13
|
162 return $file;
|
Chris@13
|
163 } else {
|
Chris@17
|
164 return \preg_replace('/^' . \preg_quote($cwd, '/') . '/', '', $file);
|
Chris@13
|
165 }
|
Chris@13
|
166 }
|
Chris@13
|
167 }
|