AbstractDumper.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarDumper\Dumper;
  11. use Symfony\Component\VarDumper\Cloner\Data;
  12. use Symfony\Component\VarDumper\Cloner\DumperInterface;
  13. /**
  14. * Abstract mechanism for dumping a Data object.
  15. *
  16. * @author Nicolas Grekas <p@tchwork.com>
  17. */
  18. abstract class AbstractDumper implements DataDumperInterface, DumperInterface
  19. {
  20. public const DUMP_LIGHT_ARRAY = 1;
  21. public const DUMP_STRING_LENGTH = 2;
  22. public const DUMP_COMMA_SEPARATOR = 4;
  23. public const DUMP_TRAILING_COMMA = 8;
  24. /** @var callable|resource|string|null */
  25. public static $defaultOutput = 'php://output';
  26. protected $line = '';
  27. /** @var callable|null */
  28. protected $lineDumper;
  29. /** @var resource|null */
  30. protected $outputStream;
  31. protected $decimalPoint = '.';
  32. protected $indentPad = ' ';
  33. protected $flags;
  34. private string $charset = '';
  35. /**
  36. * @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path, defaults to static::$defaultOutput
  37. * @param string|null $charset The default character encoding to use for non-UTF8 strings
  38. * @param int $flags A bit field of static::DUMP_* constants to fine tune dumps representation
  39. */
  40. public function __construct($output = null, ?string $charset = null, int $flags = 0)
  41. {
  42. $this->flags = $flags;
  43. $this->setCharset($charset ?: \ini_get('php.output_encoding') ?: \ini_get('default_charset') ?: 'UTF-8');
  44. $this->setOutput($output ?: static::$defaultOutput);
  45. if (!$output && \is_string(static::$defaultOutput)) {
  46. static::$defaultOutput = $this->outputStream;
  47. }
  48. }
  49. /**
  50. * Sets the output destination of the dumps.
  51. *
  52. * @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path
  53. *
  54. * @return callable|resource|string|null The previous output destination
  55. */
  56. public function setOutput($output)
  57. {
  58. $prev = $this->outputStream ?? $this->lineDumper;
  59. if (\is_callable($output)) {
  60. $this->outputStream = null;
  61. $this->lineDumper = $output;
  62. } else {
  63. if (\is_string($output)) {
  64. $output = fopen($output, 'w');
  65. }
  66. $this->outputStream = $output;
  67. $this->lineDumper = $this->echoLine(...);
  68. }
  69. return $prev;
  70. }
  71. /**
  72. * Sets the default character encoding to use for non-UTF8 strings.
  73. *
  74. * @return string The previous charset
  75. */
  76. public function setCharset(string $charset): string
  77. {
  78. $prev = $this->charset;
  79. $charset = strtoupper($charset);
  80. $charset = null === $charset || 'UTF-8' === $charset || 'UTF8' === $charset ? 'CP1252' : $charset;
  81. $this->charset = $charset;
  82. return $prev;
  83. }
  84. /**
  85. * Sets the indentation pad string.
  86. *
  87. * @param string $pad A string that will be prepended to dumped lines, repeated by nesting level
  88. *
  89. * @return string The previous indent pad
  90. */
  91. public function setIndentPad(string $pad): string
  92. {
  93. $prev = $this->indentPad;
  94. $this->indentPad = $pad;
  95. return $prev;
  96. }
  97. /**
  98. * Dumps a Data object.
  99. *
  100. * @param callable|resource|string|true|null $output A line dumper callable, an opened stream, an output path or true to return the dump
  101. *
  102. * @return string|null The dump as string when $output is true
  103. */
  104. public function dump(Data $data, $output = null): ?string
  105. {
  106. if ($locale = $this->flags & (self::DUMP_COMMA_SEPARATOR | self::DUMP_TRAILING_COMMA) ? setlocale(\LC_NUMERIC, 0) : null) {
  107. setlocale(\LC_NUMERIC, 'C');
  108. }
  109. if ($returnDump = true === $output) {
  110. $output = fopen('php://memory', 'r+');
  111. }
  112. if ($output) {
  113. $prevOutput = $this->setOutput($output);
  114. }
  115. try {
  116. $data->dump($this);
  117. $this->dumpLine(-1);
  118. if ($returnDump) {
  119. $result = stream_get_contents($output, -1, 0);
  120. fclose($output);
  121. return $result;
  122. }
  123. } finally {
  124. if ($output) {
  125. $this->setOutput($prevOutput);
  126. }
  127. if ($locale) {
  128. setlocale(\LC_NUMERIC, $locale);
  129. }
  130. }
  131. return null;
  132. }
  133. /**
  134. * Dumps the current line.
  135. *
  136. * @param int $depth The recursive depth in the dumped structure for the line being dumped,
  137. * or -1 to signal the end-of-dump to the line dumper callable
  138. *
  139. * @return void
  140. */
  141. protected function dumpLine(int $depth)
  142. {
  143. ($this->lineDumper)($this->line, $depth, $this->indentPad);
  144. $this->line = '';
  145. }
  146. /**
  147. * Generic line dumper callback.
  148. *
  149. * @return void
  150. */
  151. protected function echoLine(string $line, int $depth, string $indentPad)
  152. {
  153. if (-1 !== $depth) {
  154. fwrite($this->outputStream, str_repeat($indentPad, $depth).$line."\n");
  155. }
  156. }
  157. /**
  158. * Converts a non-UTF-8 string to UTF-8.
  159. */
  160. protected function utf8Encode(?string $s): ?string
  161. {
  162. if (null === $s || preg_match('//u', $s)) {
  163. return $s;
  164. }
  165. if (\function_exists('iconv')) {
  166. if (false !== $c = @iconv($this->charset, 'UTF-8', $s)) {
  167. return $c;
  168. }
  169. if ('CP1252' !== $this->charset && false !== $c = @iconv('CP1252', 'UTF-8', $s)) {
  170. return $c;
  171. }
  172. }
  173. $s .= $s;
  174. $len = \strlen($s);
  175. $mapCp1252 = false;
  176. for ($i = $len >> 1, $j = 0; $i < $len; ++$i, ++$j) {
  177. if ($s[$i] < "\x80") {
  178. $s[$j] = $s[$i];
  179. } elseif ($s[$i] < "\xC0") {
  180. $s[$j] = "\xC2";
  181. $s[++$j] = $s[$i];
  182. if ($s[$i] < "\xA0") {
  183. $mapCp1252 = true;
  184. }
  185. } else {
  186. $s[$j] = "\xC3";
  187. $s[++$j] = \chr(\ord($s[$i]) - 64);
  188. }
  189. }
  190. $s = substr($s, 0, $j);
  191. if (!$mapCp1252) {
  192. return $s;
  193. }
  194. return strtr($s, [
  195. "\xC2\x80" => '€', "\xC2\x82" => '‚', "\xC2\x83" => 'ƒ', "\xC2\x84" => '„',
  196. "\xC2\x85" => '…', "\xC2\x86" => '†', "\xC2\x87" => '‡', "\xC2\x88" => 'ˆ',
  197. "\xC2\x89" => '‰', "\xC2\x8A" => 'Š', "\xC2\x8B" => '‹', "\xC2\x8C" => 'Œ',
  198. "\xC2\x8D" => 'Ž', "\xC2\x91" => '‘', "\xC2\x92" => '’', "\xC2\x93" => '“',
  199. "\xC2\x94" => '”', "\xC2\x95" => '•', "\xC2\x96" => '–', "\xC2\x97" => '—',
  200. "\xC2\x98" => '˜', "\xC2\x99" => '™', "\xC2\x9A" => 'š', "\xC2\x9B" => '›',
  201. "\xC2\x9C" => 'œ', "\xC2\x9E" => 'ž',
  202. ]);
  203. }
  204. }