Version 1
[yaffs-website] / vendor / symfony / var-dumper / Dumper / AbstractDumper.php
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 static $defaultOutput = 'php://output';
25
26     protected $line = '';
27     protected $lineDumper;
28     protected $outputStream;
29     protected $decimalPoint; // This is locale dependent
30     protected $indentPad = '  ';
31
32     private $charset;
33
34     /**
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
37      */
38     public function __construct($output = null, $charset = null)
39     {
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;
46         }
47     }
48
49     /**
50      * Sets the output destination of the dumps.
51      *
52      * @param callable|resource|string $output A line dumper callable, an opened stream or an output path
53      *
54      * @return callable|resource|string The previous output destination
55      */
56     public function setOutput($output)
57     {
58         $prev = null !== $this->outputStream ? $this->outputStream : $this->lineDumper;
59
60         if (is_callable($output)) {
61             $this->outputStream = null;
62             $this->lineDumper = $output;
63         } else {
64             if (is_string($output)) {
65                 $output = fopen($output, 'wb');
66             }
67             $this->outputStream = $output;
68             $this->lineDumper = array($this, 'echoLine');
69         }
70
71         return $prev;
72     }
73
74     /**
75      * Sets the default character encoding to use for non-UTF8 strings.
76      *
77      * @param string $charset The default character encoding to use for non-UTF8 strings
78      *
79      * @return string The previous charset
80      */
81     public function setCharset($charset)
82     {
83         $prev = $this->charset;
84
85         $charset = strtoupper($charset);
86         $charset = null === $charset || 'UTF-8' === $charset || 'UTF8' === $charset ? 'CP1252' : $charset;
87
88         $this->charset = $charset;
89
90         return $prev;
91     }
92
93     /**
94      * Sets the indentation pad string.
95      *
96      * @param string $pad A string the will be prepended to dumped lines, repeated by nesting level
97      *
98      * @return string The indent pad
99      */
100     public function setIndentPad($pad)
101     {
102         $prev = $this->indentPad;
103         $this->indentPad = $pad;
104
105         return $prev;
106     }
107
108     /**
109      * Dumps a Data object.
110      *
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
113      */
114     public function dump(Data $data, $output = null)
115     {
116         $exception = null;
117         if ($output) {
118             $prevOutput = $this->setOutput($output);
119         }
120         try {
121             $data->dump($this);
122             $this->dumpLine(-1);
123         } catch (\Exception $exception) {
124             // Re-thrown below
125         } catch (\Throwable $exception) {
126             // Re-thrown below
127         }
128         if ($output) {
129             $this->setOutput($prevOutput);
130         }
131         if (null !== $exception) {
132             throw $exception;
133         }
134     }
135
136     /**
137      * Dumps the current line.
138      *
139      * @param int $depth The recursive depth in the dumped structure for the line being dumped
140      */
141     protected function dumpLine($depth)
142     {
143         call_user_func($this->lineDumper, $this->line, $depth, $this->indentPad);
144         $this->line = '';
145     }
146
147     /**
148      * Generic line dumper callback.
149      *
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
153      */
154     protected function echoLine($line, $depth, $indentPad)
155     {
156         if (-1 !== $depth) {
157             fwrite($this->outputStream, str_repeat($indentPad, $depth).$line."\n");
158         }
159     }
160
161     /**
162      * Converts a non-UTF-8 string to UTF-8.
163      *
164      * @param string $s The non-UTF-8 string to convert
165      *
166      * @return string The string converted to UTF-8
167      */
168     protected function utf8Encode($s)
169     {
170         if (false !== $c = @iconv($this->charset, 'UTF-8', $s)) {
171             return $c;
172         }
173         if ('CP1252' !== $this->charset && false !== $c = @iconv('CP1252', 'UTF-8', $s)) {
174             return $c;
175         }
176
177         return iconv('CP850', 'UTF-8', $s);
178     }
179 }