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