comparison vendor/symfony/var-dumper/Dumper/AbstractDumper.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 const DUMP_LIGHT_ARRAY = 1;
25 const DUMP_STRING_LENGTH = 2;
26 const DUMP_COMMA_SEPARATOR = 4;
27 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; // This is locale dependent
35 protected $indentPad = ' ';
36 protected $flags;
37
38 private $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 $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, $charset = null, $flags = 0)
46 {
47 $this->flags = (int) $flags;
48 $this->setCharset($charset ?: ini_get('php.output_encoding') ?: ini_get('default_charset') ?: 'UTF-8');
49 $this->decimalPoint = localeconv();
50 $this->decimalPoint = $this->decimalPoint['decimal_point'];
51 $this->setOutput($output ?: static::$defaultOutput);
52 if (!$output && is_string(static::$defaultOutput)) {
53 static::$defaultOutput = $this->outputStream;
54 }
55 }
56
57 /**
58 * Sets the output destination of the dumps.
59 *
60 * @param callable|resource|string $output A line dumper callable, an opened stream or an output path
61 *
62 * @return callable|resource|string The previous output destination
63 */
64 public function setOutput($output)
65 {
66 $prev = null !== $this->outputStream ? $this->outputStream : $this->lineDumper;
67
68 if (is_callable($output)) {
69 $this->outputStream = null;
70 $this->lineDumper = $output;
71 } else {
72 if (is_string($output)) {
73 $output = fopen($output, 'wb');
74 }
75 $this->outputStream = $output;
76 $this->lineDumper = array($this, 'echoLine');
77 }
78
79 return $prev;
80 }
81
82 /**
83 * Sets the default character encoding to use for non-UTF8 strings.
84 *
85 * @param string $charset The default character encoding to use for non-UTF8 strings
86 *
87 * @return string The previous charset
88 */
89 public function setCharset($charset)
90 {
91 $prev = $this->charset;
92
93 $charset = strtoupper($charset);
94 $charset = null === $charset || 'UTF-8' === $charset || 'UTF8' === $charset ? 'CP1252' : $charset;
95
96 $this->charset = $charset;
97
98 return $prev;
99 }
100
101 /**
102 * Sets the indentation pad string.
103 *
104 * @param string $pad A string the will be prepended to dumped lines, repeated by nesting level
105 *
106 * @return string The indent pad
107 */
108 public function setIndentPad($pad)
109 {
110 $prev = $this->indentPad;
111 $this->indentPad = $pad;
112
113 return $prev;
114 }
115
116 /**
117 * Dumps a Data object.
118 *
119 * @param Data $data A Data object
120 * @param callable|resource|string|true|null $output A line dumper callable, an opened stream, an output path or true to return the dump
121 *
122 * @return string|null The dump as string when $output is true
123 */
124 public function dump(Data $data, $output = null)
125 {
126 $this->decimalPoint = localeconv();
127 $this->decimalPoint = $this->decimalPoint['decimal_point'];
128
129 if ($locale = $this->flags & (self::DUMP_COMMA_SEPARATOR | self::DUMP_TRAILING_COMMA) ? setlocale(LC_NUMERIC, 0) : null) {
130 setlocale(LC_NUMERIC, 'C');
131 }
132
133 if ($returnDump = true === $output) {
134 $output = fopen('php://memory', 'r+b');
135 }
136 if ($output) {
137 $prevOutput = $this->setOutput($output);
138 }
139 try {
140 $data->dump($this);
141 $this->dumpLine(-1);
142
143 if ($returnDump) {
144 $result = stream_get_contents($output, -1, 0);
145 fclose($output);
146
147 return $result;
148 }
149 } finally {
150 if ($output) {
151 $this->setOutput($prevOutput);
152 }
153 if ($locale) {
154 setlocale(LC_NUMERIC, $locale);
155 }
156 }
157 }
158
159 /**
160 * Dumps the current line.
161 *
162 * @param int $depth The recursive depth in the dumped structure for the line being dumped,
163 * or -1 to signal the end-of-dump to the line dumper callable
164 */
165 protected function dumpLine($depth)
166 {
167 call_user_func($this->lineDumper, $this->line, $depth, $this->indentPad);
168 $this->line = '';
169 }
170
171 /**
172 * Generic line dumper callback.
173 *
174 * @param string $line The line to write
175 * @param int $depth The recursive depth in the dumped structure
176 * @param string $indentPad The line indent pad
177 */
178 protected function echoLine($line, $depth, $indentPad)
179 {
180 if (-1 !== $depth) {
181 fwrite($this->outputStream, str_repeat($indentPad, $depth).$line."\n");
182 }
183 }
184
185 /**
186 * Converts a non-UTF-8 string to UTF-8.
187 *
188 * @param string $s The non-UTF-8 string to convert
189 *
190 * @return string The string converted to UTF-8
191 */
192 protected function utf8Encode($s)
193 {
194 if (preg_match('//u', $s)) {
195 return $s;
196 }
197
198 if (!function_exists('iconv')) {
199 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.');
200 }
201
202 if (false !== $c = @iconv($this->charset, 'UTF-8', $s)) {
203 return $c;
204 }
205 if ('CP1252' !== $this->charset && false !== $c = @iconv('CP1252', 'UTF-8', $s)) {
206 return $c;
207 }
208
209 return iconv('CP850', 'UTF-8', $s);
210 }
211 }