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\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
    public function presentRef($value): string
95
    {
96
        return $this->present($value, 0);
97
    }
98
 
99
    /**
100
     * Present a full representation of the value.
101
     *
102
     * If $depth is 0, the value will be presented as a ref instead.
103
     *
104
     * @param mixed $value
105
     * @param int   $depth   (default: null)
106
     * @param int   $options One of Presenter constants
107
     */
108
    public function present($value, int $depth = null, int $options = 0): string
109
    {
110
        $data = $this->cloner->cloneVar($value, !($options & self::VERBOSE) ? Caster::EXCLUDE_VERBOSE : 0);
111
 
112
        if (null !== $depth) {
113
            $data = $data->withMaxDepth($depth);
114
        }
115
 
116
        // Work around https://github.com/symfony/symfony/issues/23572
117
        $oldLocale = \setlocale(\LC_NUMERIC, 0);
118
        \setlocale(\LC_NUMERIC, 'C');
119
 
120
        $output = '';
121
        $this->dumper->dump($data, function ($line, $depth) use (&$output) {
122
            if ($depth >= 0) {
123
                if ('' !== $output) {
124
                    $output .= \PHP_EOL;
125
                }
126
                $output .= \str_repeat('  ', $depth).$line;
127
            }
128
        });
129
 
130
        // Now put the locale back
131
        \setlocale(\LC_NUMERIC, $oldLocale);
132
 
133
        return OutputFormatter::escape($output);
134
    }
135
}