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\Exception\RuntimeException;
|
Chris@13
|
18 use Psy\Formatter\CodeFormatter;
|
Chris@13
|
19 use Psy\Formatter\SignatureFormatter;
|
Chris@13
|
20 use Psy\Input\CodeArgument;
|
Chris@13
|
21 use Psy\Output\ShellOutput;
|
Chris@13
|
22 use Symfony\Component\Console\Formatter\OutputFormatter;
|
Chris@13
|
23 use Symfony\Component\Console\Input\InputInterface;
|
Chris@13
|
24 use Symfony\Component\Console\Input\InputOption;
|
Chris@13
|
25 use Symfony\Component\Console\Output\OutputInterface;
|
Chris@13
|
26
|
Chris@13
|
27 /**
|
Chris@13
|
28 * Show the code for an object, class, constant, method or property.
|
Chris@13
|
29 */
|
Chris@13
|
30 class ShowCommand extends ReflectingCommand
|
Chris@13
|
31 {
|
Chris@13
|
32 private $colorMode;
|
Chris@13
|
33 private $highlighter;
|
Chris@13
|
34 private $lastException;
|
Chris@13
|
35 private $lastExceptionIndex;
|
Chris@13
|
36
|
Chris@13
|
37 /**
|
Chris@13
|
38 * @param null|string $colorMode (default: null)
|
Chris@13
|
39 */
|
Chris@13
|
40 public function __construct($colorMode = null)
|
Chris@13
|
41 {
|
Chris@13
|
42 $this->colorMode = $colorMode ?: Configuration::COLOR_MODE_AUTO;
|
Chris@13
|
43
|
Chris@13
|
44 parent::__construct();
|
Chris@13
|
45 }
|
Chris@13
|
46
|
Chris@13
|
47 /**
|
Chris@13
|
48 * {@inheritdoc}
|
Chris@13
|
49 */
|
Chris@13
|
50 protected function configure()
|
Chris@13
|
51 {
|
Chris@13
|
52 $this
|
Chris@13
|
53 ->setName('show')
|
Chris@13
|
54 ->setDefinition([
|
Chris@13
|
55 new CodeArgument('target', CodeArgument::OPTIONAL, 'Function, class, instance, constant, method or property to show.'),
|
Chris@13
|
56 new InputOption('ex', null, InputOption::VALUE_OPTIONAL, 'Show last exception context. Optionally specify a stack index.', 1),
|
Chris@13
|
57 ])
|
Chris@13
|
58 ->setDescription('Show the code for an object, class, constant, method or property.')
|
Chris@13
|
59 ->setHelp(
|
Chris@13
|
60 <<<HELP
|
Chris@13
|
61 Show the code for an object, class, constant, method or property, or the context
|
Chris@13
|
62 of the last exception.
|
Chris@13
|
63
|
Chris@13
|
64 <return>cat --ex</return> defaults to showing the lines surrounding the location of the last
|
Chris@13
|
65 exception. Invoking it more than once travels up the exception's stack trace,
|
Chris@13
|
66 and providing a number shows the context of the given index of the trace.
|
Chris@13
|
67
|
Chris@13
|
68 e.g.
|
Chris@13
|
69 <return>>>> show \$myObject</return>
|
Chris@13
|
70 <return>>>> show Psy\Shell::debug</return>
|
Chris@13
|
71 <return>>>> show --ex</return>
|
Chris@13
|
72 <return>>>> show --ex 3</return>
|
Chris@13
|
73 HELP
|
Chris@13
|
74 );
|
Chris@13
|
75 }
|
Chris@13
|
76
|
Chris@13
|
77 /**
|
Chris@13
|
78 * {@inheritdoc}
|
Chris@13
|
79 */
|
Chris@13
|
80 protected function execute(InputInterface $input, OutputInterface $output)
|
Chris@13
|
81 {
|
Chris@13
|
82 // n.b. As far as I can tell, InputInterface doesn't want to tell me
|
Chris@13
|
83 // whether an option with an optional value was actually passed. If you
|
Chris@13
|
84 // call `$input->getOption('ex')`, it will return the default, both when
|
Chris@13
|
85 // `--ex` is specified with no value, and when `--ex` isn't specified at
|
Chris@13
|
86 // all.
|
Chris@13
|
87 //
|
Chris@13
|
88 // So we're doing something sneaky here. If we call `getOptions`, it'll
|
Chris@13
|
89 // return the default value when `--ex` is not present, and `null` if
|
Chris@13
|
90 // `--ex` is passed with no value. /shrug
|
Chris@13
|
91 $opts = $input->getOptions();
|
Chris@13
|
92
|
Chris@13
|
93 // Strict comparison to `1` (the default value) here, because `--ex 1`
|
Chris@13
|
94 // will come in as `"1"`. Now we can tell the difference between
|
Chris@13
|
95 // "no --ex present", because it's the integer 1, "--ex with no value",
|
Chris@13
|
96 // because it's `null`, and "--ex 1", because it's the string "1".
|
Chris@13
|
97 if ($opts['ex'] !== 1) {
|
Chris@13
|
98 if ($input->getArgument('target')) {
|
Chris@13
|
99 throw new \InvalidArgumentException('Too many arguments (supply either "target" or "--ex")');
|
Chris@13
|
100 }
|
Chris@13
|
101
|
Chris@13
|
102 return $this->writeExceptionContext($input, $output);
|
Chris@13
|
103 }
|
Chris@13
|
104
|
Chris@13
|
105 if ($input->getArgument('target')) {
|
Chris@13
|
106 return $this->writeCodeContext($input, $output);
|
Chris@13
|
107 }
|
Chris@13
|
108
|
Chris@13
|
109 throw new RuntimeException('Not enough arguments (missing: "target")');
|
Chris@13
|
110 }
|
Chris@13
|
111
|
Chris@13
|
112 private function writeCodeContext(InputInterface $input, OutputInterface $output)
|
Chris@13
|
113 {
|
Chris@13
|
114 list($target, $reflector) = $this->getTargetAndReflector($input->getArgument('target'));
|
Chris@13
|
115
|
Chris@13
|
116 // Set some magic local variables
|
Chris@13
|
117 $this->setCommandScopeVariables($reflector);
|
Chris@13
|
118
|
Chris@13
|
119 try {
|
Chris@13
|
120 $output->page(CodeFormatter::format($reflector, $this->colorMode), ShellOutput::OUTPUT_RAW);
|
Chris@13
|
121 } catch (RuntimeException $e) {
|
Chris@13
|
122 $output->writeln(SignatureFormatter::format($reflector));
|
Chris@13
|
123 throw $e;
|
Chris@13
|
124 }
|
Chris@13
|
125 }
|
Chris@13
|
126
|
Chris@13
|
127 private function writeExceptionContext(InputInterface $input, OutputInterface $output)
|
Chris@13
|
128 {
|
Chris@13
|
129 $exception = $this->context->getLastException();
|
Chris@13
|
130 if ($exception !== $this->lastException) {
|
Chris@13
|
131 $this->lastException = null;
|
Chris@13
|
132 $this->lastExceptionIndex = null;
|
Chris@13
|
133 }
|
Chris@13
|
134
|
Chris@13
|
135 $opts = $input->getOptions();
|
Chris@13
|
136 if ($opts['ex'] === null) {
|
Chris@13
|
137 if ($this->lastException && $this->lastExceptionIndex !== null) {
|
Chris@13
|
138 $index = $this->lastExceptionIndex + 1;
|
Chris@13
|
139 } else {
|
Chris@13
|
140 $index = 0;
|
Chris@13
|
141 }
|
Chris@13
|
142 } else {
|
Chris@17
|
143 $index = \max(0, \intval($input->getOption('ex')) - 1);
|
Chris@13
|
144 }
|
Chris@13
|
145
|
Chris@13
|
146 $trace = $exception->getTrace();
|
Chris@17
|
147 \array_unshift($trace, [
|
Chris@13
|
148 'file' => $exception->getFile(),
|
Chris@13
|
149 'line' => $exception->getLine(),
|
Chris@13
|
150 ]);
|
Chris@13
|
151
|
Chris@17
|
152 if ($index >= \count($trace)) {
|
Chris@13
|
153 $index = 0;
|
Chris@13
|
154 }
|
Chris@13
|
155
|
Chris@13
|
156 $this->lastException = $exception;
|
Chris@13
|
157 $this->lastExceptionIndex = $index;
|
Chris@13
|
158
|
Chris@13
|
159 $output->writeln($this->getApplication()->formatException($exception));
|
Chris@13
|
160 $output->writeln('--');
|
Chris@13
|
161 $this->writeTraceLine($output, $trace, $index);
|
Chris@13
|
162 $this->writeTraceCodeSnippet($output, $trace, $index);
|
Chris@13
|
163
|
Chris@13
|
164 $this->setCommandScopeVariablesFromContext($trace[$index]);
|
Chris@13
|
165 }
|
Chris@13
|
166
|
Chris@13
|
167 private function writeTraceLine(OutputInterface $output, array $trace, $index)
|
Chris@13
|
168 {
|
Chris@13
|
169 $file = isset($trace[$index]['file']) ? $this->replaceCwd($trace[$index]['file']) : 'n/a';
|
Chris@13
|
170 $line = isset($trace[$index]['line']) ? $trace[$index]['line'] : 'n/a';
|
Chris@13
|
171
|
Chris@17
|
172 $output->writeln(\sprintf(
|
Chris@13
|
173 'From <info>%s:%d</info> at <strong>level %d</strong> of backtrace (of %d).',
|
Chris@13
|
174 OutputFormatter::escape($file),
|
Chris@13
|
175 OutputFormatter::escape($line),
|
Chris@13
|
176 $index + 1,
|
Chris@17
|
177 \count($trace)
|
Chris@13
|
178 ));
|
Chris@13
|
179 }
|
Chris@13
|
180
|
Chris@13
|
181 private function replaceCwd($file)
|
Chris@13
|
182 {
|
Chris@17
|
183 if ($cwd = \getcwd()) {
|
Chris@17
|
184 $cwd = \rtrim($cwd, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
|
Chris@13
|
185 }
|
Chris@13
|
186
|
Chris@13
|
187 if ($cwd === false) {
|
Chris@13
|
188 return $file;
|
Chris@13
|
189 } else {
|
Chris@17
|
190 return \preg_replace('/^' . \preg_quote($cwd, '/') . '/', '', $file);
|
Chris@13
|
191 }
|
Chris@13
|
192 }
|
Chris@13
|
193
|
Chris@13
|
194 private function writeTraceCodeSnippet(OutputInterface $output, array $trace, $index)
|
Chris@13
|
195 {
|
Chris@13
|
196 if (!isset($trace[$index]['file'])) {
|
Chris@13
|
197 return;
|
Chris@13
|
198 }
|
Chris@13
|
199
|
Chris@13
|
200 $file = $trace[$index]['file'];
|
Chris@13
|
201 if ($fileAndLine = $this->extractEvalFileAndLine($file)) {
|
Chris@13
|
202 list($file, $line) = $fileAndLine;
|
Chris@13
|
203 } else {
|
Chris@13
|
204 if (!isset($trace[$index]['line'])) {
|
Chris@13
|
205 return;
|
Chris@13
|
206 }
|
Chris@13
|
207
|
Chris@13
|
208 $line = $trace[$index]['line'];
|
Chris@13
|
209 }
|
Chris@13
|
210
|
Chris@17
|
211 if (\is_file($file)) {
|
Chris@17
|
212 $code = @\file_get_contents($file);
|
Chris@13
|
213 }
|
Chris@13
|
214
|
Chris@13
|
215 if (empty($code)) {
|
Chris@13
|
216 return;
|
Chris@13
|
217 }
|
Chris@13
|
218
|
Chris@13
|
219 $output->write($this->getHighlighter()->getCodeSnippet($code, $line, 5, 5), ShellOutput::OUTPUT_RAW);
|
Chris@13
|
220 }
|
Chris@13
|
221
|
Chris@13
|
222 private function getHighlighter()
|
Chris@13
|
223 {
|
Chris@13
|
224 if (!$this->highlighter) {
|
Chris@13
|
225 $factory = new ConsoleColorFactory($this->colorMode);
|
Chris@13
|
226 $this->highlighter = new Highlighter($factory->getConsoleColor());
|
Chris@13
|
227 }
|
Chris@13
|
228
|
Chris@13
|
229 return $this->highlighter;
|
Chris@13
|
230 }
|
Chris@13
|
231
|
Chris@13
|
232 private function setCommandScopeVariablesFromContext(array $context)
|
Chris@13
|
233 {
|
Chris@13
|
234 $vars = [];
|
Chris@13
|
235
|
Chris@13
|
236 if (isset($context['class'])) {
|
Chris@13
|
237 $vars['__class'] = $context['class'];
|
Chris@13
|
238 if (isset($context['function'])) {
|
Chris@13
|
239 $vars['__method'] = $context['function'];
|
Chris@13
|
240 }
|
Chris@13
|
241
|
Chris@13
|
242 try {
|
Chris@13
|
243 $refl = new \ReflectionClass($context['class']);
|
Chris@13
|
244 if ($namespace = $refl->getNamespaceName()) {
|
Chris@13
|
245 $vars['__namespace'] = $namespace;
|
Chris@13
|
246 }
|
Chris@13
|
247 } catch (\Exception $e) {
|
Chris@13
|
248 // oh well
|
Chris@13
|
249 }
|
Chris@13
|
250 } elseif (isset($context['function'])) {
|
Chris@13
|
251 $vars['__function'] = $context['function'];
|
Chris@13
|
252
|
Chris@13
|
253 try {
|
Chris@13
|
254 $refl = new \ReflectionFunction($context['function']);
|
Chris@13
|
255 if ($namespace = $refl->getNamespaceName()) {
|
Chris@13
|
256 $vars['__namespace'] = $namespace;
|
Chris@13
|
257 }
|
Chris@13
|
258 } catch (\Exception $e) {
|
Chris@13
|
259 // oh well
|
Chris@13
|
260 }
|
Chris@13
|
261 }
|
Chris@13
|
262
|
Chris@13
|
263 if (isset($context['file'])) {
|
Chris@13
|
264 $file = $context['file'];
|
Chris@13
|
265 if ($fileAndLine = $this->extractEvalFileAndLine($file)) {
|
Chris@13
|
266 list($file, $line) = $fileAndLine;
|
Chris@13
|
267 } elseif (isset($context['line'])) {
|
Chris@13
|
268 $line = $context['line'];
|
Chris@13
|
269 }
|
Chris@13
|
270
|
Chris@17
|
271 if (\is_file($file)) {
|
Chris@13
|
272 $vars['__file'] = $file;
|
Chris@13
|
273 if (isset($line)) {
|
Chris@13
|
274 $vars['__line'] = $line;
|
Chris@13
|
275 }
|
Chris@17
|
276 $vars['__dir'] = \dirname($file);
|
Chris@13
|
277 }
|
Chris@13
|
278 }
|
Chris@13
|
279
|
Chris@13
|
280 $this->context->setCommandScopeVariables($vars);
|
Chris@13
|
281 }
|
Chris@13
|
282
|
Chris@13
|
283 private function extractEvalFileAndLine($file)
|
Chris@13
|
284 {
|
Chris@17
|
285 if (\preg_match('/(.*)\\((\\d+)\\) : eval\\(\\)\'d code$/', $file, $matches)) {
|
Chris@13
|
286 return [$matches[1], $matches[2]];
|
Chris@13
|
287 }
|
Chris@13
|
288 }
|
Chris@13
|
289 }
|