Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
200 lars 6
 * (c) 2012-2023 Justin Hileman
148 lars 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
    private $forceArrayIndexes;
25
 
26
    protected static $onlyControlCharsRx = '/^[\x00-\x1F\x7F]+$/';
27
    protected static $controlCharsRx = '/([\x00-\x1F\x7F]+)/';
28
    protected static $controlCharsMap = [
29
        "\0"   => '\0',
30
        "\t"   => '\t',
31
        "\n"   => '\n',
32
        "\v"   => '\v',
33
        "\f"   => '\f',
34
        "\r"   => '\r',
35
        "\033" => '\e',
36
    ];
37
 
38
    public function __construct(OutputFormatter $formatter, $forceArrayIndexes = false)
39
    {
40
        $this->formatter = $formatter;
41
        $this->forceArrayIndexes = $forceArrayIndexes;
42
        parent::__construct();
43
        $this->setColors(false);
44
    }
45
 
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function enterHash(Cursor $cursor, $type, $class, $hasChild)
50
    {
51
        if (Cursor::HASH_INDEXED === $type || Cursor::HASH_ASSOC === $type) {
52
            $class = 0;
53
        }
54
        parent::enterHash($cursor, $type, $class, $hasChild);
55
    }
56
 
57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected function dumpKey(Cursor $cursor)
61
    {
62
        if ($this->forceArrayIndexes || Cursor::HASH_INDEXED !== $cursor->hashType) {
63
            parent::dumpKey($cursor);
64
        }
65
    }
66
 
67
    protected function style($style, $value, $attr = []): string
68
    {
69
        if ('ref' === $style) {
70
            $value = \strtr($value, '@', '#');
71
        }
72
 
73
        $styled = '';
74
        $map = self::$controlCharsMap;
75
        $cchr = $this->styles['cchr'];
76
 
77
        $chunks = \preg_split(self::$controlCharsRx, $value, -1, \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_DELIM_CAPTURE);
78
        foreach ($chunks as $chunk) {
79
            if (\preg_match(self::$onlyControlCharsRx, $chunk)) {
80
                $chars = '';
81
                $i = 0;
82
                do {
83
                    $chars .= isset($map[$chunk[$i]]) ? $map[$chunk[$i]] : \sprintf('\x%02X', \ord($chunk[$i]));
84
                } while (isset($chunk[++$i]));
85
 
86
                $chars = $this->formatter->escape($chars);
87
                $styled .= "<{$cchr}>{$chars}</{$cchr}>";
88
            } else {
89
                $styled .= $this->formatter->escape($chunk);
90
            }
91
        }
92
 
93
        $style = $this->styles[$style];
94
 
95
        return "<{$style}>{$styled}</{$style}>";
96
    }
97
 
98
    /**
99
     * {@inheritdoc}
100
     */
101
    protected function dumpLine($depth, $endOfValue = false)
102
    {
103
        if ($endOfValue && 0 < $depth) {
104
            $this->line .= ',';
105
        }
106
        $this->line = $this->formatter->format($this->line);
107
        parent::dumpLine($depth, $endOfValue);
108
    }
109
}