Upgraded drupal core with security updates
[yaffs-website] / vendor / psy / psysh / src / Psy / Command / WhereamiCommand.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 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
29     /**
30      * @param null|string $colorMode (default: null)
31      */
32     public function __construct($colorMode = null)
33     {
34         $this->colorMode = $colorMode ?: Configuration::COLOR_MODE_AUTO;
35
36         if (version_compare(PHP_VERSION, '5.3.6', '>=')) {
37             $this->backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
38         } else {
39             $this->backtrace = debug_backtrace();
40         }
41
42         return parent::__construct();
43     }
44
45     /**
46      * {@inheritdoc}
47      */
48     protected function configure()
49     {
50         $this
51             ->setName('whereami')
52             ->setDefinition(array(
53                 new InputOption('num', 'n', InputOption::VALUE_OPTIONAL, 'Number of lines before and after.', '5'),
54             ))
55             ->setDescription('Show where you are in the code.')
56             ->setHelp(
57                 <<<'HELP'
58 Show where you are in the code.
59
60 Optionally, include how many lines before and after you want to display.
61
62 e.g.
63 <return>> whereami </return>
64 <return>> whereami -n10</return>
65 HELP
66             );
67     }
68
69     /**
70      * Obtains the correct trace in the full backtrace.
71      *
72      * @return array
73      */
74     protected function trace()
75     {
76         foreach ($this->backtrace as $i => $backtrace) {
77             if (!isset($backtrace['class'], $backtrace['function'])) {
78                 continue;
79             }
80             $correctClass = $backtrace['class'] === 'Psy\Shell';
81             $correctFunction = $backtrace['function'] === 'debug';
82             if ($correctClass && $correctFunction) {
83                 return $backtrace;
84             }
85         }
86
87         return end($this->backtrace);
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         $backtrace = $this->trace();
98         if (preg_match('/eval\(/', $backtrace['file'])) {
99             preg_match_all('/([^\(]+)\((\d+)/', $backtrace['file'], $matches);
100             $file = $matches[1][0];
101             $line = (int) $matches[2][0];
102         } else {
103             $file = $backtrace['file'];
104             $line = $backtrace['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         $output->page($highlighter->getCodeSnippet($contents, $info['line'], $num, $num), ShellOutput::OUTPUT_RAW);
122     }
123 }