You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
6.1 KiB

3 years ago
  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. public static $defaultOutput = 'php://output';
  25. protected $line = '';
  26. protected $lineDumper;
  27. protected $outputStream;
  28. protected $decimalPoint; // This is locale dependent
  29. protected $indentPad = ' ';
  30. protected $flags;
  31. private $charset = '';
  32. /**
  33. * @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path, defaults to static::$defaultOutput
  34. * @param string|null $charset The default character encoding to use for non-UTF8 strings
  35. * @param int $flags A bit field of static::DUMP_* constants to fine tune dumps representation
  36. */
  37. public function __construct($output = null, string $charset = null, int $flags = 0)
  38. {
  39. $this->flags = $flags;
  40. $this->setCharset($charset ?: ini_get('php.output_encoding') ?: ini_get('default_charset') ?: 'UTF-8');
  41. $this->decimalPoint = localeconv();
  42. $this->decimalPoint = $this->decimalPoint['decimal_point'];
  43. $this->setOutput($output ?: static::$defaultOutput);
  44. if (!$output && \is_string(static::$defaultOutput)) {
  45. static::$defaultOutput = $this->outputStream;
  46. }
  47. }
  48. /**
  49. * Sets the output destination of the dumps.
  50. *
  51. * @param callable|resource|string $output A line dumper callable, an opened stream or an output path
  52. *
  53. * @return callable|resource|string The previous output destination
  54. */
  55. public function setOutput($output)
  56. {
  57. $prev = null !== $this->outputStream ? $this->outputStream : $this->lineDumper;
  58. if (\is_callable($output)) {
  59. $this->outputStream = null;
  60. $this->lineDumper = $output;
  61. } else {
  62. if (\is_string($output)) {
  63. $output = fopen($output, 'w');
  64. }
  65. $this->outputStream = $output;
  66. $this->lineDumper = [$this, 'echoLine'];
  67. }
  68. return $prev;
  69. }
  70. /**
  71. * Sets the default character encoding to use for non-UTF8 strings.
  72. *
  73. * @return string The previous charset
  74. */
  75. public function setCharset(string $charset)
  76. {
  77. $prev = $this->charset;
  78. $charset = strtoupper($charset);
  79. $charset = null === $charset || 'UTF-8' === $charset || 'UTF8' === $charset ? 'CP1252' : $charset;
  80. $this->charset = $charset;
  81. return $prev;
  82. }
  83. /**
  84. * Sets the indentation pad string.
  85. *
  86. * @param string $pad A string that will be prepended to dumped lines, repeated by nesting level
  87. *
  88. * @return string The previous indent pad
  89. */
  90. public function setIndentPad(string $pad)
  91. {
  92. $prev = $this->indentPad;
  93. $this->indentPad = $pad;
  94. return $prev;
  95. }
  96. /**
  97. * Dumps a Data object.
  98. *
  99. * @param callable|resource|string|true|null $output A line dumper callable, an opened stream, an output path or true to return the dump
  100. *
  101. * @return string|null The dump as string when $output is true
  102. */
  103. public function dump(Data $data, $output = null)
  104. {
  105. $this->decimalPoint = localeconv();
  106. $this->decimalPoint = $this->decimalPoint['decimal_point'];
  107. if ($locale = $this->flags & (self::DUMP_COMMA_SEPARATOR | self::DUMP_TRAILING_COMMA) ? setlocale(\LC_NUMERIC, 0) : null) {
  108. setlocale(\LC_NUMERIC, 'C');
  109. }
  110. if ($returnDump = true === $output) {
  111. $output = fopen('php://memory', 'r+');
  112. }
  113. if ($output) {
  114. $prevOutput = $this->setOutput($output);
  115. }
  116. try {
  117. $data->dump($this);
  118. $this->dumpLine(-1);
  119. if ($returnDump) {
  120. $result = stream_get_contents($output, -1, 0);
  121. fclose($output);
  122. return $result;
  123. }
  124. } finally {
  125. if ($output) {
  126. $this->setOutput($prevOutput);
  127. }
  128. if ($locale) {
  129. setlocale(\LC_NUMERIC, $locale);
  130. }
  131. }
  132. return null;
  133. }
  134. /**
  135. * Dumps the current line.
  136. *
  137. * @param int $depth The recursive depth in the dumped structure for the line being dumped,
  138. * or -1 to signal the end-of-dump to the line dumper callable
  139. */
  140. protected function dumpLine(int $depth)
  141. {
  142. ($this->lineDumper)($this->line, $depth, $this->indentPad);
  143. $this->line = '';
  144. }
  145. /**
  146. * Generic line dumper callback.
  147. */
  148. protected function echoLine(string $line, int $depth, string $indentPad)
  149. {
  150. if (-1 !== $depth) {
  151. fwrite($this->outputStream, str_repeat($indentPad, $depth).$line."\n");
  152. }
  153. }
  154. /**
  155. * Converts a non-UTF-8 string to UTF-8.
  156. *
  157. * @return string|null The string converted to UTF-8
  158. */
  159. protected function utf8Encode(?string $s)
  160. {
  161. if (null === $s || preg_match('//u', $s)) {
  162. return $s;
  163. }
  164. if (!\function_exists('iconv')) {
  165. 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.');
  166. }
  167. if (false !== $c = @iconv($this->charset, 'UTF-8', $s)) {
  168. return $c;
  169. }
  170. if ('CP1252' !== $this->charset && false !== $c = @iconv('CP1252', 'UTF-8', $s)) {
  171. return $c;
  172. }
  173. return iconv('CP850', 'UTF-8', $s);
  174. }
  175. }