4 * This file is part of Psy Shell.
6 * (c) 2012-2017 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
25 protected static $onlyControlCharsRx = '/^[\x00-\x1F\x7F]+$/';
26 protected static $controlCharsRx = '/([\x00-\x1F\x7F]+)/';
27 protected static $controlCharsMap = array(
37 public function __construct(OutputFormatter $formatter)
39 $this->formatter = $formatter;
40 parent::__construct();
41 $this->setColors(false);
47 public function enterHash(Cursor $cursor, $type, $class, $hasChild)
49 if (Cursor::HASH_INDEXED === $type || Cursor::HASH_ASSOC === $type) {
52 parent::enterHash($cursor, $type, $class, $hasChild);
58 protected function dumpKey(Cursor $cursor)
60 if (Cursor::HASH_INDEXED !== $cursor->hashType) {
61 parent::dumpKey($cursor);
65 protected function style($style, $value, $attr = array())
67 if ('ref' === $style) {
68 $value = strtr($value, '@', '#');
72 $map = self::$controlCharsMap;
73 $cchr = $this->styles['cchr'];
75 $chunks = preg_split(self::$controlCharsRx, $value, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
76 foreach ($chunks as $chunk) {
77 if (preg_match(self::$onlyControlCharsRx, $chunk)) {
81 $chars .= isset($map[$chunk[$i]]) ? $map[$chunk[$i]] : sprintf('\x%02X', ord($chunk[$i]));
82 } while (isset($chunk[++$i]));
84 $chars = $this->formatter->escape($chars);
85 $styled .= "<{$cchr}>{$chars}</{$cchr}>";
87 $styled .= $this->formatter->escape($chunk);
91 $style = $this->styles[$style];
93 return "<{$style}>{$styled}</{$style}>";
99 protected function dumpLine($depth, $endOfValue = false)
101 if ($endOfValue && 0 < $depth) {
104 $this->line = $this->formatter->format($this->line);
105 parent::dumpLine($depth, $endOfValue);