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