4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Command;
14 use Psy\Output\ShellOutput;
15 use Symfony\Component\Console\Formatter\OutputFormatter;
16 use Symfony\Component\Console\Input\InputInterface;
17 use Symfony\Component\Console\Input\InputOption;
18 use Symfony\Component\Console\Output\OutputInterface;
21 * Show the current stack trace.
23 class TraceCommand extends Command
28 protected function configure()
32 ->setDefinition(array(
33 new InputOption('include-psy', 'p', InputOption::VALUE_NONE, 'Include Psy in the call stack.'),
34 new InputOption('num', 'n', InputOption::VALUE_REQUIRED, 'Only include NUM lines.'),
36 ->setDescription('Show the current call stack.')
39 Show the current call stack.
41 Optionally, include PsySH in the call stack by passing the <info>--include-psy</info> option.
44 <return>> trace -n10</return>
45 <return>> trace --include-psy</return>
53 protected function execute(InputInterface $input, OutputInterface $output)
55 $trace = $this->getBacktrace(new \Exception(), $input->getOption('num'), $input->getOption('include-psy'));
56 $output->page($trace, ShellOutput::NUMBER_LINES);
60 * Get a backtrace for an exception.
62 * Optionally limit the number of rows to include with $count, and exclude
65 * @param \Exception $e The exception with a backtrace
66 * @param int $count (default: PHP_INT_MAX)
67 * @param bool $includePsy (default: true)
69 * @return array Formatted stacktrace lines
71 protected function getBacktrace(\Exception $e, $count = null, $includePsy = true)
73 if ($cwd = getcwd()) {
74 $cwd = rtrim($cwd, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
77 if ($count === null) {
83 $trace = $e->getTrace();
84 array_unshift($trace, array(
86 'file' => $e->getFile() !== null ? $e->getFile() : 'n/a',
87 'line' => $e->getLine() !== null ? $e->getLine() : 'n/a',
92 for ($i = count($trace) - 1; $i >= 0; $i--) {
93 $thing = isset($trace[$i]['class']) ? $trace[$i]['class'] : $trace[$i]['function'];
94 if (preg_match('/\\\\?Psy\\\\/', $thing)) {
95 $trace = array_slice($trace, $i + 1);
101 for ($i = 0, $count = min($count, count($trace)); $i < $count; $i++) {
102 $class = isset($trace[$i]['class']) ? $trace[$i]['class'] : '';
103 $type = isset($trace[$i]['type']) ? $trace[$i]['type'] : '';
104 $function = $trace[$i]['function'];
105 $file = isset($trace[$i]['file']) ? $this->replaceCwd($cwd, $trace[$i]['file']) : 'n/a';
106 $line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a';
109 ' <class>%s</class>%s%s() at <info>%s:%s</info>',
110 OutputFormatter::escape($class),
111 OutputFormatter::escape($type),
112 OutputFormatter::escape($function),
113 OutputFormatter::escape($file),
114 OutputFormatter::escape($line)
122 * Replace the given directory from the start of a filepath.
125 * @param string $file
129 private function replaceCwd($cwd, $file)
131 if ($cwd === false) {
134 return preg_replace('/^' . preg_quote($cwd, '/') . '/', '', $file);