annotate vendor/psy/psysh/src/VarDumper/Dumper.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@13 1 <?php
Chris@13 2
Chris@13 3 /*
Chris@13 4 * This file is part of Psy Shell.
Chris@13 5 *
Chris@13 6 * (c) 2012-2018 Justin Hileman
Chris@13 7 *
Chris@13 8 * For the full copyright and license information, please view the LICENSE
Chris@13 9 * file that was distributed with this source code.
Chris@13 10 */
Chris@13 11
Chris@13 12 namespace Psy\VarDumper;
Chris@13 13
Chris@13 14 use Symfony\Component\Console\Formatter\OutputFormatter;
Chris@13 15 use Symfony\Component\VarDumper\Cloner\Cursor;
Chris@13 16 use Symfony\Component\VarDumper\Dumper\CliDumper;
Chris@13 17
Chris@13 18 /**
Chris@13 19 * A PsySH-specialized CliDumper.
Chris@13 20 */
Chris@13 21 class Dumper extends CliDumper
Chris@13 22 {
Chris@13 23 private $formatter;
Chris@13 24 private $forceArrayIndexes;
Chris@13 25
Chris@13 26 protected static $onlyControlCharsRx = '/^[\x00-\x1F\x7F]+$/';
Chris@13 27 protected static $controlCharsRx = '/([\x00-\x1F\x7F]+)/';
Chris@13 28 protected static $controlCharsMap = [
Chris@13 29 "\0" => '\0',
Chris@13 30 "\t" => '\t',
Chris@13 31 "\n" => '\n',
Chris@13 32 "\v" => '\v',
Chris@13 33 "\f" => '\f',
Chris@13 34 "\r" => '\r',
Chris@13 35 "\033" => '\e',
Chris@13 36 ];
Chris@13 37
Chris@13 38 public function __construct(OutputFormatter $formatter, $forceArrayIndexes = false)
Chris@13 39 {
Chris@13 40 $this->formatter = $formatter;
Chris@13 41 $this->forceArrayIndexes = $forceArrayIndexes;
Chris@13 42 parent::__construct();
Chris@13 43 $this->setColors(false);
Chris@13 44 }
Chris@13 45
Chris@13 46 /**
Chris@13 47 * {@inheritdoc}
Chris@13 48 */
Chris@13 49 public function enterHash(Cursor $cursor, $type, $class, $hasChild)
Chris@13 50 {
Chris@13 51 if (Cursor::HASH_INDEXED === $type || Cursor::HASH_ASSOC === $type) {
Chris@13 52 $class = 0;
Chris@13 53 }
Chris@13 54 parent::enterHash($cursor, $type, $class, $hasChild);
Chris@13 55 }
Chris@13 56
Chris@13 57 /**
Chris@13 58 * {@inheritdoc}
Chris@13 59 */
Chris@13 60 protected function dumpKey(Cursor $cursor)
Chris@13 61 {
Chris@13 62 if ($this->forceArrayIndexes || Cursor::HASH_INDEXED !== $cursor->hashType) {
Chris@13 63 parent::dumpKey($cursor);
Chris@13 64 }
Chris@13 65 }
Chris@13 66
Chris@13 67 protected function style($style, $value, $attr = [])
Chris@13 68 {
Chris@13 69 if ('ref' === $style) {
Chris@17 70 $value = \strtr($value, '@', '#');
Chris@13 71 }
Chris@13 72
Chris@13 73 $styled = '';
Chris@13 74 $map = self::$controlCharsMap;
Chris@13 75 $cchr = $this->styles['cchr'];
Chris@13 76
Chris@17 77 $chunks = \preg_split(self::$controlCharsRx, $value, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
Chris@13 78 foreach ($chunks as $chunk) {
Chris@17 79 if (\preg_match(self::$onlyControlCharsRx, $chunk)) {
Chris@13 80 $chars = '';
Chris@13 81 $i = 0;
Chris@13 82 do {
Chris@17 83 $chars .= isset($map[$chunk[$i]]) ? $map[$chunk[$i]] : \sprintf('\x%02X', \ord($chunk[$i]));
Chris@13 84 } while (isset($chunk[++$i]));
Chris@13 85
Chris@13 86 $chars = $this->formatter->escape($chars);
Chris@13 87 $styled .= "<{$cchr}>{$chars}</{$cchr}>";
Chris@13 88 } else {
Chris@13 89 $styled .= $this->formatter->escape($chunk);
Chris@13 90 }
Chris@13 91 }
Chris@13 92
Chris@13 93 $style = $this->styles[$style];
Chris@13 94
Chris@13 95 return "<{$style}>{$styled}</{$style}>";
Chris@13 96 }
Chris@13 97
Chris@13 98 /**
Chris@13 99 * {@inheritdoc}
Chris@13 100 */
Chris@13 101 protected function dumpLine($depth, $endOfValue = false)
Chris@13 102 {
Chris@13 103 if ($endOfValue && 0 < $depth) {
Chris@13 104 $this->line .= ',';
Chris@13 105 }
Chris@13 106 $this->line = $this->formatter->format($this->line);
Chris@13 107 parent::dumpLine($depth, $endOfValue);
Chris@13 108 }
Chris@13 109 }