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\Output\ShellOutput;
17 use Symfony\Component\Console\Input\InputArgument;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Input\InputOption;
20 use Symfony\Component\Console\Output\OutputInterface;
23 * Show the last uncaught exception.
25 class WtfCommand extends TraceCommand implements ContextAware
28 * Context instance (for ContextAware interface).
35 * ContextAware interface.
37 * @param Context $context
39 public function setContext(Context $context)
41 $this->context = $context;
47 protected function configure()
51 ->setAliases(array('last-exception', 'wtf?'))
52 ->setDefinition(array(
53 new InputArgument('incredulity', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Number of lines to show'),
54 new InputOption('verbose', 'v', InputOption::VALUE_NONE, 'Show entire backtrace.'),
56 ->setDescription('Show the backtrace of the most recent exception.')
59 Shows a few lines of the backtrace of the most recent exception.
61 If you want to see more lines, add more question marks or exclamation marks:
64 <return>>>> wtf ?</return>
65 <return>>>> wtf ?!???!?!?</return>
67 To see the entire backtrace, pass the -v/--verbose flag:
70 <return>>>> wtf -v</return>
78 * --verbose is not hidden for this option :)
82 protected function getHiddenOptions()
84 $options = parent::getHiddenOptions();
85 unset($options[array_search('verbose', $options)]);
93 protected function execute(InputInterface $input, OutputInterface $output)
95 $incredulity = implode('', $input->getArgument('incredulity'));
96 if (strlen(preg_replace('/[\\?!]/', '', $incredulity))) {
97 throw new \InvalidArgumentException('Incredulity must include only "?" and "!".');
100 $exception = $this->context->getLastException();
101 $count = $input->getOption('verbose') ? PHP_INT_MAX : pow(2, max(0, (strlen($incredulity) - 1)));
102 $trace = $this->getBacktrace($exception, $count);
104 $shell = $this->getApplication();
105 $output->page(function ($output) use ($exception, $trace, $shell) {
106 $shell->renderException($exception, $output);
107 $output->writeln('--');
108 $output->write($trace, true, ShellOutput::NUMBER_LINES);