Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
6
 * (c) 2012-2022 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\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;
22
 
23
/**
24
 * Show the last uncaught exception.
25
 */
26
class WtfCommand extends TraceCommand implements ContextAware
27
{
28
    /**
29
     * Context instance (for ContextAware interface).
30
     *
31
     * @var Context
32
     */
33
    protected $context;
34
 
35
    /**
36
     * ContextAware interface.
37
     *
38
     * @param Context $context
39
     */
40
    public function setContext(Context $context)
41
    {
42
        $this->context = $context;
43
    }
44
 
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function configure()
49
    {
50
        list($grep, $insensitive, $invert) = FilterOptions::getOptions();
51
 
52
        $this
53
            ->setName('wtf')
54
            ->setAliases(['last-exception', 'wtf?'])
55
            ->setDefinition([
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.'),
58
 
59
                $grep,
60
                $insensitive,
61
                $invert,
62
            ])
63
            ->setDescription('Show the backtrace of the most recent exception.')
64
            ->setHelp(
65
                <<<'HELP'
66
Shows a few lines of the backtrace of the most recent exception.
67
 
68
If you want to see more lines, add more question marks or exclamation marks:
69
 
70
e.g.
71
<return>>>> wtf ?</return>
72
<return>>>> wtf ?!???!?!?</return>
73
 
74
To see the entire backtrace, pass the -a/--all flag:
75
 
76
e.g.
77
<return>>>> wtf -a</return>
78
HELP
79
            );
80
    }
81
 
82
    /**
83
     * {@inheritdoc}
84
     *
85
     * @return int 0 if everything went fine, or an exit code
86
     */
87
    protected function execute(InputInterface $input, OutputInterface $output)
88
    {
89
        $this->filter->bind($input);
90
 
91
        $incredulity = \implode('', $input->getArgument('incredulity'));
92
        if (\strlen(\preg_replace('/[\\?!]/', '', $incredulity))) {
93
            throw new \InvalidArgumentException('Incredulity must include only "?" and "!"');
94
        }
95
 
96
        $exception = $this->context->getLastException();
97
        $count = $input->getOption('all') ? \PHP_INT_MAX : \max(3, \pow(2, \strlen($incredulity) + 1));
98
 
99
        $shell = $this->getApplication();
100
 
101
        if ($output instanceof ShellOutput) {
102
            $output->startPaging();
103
        }
104
 
105
        do {
106
            $traceCount = \count($exception->getTrace());
107
            $showLines = $count;
108
            // Show the whole trace if we'd only be hiding a few lines
109
            if ($traceCount < \max($count * 1.2, $count + 2)) {
110
                $showLines = \PHP_INT_MAX;
111
            }
112
 
113
            $trace = $this->getBacktrace($exception, $showLines);
114
            $moreLines = $traceCount - \count($trace);
115
 
116
            $output->writeln($shell->formatException($exception));
117
            $output->writeln('--');
118
            $output->write($trace, true, ShellOutput::NUMBER_LINES);
119
            $output->writeln('');
120
 
121
            if ($moreLines > 0) {
122
                $output->writeln(\sprintf(
123
                    '<aside>Use <return>wtf -a</return> to see %d more lines</aside>',
124
                    $moreLines
125
                ));
126
                $output->writeln('');
127
            }
128
        } while ($exception = $exception->getPrevious());
129
 
130
        if ($output instanceof ShellOutput) {
131
            $output->stopPaging();
132
        }
133
 
134
        return 0;
135
    }
136
}