annotate vendor/psy/psysh/src/Psy/Command/WtfCommand.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of Psy Shell.
Chris@0 5 *
Chris@0 6 * (c) 2012-2017 Justin Hileman
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Psy\Command;
Chris@0 13
Chris@0 14 use Psy\Context;
Chris@0 15 use Psy\ContextAware;
Chris@0 16 use Psy\Input\FilterOptions;
Chris@0 17 use Psy\Output\ShellOutput;
Chris@0 18 use Symfony\Component\Console\Input\InputArgument;
Chris@0 19 use Symfony\Component\Console\Input\InputInterface;
Chris@0 20 use Symfony\Component\Console\Input\InputOption;
Chris@0 21 use Symfony\Component\Console\Output\OutputInterface;
Chris@0 22
Chris@0 23 /**
Chris@0 24 * Show the last uncaught exception.
Chris@0 25 */
Chris@0 26 class WtfCommand extends TraceCommand implements ContextAware
Chris@0 27 {
Chris@0 28 /**
Chris@0 29 * Context instance (for ContextAware interface).
Chris@0 30 *
Chris@0 31 * @var Context
Chris@0 32 */
Chris@0 33 protected $context;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * ContextAware interface.
Chris@0 37 *
Chris@0 38 * @param Context $context
Chris@0 39 */
Chris@0 40 public function setContext(Context $context)
Chris@0 41 {
Chris@0 42 $this->context = $context;
Chris@0 43 }
Chris@0 44
Chris@0 45 /**
Chris@0 46 * {@inheritdoc}
Chris@0 47 */
Chris@0 48 protected function configure()
Chris@0 49 {
Chris@0 50 list($grep, $insensitive, $invert) = FilterOptions::getOptions();
Chris@0 51
Chris@0 52 $this
Chris@0 53 ->setName('wtf')
Chris@0 54 ->setAliases(array('last-exception', 'wtf?'))
Chris@0 55 ->setDefinition(array(
Chris@0 56 new InputArgument('incredulity', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Number of lines to show'),
Chris@0 57 new InputOption('all', 'a', InputOption::VALUE_NONE, 'Show entire backtrace.'),
Chris@0 58
Chris@0 59 $grep,
Chris@0 60 $insensitive,
Chris@0 61 $invert,
Chris@0 62 ))
Chris@0 63 ->setDescription('Show the backtrace of the most recent exception.')
Chris@0 64 ->setHelp(
Chris@0 65 <<<'HELP'
Chris@0 66 Shows a few lines of the backtrace of the most recent exception.
Chris@0 67
Chris@0 68 If you want to see more lines, add more question marks or exclamation marks:
Chris@0 69
Chris@0 70 e.g.
Chris@0 71 <return>>>> wtf ?</return>
Chris@0 72 <return>>>> wtf ?!???!?!?</return>
Chris@0 73
Chris@0 74 To see the entire backtrace, pass the -a/--all flag:
Chris@0 75
Chris@0 76 e.g.
Chris@0 77 <return>>>> wtf -v</return>
Chris@0 78 HELP
Chris@0 79 );
Chris@0 80 }
Chris@0 81
Chris@0 82 /**
Chris@0 83 * {@inheritdoc}
Chris@0 84 */
Chris@0 85 protected function execute(InputInterface $input, OutputInterface $output)
Chris@0 86 {
Chris@0 87 $this->filter->bind($input);
Chris@0 88
Chris@0 89 $incredulity = implode('', $input->getArgument('incredulity'));
Chris@0 90 if (strlen(preg_replace('/[\\?!]/', '', $incredulity))) {
Chris@0 91 throw new \InvalidArgumentException('Incredulity must include only "?" and "!".');
Chris@0 92 }
Chris@0 93
Chris@0 94 $exception = $this->context->getLastException();
Chris@0 95 $count = $input->getOption('all') ? PHP_INT_MAX : max(3, pow(2, strlen($incredulity) + 1));
Chris@0 96
Chris@0 97 $shell = $this->getApplication();
Chris@0 98 $output->startPaging();
Chris@0 99 do {
Chris@0 100 $traceCount = count($exception->getTrace());
Chris@0 101 $showLines = $count;
Chris@0 102 // Show the whole trace if we'd only be hiding a few lines
Chris@0 103 if ($traceCount < max($count * 1.2, $count + 2)) {
Chris@0 104 $showLines = PHP_INT_MAX;
Chris@0 105 }
Chris@0 106
Chris@0 107 $trace = $this->getBacktrace($exception, $showLines);
Chris@0 108 $moreLines = $traceCount - count($trace);
Chris@0 109
Chris@0 110 $output->writeln($shell->formatException($exception));
Chris@0 111 $output->writeln('--');
Chris@0 112 $output->write($trace, true, ShellOutput::NUMBER_LINES);
Chris@0 113 $output->writeln('');
Chris@0 114
Chris@0 115 if ($moreLines > 0) {
Chris@0 116 $output->writeln(sprintf(
Chris@0 117 '<aside>Use <return>wtf -a</return> to see %d more lines</aside>',
Chris@0 118 $moreLines
Chris@0 119 ));
Chris@0 120 $output->writeln('');
Chris@0 121 }
Chris@0 122 } while ($exception = $exception->getPrevious());
Chris@0 123 $output->stopPaging();
Chris@0 124 }
Chris@0 125 }