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