AbstractDumper.php 5.6 KB

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