4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Command;
14 use JakubOnderka\PhpConsoleHighlighter\Highlighter;
15 use Psy\Configuration;
16 use Psy\ConsoleColorFactory;
17 use Psy\Output\ShellOutput;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Input\InputOption;
20 use Symfony\Component\Console\Output\OutputInterface;
23 * Show the context of where you opened the debugger.
25 class WhereamiCommand extends Command
30 * @param null|string $colorMode (default: null)
32 public function __construct($colorMode = null)
34 $this->colorMode = $colorMode ?: Configuration::COLOR_MODE_AUTO;
36 if (version_compare(PHP_VERSION, '5.3.6', '>=')) {
37 $this->backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
39 $this->backtrace = debug_backtrace();
42 return parent::__construct();
48 protected function configure()
52 ->setDefinition(array(
53 new InputOption('num', 'n', InputOption::VALUE_OPTIONAL, 'Number of lines before and after.', '5'),
55 ->setDescription('Show where you are in the code.')
58 Show where you are in the code.
60 Optionally, include how many lines before and after you want to display.
63 <return>> whereami </return>
64 <return>> whereami -n10</return>
70 * Obtains the correct stack frame in the full backtrace.
74 protected function trace()
76 foreach (array_reverse($this->backtrace) as $stackFrame) {
77 if ($this->isDebugCall($stackFrame)) {
82 return end($this->backtrace);
85 private static function isDebugCall(array $stackFrame)
87 $class = isset($stackFrame['class']) ? $stackFrame['class'] : null;
88 $function = isset($stackFrame['function']) ? $stackFrame['function'] : null;
90 return ($class === null && $function === 'Psy\debug') ||
91 ($class === 'Psy\Shell' && in_array($function, array('__construct', 'debug')));
95 * Determine the file and line based on the specific backtrace.
99 protected function fileInfo()
101 $stackFrame = $this->trace();
102 if (preg_match('/eval\(/', $stackFrame['file'])) {
103 preg_match_all('/([^\(]+)\((\d+)/', $stackFrame['file'], $matches);
104 $file = $matches[1][0];
105 $line = (int) $matches[2][0];
107 $file = $stackFrame['file'];
108 $line = $stackFrame['line'];
111 return compact('file', 'line');
117 protected function execute(InputInterface $input, OutputInterface $output)
119 $info = $this->fileInfo();
120 $num = $input->getOption('num');
121 $factory = new ConsoleColorFactory($this->colorMode);
122 $colors = $factory->getConsoleColor();
123 $highlighter = new Highlighter($colors);
124 $contents = file_get_contents($info['file']);
126 $output->startPaging();
127 $output->writeln('');
128 $output->writeln(sprintf('From <info>%s:%s</info>:', $this->replaceCwd($info['file']), $info['line']));
129 $output->writeln('');
130 $output->write($highlighter->getCodeSnippet($contents, $info['line'], $num, $num), ShellOutput::OUTPUT_RAW);
131 $output->stopPaging();
135 * Replace the given directory from the start of a filepath.
137 * @param string $file
141 private function replaceCwd($file)
144 if ($cwd === false) {
148 $cwd = rtrim($cwd, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
150 return preg_replace('/^' . preg_quote($cwd, '/') . '/', '', $file);