Chris@13
|
1 <?php
|
Chris@13
|
2
|
Chris@13
|
3 /*
|
Chris@13
|
4 * This file is part of Psy Shell.
|
Chris@13
|
5 *
|
Chris@13
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@13
|
7 *
|
Chris@13
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@13
|
9 * file that was distributed with this source code.
|
Chris@13
|
10 */
|
Chris@13
|
11
|
Chris@13
|
12 namespace Psy\Command;
|
Chris@13
|
13
|
Chris@13
|
14 use JakubOnderka\PhpConsoleHighlighter\Highlighter;
|
Chris@13
|
15 use Psy\Configuration;
|
Chris@13
|
16 use Psy\ConsoleColorFactory;
|
Chris@13
|
17 use Psy\Output\ShellOutput;
|
Chris@13
|
18 use Symfony\Component\Console\Input\InputInterface;
|
Chris@13
|
19 use Symfony\Component\Console\Input\InputOption;
|
Chris@13
|
20 use Symfony\Component\Console\Output\OutputInterface;
|
Chris@13
|
21
|
Chris@13
|
22 /**
|
Chris@13
|
23 * Show the context of where you opened the debugger.
|
Chris@13
|
24 */
|
Chris@13
|
25 class WhereamiCommand extends Command
|
Chris@13
|
26 {
|
Chris@13
|
27 private $colorMode;
|
Chris@13
|
28 private $backtrace;
|
Chris@13
|
29
|
Chris@13
|
30 /**
|
Chris@13
|
31 * @param null|string $colorMode (default: null)
|
Chris@13
|
32 */
|
Chris@13
|
33 public function __construct($colorMode = null)
|
Chris@13
|
34 {
|
Chris@13
|
35 $this->colorMode = $colorMode ?: Configuration::COLOR_MODE_AUTO;
|
Chris@17
|
36 $this->backtrace = \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
Chris@13
|
37
|
Chris@13
|
38 parent::__construct();
|
Chris@13
|
39 }
|
Chris@13
|
40
|
Chris@13
|
41 /**
|
Chris@13
|
42 * {@inheritdoc}
|
Chris@13
|
43 */
|
Chris@13
|
44 protected function configure()
|
Chris@13
|
45 {
|
Chris@13
|
46 $this
|
Chris@13
|
47 ->setName('whereami')
|
Chris@13
|
48 ->setDefinition([
|
Chris@13
|
49 new InputOption('num', 'n', InputOption::VALUE_OPTIONAL, 'Number of lines before and after.', '5'),
|
Chris@13
|
50 ])
|
Chris@13
|
51 ->setDescription('Show where you are in the code.')
|
Chris@13
|
52 ->setHelp(
|
Chris@13
|
53 <<<'HELP'
|
Chris@13
|
54 Show where you are in the code.
|
Chris@13
|
55
|
Chris@13
|
56 Optionally, include how many lines before and after you want to display.
|
Chris@13
|
57
|
Chris@13
|
58 e.g.
|
Chris@13
|
59 <return>> whereami </return>
|
Chris@13
|
60 <return>> whereami -n10</return>
|
Chris@13
|
61 HELP
|
Chris@13
|
62 );
|
Chris@13
|
63 }
|
Chris@13
|
64
|
Chris@13
|
65 /**
|
Chris@13
|
66 * Obtains the correct stack frame in the full backtrace.
|
Chris@13
|
67 *
|
Chris@13
|
68 * @return array
|
Chris@13
|
69 */
|
Chris@13
|
70 protected function trace()
|
Chris@13
|
71 {
|
Chris@17
|
72 foreach (\array_reverse($this->backtrace) as $stackFrame) {
|
Chris@13
|
73 if ($this->isDebugCall($stackFrame)) {
|
Chris@13
|
74 return $stackFrame;
|
Chris@13
|
75 }
|
Chris@13
|
76 }
|
Chris@13
|
77
|
Chris@17
|
78 return \end($this->backtrace);
|
Chris@13
|
79 }
|
Chris@13
|
80
|
Chris@13
|
81 private static function isDebugCall(array $stackFrame)
|
Chris@13
|
82 {
|
Chris@13
|
83 $class = isset($stackFrame['class']) ? $stackFrame['class'] : null;
|
Chris@13
|
84 $function = isset($stackFrame['function']) ? $stackFrame['function'] : null;
|
Chris@13
|
85
|
Chris@13
|
86 return ($class === null && $function === 'Psy\debug') ||
|
Chris@17
|
87 ($class === 'Psy\Shell' && \in_array($function, ['__construct', 'debug']));
|
Chris@13
|
88 }
|
Chris@13
|
89
|
Chris@13
|
90 /**
|
Chris@13
|
91 * Determine the file and line based on the specific backtrace.
|
Chris@13
|
92 *
|
Chris@13
|
93 * @return array
|
Chris@13
|
94 */
|
Chris@13
|
95 protected function fileInfo()
|
Chris@13
|
96 {
|
Chris@13
|
97 $stackFrame = $this->trace();
|
Chris@17
|
98 if (\preg_match('/eval\(/', $stackFrame['file'])) {
|
Chris@17
|
99 \preg_match_all('/([^\(]+)\((\d+)/', $stackFrame['file'], $matches);
|
Chris@13
|
100 $file = $matches[1][0];
|
Chris@13
|
101 $line = (int) $matches[2][0];
|
Chris@13
|
102 } else {
|
Chris@13
|
103 $file = $stackFrame['file'];
|
Chris@13
|
104 $line = $stackFrame['line'];
|
Chris@13
|
105 }
|
Chris@13
|
106
|
Chris@17
|
107 return \compact('file', 'line');
|
Chris@13
|
108 }
|
Chris@13
|
109
|
Chris@13
|
110 /**
|
Chris@13
|
111 * {@inheritdoc}
|
Chris@13
|
112 */
|
Chris@13
|
113 protected function execute(InputInterface $input, OutputInterface $output)
|
Chris@13
|
114 {
|
Chris@13
|
115 $info = $this->fileInfo();
|
Chris@13
|
116 $num = $input->getOption('num');
|
Chris@13
|
117 $factory = new ConsoleColorFactory($this->colorMode);
|
Chris@13
|
118 $colors = $factory->getConsoleColor();
|
Chris@13
|
119 $highlighter = new Highlighter($colors);
|
Chris@17
|
120 $contents = \file_get_contents($info['file']);
|
Chris@13
|
121
|
Chris@13
|
122 $output->startPaging();
|
Chris@13
|
123 $output->writeln('');
|
Chris@17
|
124 $output->writeln(\sprintf('From <info>%s:%s</info>:', $this->replaceCwd($info['file']), $info['line']));
|
Chris@13
|
125 $output->writeln('');
|
Chris@13
|
126 $output->write($highlighter->getCodeSnippet($contents, $info['line'], $num, $num), ShellOutput::OUTPUT_RAW);
|
Chris@13
|
127 $output->stopPaging();
|
Chris@13
|
128 }
|
Chris@13
|
129
|
Chris@13
|
130 /**
|
Chris@13
|
131 * Replace the given directory from the start of a filepath.
|
Chris@13
|
132 *
|
Chris@13
|
133 * @param string $file
|
Chris@13
|
134 *
|
Chris@13
|
135 * @return string
|
Chris@13
|
136 */
|
Chris@13
|
137 private function replaceCwd($file)
|
Chris@13
|
138 {
|
Chris@17
|
139 $cwd = \getcwd();
|
Chris@13
|
140 if ($cwd === false) {
|
Chris@13
|
141 return $file;
|
Chris@13
|
142 }
|
Chris@13
|
143
|
Chris@17
|
144 $cwd = \rtrim($cwd, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
|
Chris@13
|
145
|
Chris@17
|
146 return \preg_replace('/^' . \preg_quote($cwd, '/') . '/', '', $file);
|
Chris@13
|
147 }
|
Chris@13
|
148 }
|