Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | 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
 *
6
 * (c) 2012-2022 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\Caster\Caster;
16
use Symfony\Component\VarDumper\Cloner\Stub;
17
 
18
/**
19
 * A Presenter service.
20
 */
21
class Presenter
22
{
23
    const VERBOSE = 1;
24
 
25
    private $cloner;
26
    private $dumper;
27
    private $exceptionsImportants = [
28
        "\0*\0message",
29
        "\0*\0code",
30
        "\0*\0file",
31
        "\0*\0line",
32
        "\0Exception\0previous",
33
    ];
34
    private $styles = [
35
        'num'       => 'number',
36
        'integer'   => 'integer',
37
        'float'     => 'float',
38
        'const'     => 'const',
39
        'str'       => 'string',
40
        'cchr'      => 'default',
41
        'note'      => 'class',
42
        'ref'       => 'default',
43
        'public'    => 'public',
44
        'protected' => 'protected',
45
        'private'   => 'private',
46
        'meta'      => 'comment',
47
        'key'       => 'comment',
48
        'index'     => 'number',
49
    ];
50
 
51
    public function __construct(OutputFormatter $formatter, $forceArrayIndexes = false)
52
    {
53
        // Work around https://github.com/symfony/symfony/issues/23572
54
        $oldLocale = \setlocale(\LC_NUMERIC, 0);
55
        \setlocale(\LC_NUMERIC, 'C');
56
 
57
        $this->dumper = new Dumper($formatter, $forceArrayIndexes);
58
        $this->dumper->setStyles($this->styles);
59
 
60
        // Now put the locale back
61
        \setlocale(\LC_NUMERIC, $oldLocale);
62
 
63
        $this->cloner = new Cloner();
64
        $this->cloner->addCasters(['*' => function ($obj, array $a, Stub $stub, $isNested, $filter = 0) {
65
            if ($filter || $isNested) {
66
                if ($obj instanceof \Throwable) {
67
                    $a = Caster::filter($a, Caster::EXCLUDE_NOT_IMPORTANT | Caster::EXCLUDE_EMPTY, $this->exceptionsImportants);
68
                } else {
69
                    $a = Caster::filter($a, Caster::EXCLUDE_PROTECTED | Caster::EXCLUDE_PRIVATE);
70
                }
71
            }
72
 
73
            return $a;
74
        }]);
75
    }
76
 
77
    /**
78
     * Register casters.
79
     *
80
     * @see http://symfony.com/doc/current/components/var_dumper/advanced.html#casters
81
     *
82
     * @param callable[] $casters A map of casters
83
     */
84
    public function addCasters(array $casters)
85
    {
86
        $this->cloner->addCasters($casters);
87
    }
88
 
89
    /**
90
     * Present a reference to the value.
91
     *
92
     * @param mixed $value
93
     *
94
     * @return string
95
     */
96
    public function presentRef($value): string
97
    {
98
        return $this->present($value, 0);
99
    }
100
 
101
    /**
102
     * Present a full representation of the value.
103
     *
104
     * If $depth is 0, the value will be presented as a ref instead.
105
     *
106
     * @param mixed $value
107
     * @param int   $depth   (default: null)
108
     * @param int   $options One of Presenter constants
109
     *
110
     * @return string
111
     */
112
    public function present($value, int $depth = null, int $options = 0): string
113
    {
114
        $data = $this->cloner->cloneVar($value, !($options & self::VERBOSE) ? Caster::EXCLUDE_VERBOSE : 0);
115
 
116
        if (null !== $depth) {
117
            $data = $data->withMaxDepth($depth);
118
        }
119
 
120
        // Work around https://github.com/symfony/symfony/issues/23572
121
        $oldLocale = \setlocale(\LC_NUMERIC, 0);
122
        \setlocale(\LC_NUMERIC, 'C');
123
 
124
        $output = '';
125
        $this->dumper->dump($data, function ($line, $depth) use (&$output) {
126
            if ($depth >= 0) {
127
                if ('' !== $output) {
128
                    $output .= \PHP_EOL;
129
                }
130
                $output .= \str_repeat('  ', $depth).$line;
131
            }
132
        });
133
 
134
        // Now put the locale back
135
        \setlocale(\LC_NUMERIC, $oldLocale);
136
 
137
        return OutputFormatter::escape($output);
138
    }
139
}