Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 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; // This is locale dependent |
| 35 | protected $indentPad = ' '; |
| 36 | protected $flags; |
| 37 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 38 | private string $charset = ''; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 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->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 = $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, 'w'); |
| 74 | } |
| 75 | $this->outputStream = $output; |
| 76 | $this->lineDumper = [$this, 'echoLine']; |
| 77 | } |
| 78 | |
| 79 | return $prev; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Sets the default character encoding to use for non-UTF8 strings. |
| 84 | * |
| 85 | * @return string The previous charset |
| 86 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 87 | public function setCharset(string $charset): string |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 88 | { |
| 89 | $prev = $this->charset; |
| 90 | |
| 91 | $charset = strtoupper($charset); |
| 92 | $charset = null === $charset || 'UTF-8' === $charset || 'UTF8' === $charset ? 'CP1252' : $charset; |
| 93 | |
| 94 | $this->charset = $charset; |
| 95 | |
| 96 | return $prev; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Sets the indentation pad string. |
| 101 | * |
| 102 | * @param string $pad A string that will be prepended to dumped lines, repeated by nesting level |
| 103 | * |
| 104 | * @return string The previous indent pad |
| 105 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 106 | public function setIndentPad(string $pad): string |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 107 | { |
| 108 | $prev = $this->indentPad; |
| 109 | $this->indentPad = $pad; |
| 110 | |
| 111 | return $prev; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Dumps a Data object. |
| 116 | * |
| 117 | * @param callable|resource|string|true|null $output A line dumper callable, an opened stream, an output path or true to return the dump |
| 118 | * |
| 119 | * @return string|null The dump as string when $output is true |
| 120 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 121 | public function dump(Data $data, $output = null): ?string |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 122 | { |
| 123 | $this->decimalPoint = localeconv(); |
| 124 | $this->decimalPoint = $this->decimalPoint['decimal_point']; |
| 125 | |
| 126 | if ($locale = $this->flags & (self::DUMP_COMMA_SEPARATOR | self::DUMP_TRAILING_COMMA) ? setlocale(\LC_NUMERIC, 0) : null) { |
| 127 | setlocale(\LC_NUMERIC, 'C'); |
| 128 | } |
| 129 | |
| 130 | if ($returnDump = true === $output) { |
| 131 | $output = fopen('php://memory', 'r+'); |
| 132 | } |
| 133 | if ($output) { |
| 134 | $prevOutput = $this->setOutput($output); |
| 135 | } |
| 136 | try { |
| 137 | $data->dump($this); |
| 138 | $this->dumpLine(-1); |
| 139 | |
| 140 | if ($returnDump) { |
| 141 | $result = stream_get_contents($output, -1, 0); |
| 142 | fclose($output); |
| 143 | |
| 144 | return $result; |
| 145 | } |
| 146 | } finally { |
| 147 | if ($output) { |
| 148 | $this->setOutput($prevOutput); |
| 149 | } |
| 150 | if ($locale) { |
| 151 | setlocale(\LC_NUMERIC, $locale); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return null; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Dumps the current line. |
| 160 | * |
| 161 | * @param int $depth The recursive depth in the dumped structure for the line being dumped, |
| 162 | * or -1 to signal the end-of-dump to the line dumper callable |
| 163 | */ |
| 164 | protected function dumpLine(int $depth) |
| 165 | { |
| 166 | ($this->lineDumper)($this->line, $depth, $this->indentPad); |
| 167 | $this->line = ''; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Generic line dumper callback. |
| 172 | */ |
| 173 | protected function echoLine(string $line, int $depth, string $indentPad) |
| 174 | { |
| 175 | if (-1 !== $depth) { |
| 176 | fwrite($this->outputStream, str_repeat($indentPad, $depth).$line."\n"); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Converts a non-UTF-8 string to UTF-8. |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 182 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 183 | protected function utf8Encode(?string $s): ?string |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 184 | { |
| 185 | if (null === $s || preg_match('//u', $s)) { |
| 186 | return $s; |
| 187 | } |
| 188 | |
| 189 | if (!\function_exists('iconv')) { |
| 190 | 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.'); |
| 191 | } |
| 192 | |
| 193 | if (false !== $c = @iconv($this->charset, 'UTF-8', $s)) { |
| 194 | return $c; |
| 195 | } |
| 196 | if ('CP1252' !== $this->charset && false !== $c = @iconv('CP1252', 'UTF-8', $s)) { |
| 197 | return $c; |
| 198 | } |
| 199 | |
| 200 | return iconv('CP850', 'UTF-8', $s); |
| 201 | } |
| 202 | } |