Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\VarDumper\Dumper;
13
 
14
use Symfony\Component\VarDumper\Cloner\Data;
15
use Symfony\Component\VarDumper\Cloner\DumperInterface;
16
 
17
/**
18
 * Abstract mechanism for dumping a Data object.
19
 *
20
 * @author Nicolas Grekas <p@tchwork.com>
21
 */
22
abstract class AbstractDumper implements DataDumperInterface, DumperInterface
23
{
24
    public const DUMP_LIGHT_ARRAY = 1;
25
    public const DUMP_STRING_LENGTH = 2;
26
    public const DUMP_COMMA_SEPARATOR = 4;
27
    public const DUMP_TRAILING_COMMA = 8;
28
 
29
    public static $defaultOutput = 'php://output';
30
 
31
    protected $line = '';
32
    protected $lineDumper;
33
    protected $outputStream;
34
    protected $decimalPoint = '.';
35
    protected $indentPad = '  ';
36
    protected $flags;
37
 
38
    private string $charset = '';
39
 
40
    /**
41
     * @param callable|resource|string|null $output  A line dumper callable, an opened stream or an output path, defaults to static::$defaultOutput
42
     * @param string|null                   $charset The default character encoding to use for non-UTF8 strings
43
     * @param int                           $flags   A bit field of static::DUMP_* constants to fine tune dumps representation
44
     */
45
    public function __construct($output = null, string $charset = null, int $flags = 0)
46
    {
47
        $this->flags = $flags;
48
        $this->setCharset($charset ?: \ini_get('php.output_encoding') ?: \ini_get('default_charset') ?: 'UTF-8');
49
        $this->setOutput($output ?: static::$defaultOutput);
50
        if (!$output && \is_string(static::$defaultOutput)) {
51
            static::$defaultOutput = $this->outputStream;
52
        }
53
    }
54
 
55
    /**
56
     * Sets the output destination of the dumps.
57
     *
58
     * @param callable|resource|string $output A line dumper callable, an opened stream or an output path
59
     *
60
     * @return callable|resource|string The previous output destination
61
     */
62
    public function setOutput($output)
63
    {
64
        $prev = $this->outputStream ?? $this->lineDumper;
65
 
66
        if (\is_callable($output)) {
67
            $this->outputStream = null;
68
            $this->lineDumper = $output;
69
        } else {
70
            if (\is_string($output)) {
71
                $output = fopen($output, 'w');
72
            }
73
            $this->outputStream = $output;
74
            $this->lineDumper = $this->echoLine(...);
75
        }
76
 
77
        return $prev;
78
    }
79
 
80
    /**
81
     * Sets the default character encoding to use for non-UTF8 strings.
82
     *
83
     * @return string The previous charset
84
     */
85
    public function setCharset(string $charset): string
86
    {
87
        $prev = $this->charset;
88
 
89
        $charset = strtoupper($charset);
90
        $charset = null === $charset || 'UTF-8' === $charset || 'UTF8' === $charset ? 'CP1252' : $charset;
91
 
92
        $this->charset = $charset;
93
 
94
        return $prev;
95
    }
96
 
97
    /**
98
     * Sets the indentation pad string.
99
     *
100
     * @param string $pad A string that will be prepended to dumped lines, repeated by nesting level
101
     *
102
     * @return string The previous indent pad
103
     */
104
    public function setIndentPad(string $pad): string
105
    {
106
        $prev = $this->indentPad;
107
        $this->indentPad = $pad;
108
 
109
        return $prev;
110
    }
111
 
112
    /**
113
     * Dumps a Data object.
114
     *
115
     * @param callable|resource|string|true|null $output A line dumper callable, an opened stream, an output path or true to return the dump
116
     *
117
     * @return string|null The dump as string when $output is true
118
     */
119
    public function dump(Data $data, $output = null): ?string
120
    {
121
        if ($locale = $this->flags & (self::DUMP_COMMA_SEPARATOR | self::DUMP_TRAILING_COMMA) ? setlocale(\LC_NUMERIC, 0) : null) {
122
            setlocale(\LC_NUMERIC, 'C');
123
        }
124
 
125
        if ($returnDump = true === $output) {
126
            $output = fopen('php://memory', 'r+');
127
        }
128
        if ($output) {
129
            $prevOutput = $this->setOutput($output);
130
        }
131
        try {
132
            $data->dump($this);
133
            $this->dumpLine(-1);
134
 
135
            if ($returnDump) {
136
                $result = stream_get_contents($output, -1, 0);
137
                fclose($output);
138
 
139
                return $result;
140
            }
141
        } finally {
142
            if ($output) {
143
                $this->setOutput($prevOutput);
144
            }
145
            if ($locale) {
146
                setlocale(\LC_NUMERIC, $locale);
147
            }
148
        }
149
 
150
        return null;
151
    }
152
 
153
    /**
154
     * Dumps the current line.
155
     *
156
     * @param int $depth The recursive depth in the dumped structure for the line being dumped,
157
     *                   or -1 to signal the end-of-dump to the line dumper callable
158
     */
159
    protected function dumpLine(int $depth)
160
    {
161
        ($this->lineDumper)($this->line, $depth, $this->indentPad);
162
        $this->line = '';
163
    }
164
 
165
    /**
166
     * Generic line dumper callback.
167
     */
168
    protected function echoLine(string $line, int $depth, string $indentPad)
169
    {
170
        if (-1 !== $depth) {
171
            fwrite($this->outputStream, str_repeat($indentPad, $depth).$line."\n");
172
        }
173
    }
174
 
175
    /**
176
     * Converts a non-UTF-8 string to UTF-8.
177
     */
178
    protected function utf8Encode(?string $s): ?string
179
    {
180
        if (null === $s || preg_match('//u', $s)) {
181
            return $s;
182
        }
183
 
184
        if (!\function_exists('iconv')) {
185
            throw new \RuntimeException('Unable to convert a non-UTF-8 string to UTF-8: required function iconv() does not exist. You should install ext-iconv or symfony/polyfill-iconv.');
186
        }
187
 
188
        if (false !== $c = @iconv($this->charset, 'UTF-8', $s)) {
189
            return $c;
190
        }
191
        if ('CP1252' !== $this->charset && false !== $c = @iconv('CP1252', 'UTF-8', $s)) {
192
            return $c;
193
        }
194
 
195
        return iconv('CP850', 'UTF-8', $s);
196
    }
197
}