annotate vendor/psy/psysh/src/Command/WhereamiCommand.php @ 4:a9cd425dd02b

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