4 * This file is part of Psy Shell.
6 * (c) 2012-2018 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\VarDumper;
14 use Symfony\Component\Console\Formatter\OutputFormatter;
15 use Symfony\Component\VarDumper\Cloner\Cursor;
16 use Symfony\Component\VarDumper\Dumper\CliDumper;
19 * A PsySH-specialized CliDumper.
21 class Dumper extends CliDumper
24 private $forceArrayIndexes;
26 protected static $onlyControlCharsRx = '/^[\x00-\x1F\x7F]+$/';
27 protected static $controlCharsRx = '/([\x00-\x1F\x7F]+)/';
28 protected static $controlCharsMap = [
38 public function __construct(OutputFormatter $formatter, $forceArrayIndexes = false)
40 $this->formatter = $formatter;
41 $this->forceArrayIndexes = $forceArrayIndexes;
42 parent::__construct();
43 $this->setColors(false);
49 public function enterHash(Cursor $cursor, $type, $class, $hasChild)
51 if (Cursor::HASH_INDEXED === $type || Cursor::HASH_ASSOC === $type) {
54 parent::enterHash($cursor, $type, $class, $hasChild);
60 protected function dumpKey(Cursor $cursor)
62 if ($this->forceArrayIndexes || Cursor::HASH_INDEXED !== $cursor->hashType) {
63 parent::dumpKey($cursor);
67 protected function style($style, $value, $attr = [])
69 if ('ref' === $style) {
70 $value = \strtr($value, '@', '#');
74 $map = self::$controlCharsMap;
75 $cchr = $this->styles['cchr'];
77 $chunks = \preg_split(self::$controlCharsRx, $value, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
78 foreach ($chunks as $chunk) {
79 if (\preg_match(self::$onlyControlCharsRx, $chunk)) {
83 $chars .= isset($map[$chunk[$i]]) ? $map[$chunk[$i]] : \sprintf('\x%02X', \ord($chunk[$i]));
84 } while (isset($chunk[++$i]));
86 $chars = $this->formatter->escape($chars);
87 $styled .= "<{$cchr}>{$chars}</{$cchr}>";
89 $styled .= $this->formatter->escape($chunk);
93 $style = $this->styles[$style];
95 return "<{$style}>{$styled}</{$style}>";
101 protected function dumpLine($depth, $endOfValue = false)
103 if ($endOfValue && 0 < $depth) {
106 $this->line = $this->formatter->format($this->line);
107 parent::dumpLine($depth, $endOfValue);