4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\VarDumper\Dumper;
14 use Symfony\Component\VarDumper\Cloner\Data;
15 use Symfony\Component\VarDumper\Cloner\DumperInterface;
18 * Abstract mechanism for dumping a Data object.
20 * @author Nicolas Grekas <p@tchwork.com>
22 abstract class AbstractDumper implements DataDumperInterface, DumperInterface
24 public static $defaultOutput = 'php://output';
27 protected $lineDumper;
28 protected $outputStream;
29 protected $decimalPoint; // This is locale dependent
30 protected $indentPad = ' ';
35 * @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path, defaults to static::$defaultOutput
36 * @param string $charset The default character encoding to use for non-UTF8 strings
38 public function __construct($output = null, $charset = null)
40 $this->setCharset($charset ?: ini_get('php.output_encoding') ?: ini_get('default_charset') ?: 'UTF-8');
41 $this->decimalPoint = (string) 0.5;
42 $this->decimalPoint = $this->decimalPoint[1];
43 $this->setOutput($output ?: static::$defaultOutput);
44 if (!$output && is_string(static::$defaultOutput)) {
45 static::$defaultOutput = $this->outputStream;
50 * Sets the output destination of the dumps.
52 * @param callable|resource|string $output A line dumper callable, an opened stream or an output path
54 * @return callable|resource|string The previous output destination
56 public function setOutput($output)
58 $prev = null !== $this->outputStream ? $this->outputStream : $this->lineDumper;
60 if (is_callable($output)) {
61 $this->outputStream = null;
62 $this->lineDumper = $output;
64 if (is_string($output)) {
65 $output = fopen($output, 'wb');
67 $this->outputStream = $output;
68 $this->lineDumper = array($this, 'echoLine');
75 * Sets the default character encoding to use for non-UTF8 strings.
77 * @param string $charset The default character encoding to use for non-UTF8 strings
79 * @return string The previous charset
81 public function setCharset($charset)
83 $prev = $this->charset;
85 $charset = strtoupper($charset);
86 $charset = null === $charset || 'UTF-8' === $charset || 'UTF8' === $charset ? 'CP1252' : $charset;
88 $this->charset = $charset;
94 * Sets the indentation pad string.
96 * @param string $pad A string the will be prepended to dumped lines, repeated by nesting level
98 * @return string The indent pad
100 public function setIndentPad($pad)
102 $prev = $this->indentPad;
103 $this->indentPad = $pad;
109 * Dumps a Data object.
111 * @param Data $data A Data object
112 * @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path
114 public function dump(Data $data, $output = null)
118 $prevOutput = $this->setOutput($output);
123 } catch (\Exception $exception) {
125 } catch (\Throwable $exception) {
129 $this->setOutput($prevOutput);
131 if (null !== $exception) {
137 * Dumps the current line.
139 * @param int $depth The recursive depth in the dumped structure for the line being dumped
141 protected function dumpLine($depth)
143 call_user_func($this->lineDumper, $this->line, $depth, $this->indentPad);
148 * Generic line dumper callback.
150 * @param string $line The line to write
151 * @param int $depth The recursive depth in the dumped structure
152 * @param string $indentPad The line indent pad
154 protected function echoLine($line, $depth, $indentPad)
157 fwrite($this->outputStream, str_repeat($indentPad, $depth).$line."\n");
162 * Converts a non-UTF-8 string to UTF-8.
164 * @param string $s The non-UTF-8 string to convert
166 * @return string The string converted to UTF-8
168 protected function utf8Encode($s)
170 if (!function_exists('iconv')) {
171 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.');
173 if (false !== $c = @iconv($this->charset, 'UTF-8', $s)) {
176 if ('CP1252' !== $this->charset && false !== $c = @iconv('CP1252', 'UTF-8', $s)) {
180 return iconv('CP850', 'UTF-8', $s);