annotate vendor/psy/psysh/src/Command/TraceCommand.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 Psy\Input\FilterOptions;
Chris@0 15 use Psy\Output\ShellOutput;
Chris@0 16 use Symfony\Component\Console\Formatter\OutputFormatter;
Chris@0 17 use Symfony\Component\Console\Input\InputInterface;
Chris@0 18 use Symfony\Component\Console\Input\InputOption;
Chris@0 19 use Symfony\Component\Console\Output\OutputInterface;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * Show the current stack trace.
Chris@0 23 */
Chris@0 24 class TraceCommand extends Command
Chris@0 25 {
Chris@0 26 protected $filter;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * {@inheritdoc}
Chris@0 30 */
Chris@0 31 public function __construct($name = null)
Chris@0 32 {
Chris@0 33 $this->filter = new FilterOptions();
Chris@0 34
Chris@0 35 parent::__construct($name);
Chris@0 36 }
Chris@0 37
Chris@0 38 /**
Chris@0 39 * {@inheritdoc}
Chris@0 40 */
Chris@0 41 protected function configure()
Chris@0 42 {
Chris@0 43 list($grep, $insensitive, $invert) = FilterOptions::getOptions();
Chris@0 44
Chris@0 45 $this
Chris@0 46 ->setName('trace')
Chris@0 47 ->setDefinition([
Chris@0 48 new InputOption('include-psy', 'p', InputOption::VALUE_NONE, 'Include Psy in the call stack.'),
Chris@0 49 new InputOption('num', 'n', InputOption::VALUE_REQUIRED, 'Only include NUM lines.'),
Chris@0 50
Chris@0 51 $grep,
Chris@0 52 $insensitive,
Chris@0 53 $invert,
Chris@0 54 ])
Chris@0 55 ->setDescription('Show the current call stack.')
Chris@0 56 ->setHelp(
Chris@0 57 <<<'HELP'
Chris@0 58 Show the current call stack.
Chris@0 59
Chris@0 60 Optionally, include PsySH in the call stack by passing the <info>--include-psy</info> option.
Chris@0 61
Chris@0 62 e.g.
Chris@0 63 <return>> trace -n10</return>
Chris@0 64 <return>> trace --include-psy</return>
Chris@0 65 HELP
Chris@0 66 );
Chris@0 67 }
Chris@0 68
Chris@0 69 /**
Chris@0 70 * {@inheritdoc}
Chris@0 71 */
Chris@0 72 protected function execute(InputInterface $input, OutputInterface $output)
Chris@0 73 {
Chris@0 74 $this->filter->bind($input);
Chris@0 75 $trace = $this->getBacktrace(new \Exception(), $input->getOption('num'), $input->getOption('include-psy'));
Chris@0 76 $output->page($trace, ShellOutput::NUMBER_LINES);
Chris@0 77 }
Chris@0 78
Chris@0 79 /**
Chris@0 80 * Get a backtrace for an exception.
Chris@0 81 *
Chris@0 82 * Optionally limit the number of rows to include with $count, and exclude
Chris@0 83 * Psy from the trace.
Chris@0 84 *
Chris@0 85 * @param \Exception $e The exception with a backtrace
Chris@0 86 * @param int $count (default: PHP_INT_MAX)
Chris@0 87 * @param bool $includePsy (default: true)
Chris@0 88 *
Chris@0 89 * @return array Formatted stacktrace lines
Chris@0 90 */
Chris@0 91 protected function getBacktrace(\Exception $e, $count = null, $includePsy = true)
Chris@0 92 {
Chris@4 93 if ($cwd = \getcwd()) {
Chris@4 94 $cwd = \rtrim($cwd, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
Chris@0 95 }
Chris@0 96
Chris@0 97 if ($count === null) {
Chris@0 98 $count = PHP_INT_MAX;
Chris@0 99 }
Chris@0 100
Chris@0 101 $lines = [];
Chris@0 102
Chris@0 103 $trace = $e->getTrace();
Chris@4 104 \array_unshift($trace, [
Chris@0 105 'function' => '',
Chris@0 106 'file' => $e->getFile() !== null ? $e->getFile() : 'n/a',
Chris@0 107 'line' => $e->getLine() !== null ? $e->getLine() : 'n/a',
Chris@0 108 'args' => [],
Chris@0 109 ]);
Chris@0 110
Chris@0 111 if (!$includePsy) {
Chris@4 112 for ($i = \count($trace) - 1; $i >= 0; $i--) {
Chris@0 113 $thing = isset($trace[$i]['class']) ? $trace[$i]['class'] : $trace[$i]['function'];
Chris@4 114 if (\preg_match('/\\\\?Psy\\\\/', $thing)) {
Chris@4 115 $trace = \array_slice($trace, $i + 1);
Chris@0 116 break;
Chris@0 117 }
Chris@0 118 }
Chris@0 119 }
Chris@0 120
Chris@4 121 for ($i = 0, $count = \min($count, \count($trace)); $i < $count; $i++) {
Chris@0 122 $class = isset($trace[$i]['class']) ? $trace[$i]['class'] : '';
Chris@0 123 $type = isset($trace[$i]['type']) ? $trace[$i]['type'] : '';
Chris@0 124 $function = $trace[$i]['function'];
Chris@0 125 $file = isset($trace[$i]['file']) ? $this->replaceCwd($cwd, $trace[$i]['file']) : 'n/a';
Chris@0 126 $line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a';
Chris@0 127
Chris@0 128 // Leave execution loop out of the `eval()'d code` lines
Chris@4 129 if (\preg_match("#/src/Execution(?:Loop)?Closure.php\(\d+\) : eval\(\)'d code$#", \str_replace('\\', '/', $file))) {
Chris@0 130 $file = "eval()'d code";
Chris@0 131 }
Chris@0 132
Chris@0 133 // Skip any lines that don't match our filter options
Chris@4 134 if (!$this->filter->match(\sprintf('%s%s%s() at %s:%s', $class, $type, $function, $file, $line))) {
Chris@0 135 continue;
Chris@0 136 }
Chris@0 137
Chris@4 138 $lines[] = \sprintf(
Chris@0 139 ' <class>%s</class>%s%s() at <info>%s:%s</info>',
Chris@0 140 OutputFormatter::escape($class),
Chris@0 141 OutputFormatter::escape($type),
Chris@0 142 OutputFormatter::escape($function),
Chris@0 143 OutputFormatter::escape($file),
Chris@0 144 OutputFormatter::escape($line)
Chris@0 145 );
Chris@0 146 }
Chris@0 147
Chris@0 148 return $lines;
Chris@0 149 }
Chris@0 150
Chris@0 151 /**
Chris@0 152 * Replace the given directory from the start of a filepath.
Chris@0 153 *
Chris@0 154 * @param string $cwd
Chris@0 155 * @param string $file
Chris@0 156 *
Chris@0 157 * @return string
Chris@0 158 */
Chris@0 159 private function replaceCwd($cwd, $file)
Chris@0 160 {
Chris@0 161 if ($cwd === false) {
Chris@0 162 return $file;
Chris@0 163 } else {
Chris@4 164 return \preg_replace('/^' . \preg_quote($cwd, '/') . '/', '', $file);
Chris@0 165 }
Chris@0 166 }
Chris@0 167 }