7e3d08d1f7520fe109faa0e5d8938ed769601b7f
[yaffs-website] / vendor / psy / psysh / src / Psy / Command / WtfCommand.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\Command;
13
14 use Psy\Context;
15 use Psy\ContextAware;
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;
21
22 /**
23  * Show the last uncaught exception.
24  */
25 class WtfCommand extends TraceCommand implements ContextAware
26 {
27     /**
28      * Context instance (for ContextAware interface).
29      *
30      * @var Context
31      */
32     protected $context;
33
34     /**
35      * ContextAware interface.
36      *
37      * @param Context $context
38      */
39     public function setContext(Context $context)
40     {
41         $this->context = $context;
42     }
43
44     /**
45      * {@inheritdoc}
46      */
47     protected function configure()
48     {
49         $this
50             ->setName('wtf')
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.'),
55             ))
56             ->setDescription('Show the backtrace of the most recent exception.')
57             ->setHelp(
58                 <<<'HELP'
59 Shows a few lines of the backtrace of the most recent exception.
60
61 If you want to see more lines, add more question marks or exclamation marks:
62
63 e.g.
64 <return>>>> wtf ?</return>
65 <return>>>> wtf ?!???!?!?</return>
66
67 To see the entire backtrace, pass the -v/--verbose flag:
68
69 e.g.
70 <return>>>> wtf -v</return>
71 HELP
72             );
73     }
74
75     /**
76      * {@inheritdoc}
77      *
78      * --verbose is not hidden for this option :)
79      *
80      * @return array
81      */
82     protected function getHiddenOptions()
83     {
84         $options = parent::getHiddenOptions();
85         unset($options[array_search('verbose', $options)]);
86
87         return $options;
88     }
89
90     /**
91      * {@inheritdoc}
92      */
93     protected function execute(InputInterface $input, OutputInterface $output)
94     {
95         $incredulity = implode('', $input->getArgument('incredulity'));
96         if (strlen(preg_replace('/[\\?!]/', '', $incredulity))) {
97             throw new \InvalidArgumentException('Incredulity must include only "?" and "!".');
98         }
99
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);
103
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);
109         });
110     }
111 }