Version 1
[yaffs-website] / vendor / psy / psysh / src / Psy / VarDumper / Dumper.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\VarDumper;
13
14 use Symfony\Component\Console\Formatter\OutputFormatter;
15 use Symfony\Component\VarDumper\Cloner\Cursor;
16 use Symfony\Component\VarDumper\Dumper\CliDumper;
17
18 /**
19  * A PsySH-specialized CliDumper.
20  */
21 class Dumper extends CliDumper
22 {
23     private $formatter;
24
25     protected static $onlyControlCharsRx = '/^[\x00-\x1F\x7F]+$/';
26     protected static $controlCharsRx = '/([\x00-\x1F\x7F]+)/';
27     protected static $controlCharsMap = array(
28         "\0"   => '\0',
29         "\t"   => '\t',
30         "\n"   => '\n',
31         "\v"   => '\v',
32         "\f"   => '\f',
33         "\r"   => '\r',
34         "\033" => '\e',
35     );
36
37     public function __construct(OutputFormatter $formatter)
38     {
39         $this->formatter = $formatter;
40         parent::__construct();
41         $this->setColors(false);
42     }
43
44     /**
45      * {@inheritdoc}
46      */
47     public function enterHash(Cursor $cursor, $type, $class, $hasChild)
48     {
49         if (Cursor::HASH_INDEXED === $type || Cursor::HASH_ASSOC === $type) {
50             $class = 0;
51         }
52         parent::enterHash($cursor, $type, $class, $hasChild);
53     }
54
55     /**
56      * {@inheritdoc}
57      */
58     protected function dumpKey(Cursor $cursor)
59     {
60         if (Cursor::HASH_INDEXED !== $cursor->hashType) {
61             parent::dumpKey($cursor);
62         }
63     }
64
65     protected function style($style, $value, $attr = array())
66     {
67         if ('ref' === $style) {
68             $value = strtr($value, '@', '#');
69         }
70
71         $styled = '';
72         $map = self::$controlCharsMap;
73         $cchr = $this->styles['cchr'];
74
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)) {
78                 $chars = '';
79                 $i = 0;
80                 do {
81                     $chars .= isset($map[$chunk[$i]]) ? $map[$chunk[$i]] : sprintf('\x%02X', ord($chunk[$i]));
82                 } while (isset($chunk[++$i]));
83
84                 $chars = $this->formatter->escape($chars);
85                 $styled .= "<{$cchr}>{$chars}</{$cchr}>";
86             } else {
87                 $styled .= $this->formatter->escape($chunk);
88             }
89         }
90
91         $style = $this->styles[$style];
92
93         return "<{$style}>{$styled}</{$style}>";
94     }
95
96     /**
97      * {@inheritdoc}
98      */
99     protected function dumpLine($depth, $endOfValue = false)
100     {
101         if ($endOfValue && 0 < $depth) {
102             $this->line .= ',';
103         }
104         $this->line = $this->formatter->format($this->line);
105         parent::dumpLine($depth, $endOfValue);
106     }
107 }