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;
16 use Psy\Input\FilterOptions;
17 use Psy\Output\ShellOutput;
18 use Symfony\Component\Console\Input\InputArgument;
19 use Symfony\Component\Console\Input\InputInterface;
20 use Symfony\Component\Console\Input\InputOption;
21 use Symfony\Component\Console\Output\OutputInterface;
24 * Show the last uncaught exception.
26 class WtfCommand extends TraceCommand implements ContextAware
29 * Context instance (for ContextAware interface).
36 * ContextAware interface.
38 * @param Context $context
40 public function setContext(Context $context)
42 $this->context = $context;
48 protected function configure()
50 list($grep, $insensitive, $invert) = FilterOptions::getOptions();
54 ->setAliases(array('last-exception', 'wtf?'))
55 ->setDefinition(array(
56 new InputArgument('incredulity', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Number of lines to show'),
57 new InputOption('all', 'a', InputOption::VALUE_NONE, 'Show entire backtrace.'),
63 ->setDescription('Show the backtrace of the most recent exception.')
66 Shows a few lines of the backtrace of the most recent exception.
68 If you want to see more lines, add more question marks or exclamation marks:
71 <return>>>> wtf ?</return>
72 <return>>>> wtf ?!???!?!?</return>
74 To see the entire backtrace, pass the -a/--all flag:
77 <return>>>> wtf -v</return>
85 protected function execute(InputInterface $input, OutputInterface $output)
87 $this->filter->bind($input);
89 $incredulity = implode('', $input->getArgument('incredulity'));
90 if (strlen(preg_replace('/[\\?!]/', '', $incredulity))) {
91 throw new \InvalidArgumentException('Incredulity must include only "?" and "!".');
94 $exception = $this->context->getLastException();
95 $count = $input->getOption('all') ? PHP_INT_MAX : max(3, pow(2, strlen($incredulity) + 1));
97 $shell = $this->getApplication();
98 $output->startPaging();
100 $traceCount = count($exception->getTrace());
102 // Show the whole trace if we'd only be hiding a few lines
103 if ($traceCount < max($count * 1.2, $count + 2)) {
104 $showLines = PHP_INT_MAX;
107 $trace = $this->getBacktrace($exception, $showLines);
108 $moreLines = $traceCount - count($trace);
110 $output->writeln($shell->formatException($exception));
111 $output->writeln('--');
112 $output->write($trace, true, ShellOutput::NUMBER_LINES);
113 $output->writeln('');
115 if ($moreLines > 0) {
116 $output->writeln(sprintf(
117 '<aside>Use <return>wtf -a</return> to see %d more lines</aside>',
120 $output->writeln('');
122 } while ($exception = $exception->getPrevious());
123 $output->stopPaging();