annotate vendor/psy/psysh/src/Psy/Command/ShowCommand.php @ 12:7a779792577d

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