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\Formatter\CodeFormatter;
15
use Psy\Output\ShellOutput;
16
use Psy\Shell;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
 
21
/**
22
 * Show the context of where you opened the debugger.
23
 */
24
class WhereamiCommand extends Command
25
{
26
    private $backtrace;
27
 
28
    /**
29
     * @param string|null $colorMode (deprecated and ignored)
30
     */
31
    public function __construct($colorMode = null)
32
    {
33
        $this->backtrace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
34
 
35
        parent::__construct();
36
    }
37
 
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function configure()
42
    {
43
        $this
44
            ->setName('whereami')
45
            ->setDefinition([
46
                new InputOption('num', 'n', InputOption::VALUE_OPTIONAL, 'Number of lines before and after.', '5'),
47
                new InputOption('file', 'f|a', InputOption::VALUE_NONE, 'Show the full source for the current file.'),
48
            ])
49
            ->setDescription('Show where you are in the code.')
50
            ->setHelp(
51
                <<<'HELP'
52
Show where you are in the code.
53
 
54
Optionally, include the number of lines before and after you want to display,
55
or --file for the whole file.
56
 
57
e.g.
58
<return>> whereami </return>
59
<return>> whereami -n10</return>
60
<return>> whereami --file</return>
61
HELP
62
            );
63
    }
64
 
65
    /**
66
     * Obtains the correct stack frame in the full backtrace.
67
     *
68
     * @return array
69
     */
70
    protected function trace(): array
71
    {
72
        foreach (\array_reverse($this->backtrace) as $stackFrame) {
73
            if ($this->isDebugCall($stackFrame)) {
74
                return $stackFrame;
75
            }
76
        }
77
 
78
        return \end($this->backtrace);
79
    }
80
 
81
    private static function isDebugCall(array $stackFrame): bool
82
    {
83
        $class = isset($stackFrame['class']) ? $stackFrame['class'] : null;
84
        $function = isset($stackFrame['function']) ? $stackFrame['function'] : null;
85
 
86
        return ($class === null && $function === 'Psy\\debug') ||
87
            ($class === Shell::class && \in_array($function, ['__construct', 'debug']));
88
    }
89
 
90
    /**
91
     * Determine the file and line based on the specific backtrace.
92
     *
93
     * @return array
94
     */
95
    protected function fileInfo(): array
96
    {
97
        $stackFrame = $this->trace();
98
        if (\preg_match('/eval\(/', $stackFrame['file'])) {
99
            \preg_match_all('/([^\(]+)\((\d+)/', $stackFrame['file'], $matches);
100
            $file = $matches[1][0];
101
            $line = (int) $matches[2][0];
102
        } else {
103
            $file = $stackFrame['file'];
104
            $line = $stackFrame['line'];
105
        }
106
 
107
        return \compact('file', 'line');
108
    }
109
 
110
    /**
111
     * {@inheritdoc}
112
     *
113
     * @return int 0 if everything went fine, or an exit code
114
     */
115
    protected function execute(InputInterface $input, OutputInterface $output)
116
    {
117
        $info = $this->fileInfo();
118
        $num = $input->getOption('num');
119
        $lineNum = $info['line'];
120
        $startLine = \max($lineNum - $num, 1);
121
        $endLine = $lineNum + $num;
122
        $code = \file_get_contents($info['file']);
123
 
124
        if ($input->getOption('file')) {
125
            $startLine = 1;
126
            $endLine = null;
127
        }
128
 
129
        if ($output instanceof ShellOutput) {
130
            $output->startPaging();
131
        }
132
 
133
        $output->writeln(\sprintf('From <info>%s:%s</info>:', $this->replaceCwd($info['file']), $lineNum));
134
        $output->write(CodeFormatter::formatCode($code, $startLine, $endLine, $lineNum), false);
135
 
136
        if ($output instanceof ShellOutput) {
137
            $output->stopPaging();
138
        }
139
 
140
        return 0;
141
    }
142
 
143
    /**
144
     * Replace the given directory from the start of a filepath.
145
     *
146
     * @param string $file
147
     *
148
     * @return string
149
     */
150
    private function replaceCwd(string $file): string
151
    {
152
        $cwd = \getcwd();
153
        if ($cwd === false) {
154
            return $file;
155
        }
156
 
157
        $cwd = \rtrim($cwd, \DIRECTORY_SEPARATOR).\DIRECTORY_SEPARATOR;
158
 
159
        return \preg_replace('/^'.\preg_quote($cwd, '/').'/', '', $file);
160
    }
161
}