AbstractDumper.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. private $charsetConverter;
  28. /**
  29. * @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path, defaults to static::$defaultOutput.
  30. * @param string $charset The default character encoding to use for non-UTF8 strings.
  31. */
  32. public function __construct($output = null, $charset = null)
  33. {
  34. $this->setCharset($charset ?: ini_get('php.output_encoding') ?: ini_get('default_charset') ?: 'UTF-8');
  35. $this->decimalPoint = (string) 0.5;
  36. $this->decimalPoint = $this->decimalPoint[1];
  37. $this->setOutput($output ?: static::$defaultOutput);
  38. if (!$output && is_string(static::$defaultOutput)) {
  39. static::$defaultOutput = $this->outputStream;
  40. }
  41. }
  42. /**
  43. * Sets the output destination of the dumps.
  44. *
  45. * @param callable|resource|string $output A line dumper callable, an opened stream or an output path.
  46. *
  47. * @return callable|resource|string The previous output destination.
  48. */
  49. public function setOutput($output)
  50. {
  51. $prev = null !== $this->outputStream ? $this->outputStream : $this->lineDumper;
  52. if (is_callable($output)) {
  53. $this->outputStream = null;
  54. $this->lineDumper = $output;
  55. } else {
  56. if (is_string($output)) {
  57. $output = fopen($output, 'wb');
  58. }
  59. $this->outputStream = $output;
  60. $this->lineDumper = array($this, 'echoLine');
  61. }
  62. return $prev;
  63. }
  64. /**
  65. * Sets the default character encoding to use for non-UTF8 strings.
  66. *
  67. * @param string $charset The default character encoding to use for non-UTF8 strings.
  68. *
  69. * @return string The previous charset.
  70. */
  71. public function setCharset($charset)
  72. {
  73. $prev = $this->charset;
  74. $this->charsetConverter = 'fallback';
  75. $charset = strtoupper($charset);
  76. $charset = null === $charset || 'UTF-8' === $charset || 'UTF8' === $charset ? 'CP1252' : $charset;
  77. $supported = true;
  78. set_error_handler(function () use (&$supported) {$supported = false;});
  79. if (function_exists('mb_encoding_aliases') && mb_encoding_aliases($charset)) {
  80. $this->charset = $charset;
  81. $this->charsetConverter = 'mbstring';
  82. } elseif (function_exists('iconv')) {
  83. $supported = true;
  84. iconv($charset, 'UTF-8', '');
  85. if ($supported) {
  86. $this->charset = $charset;
  87. $this->charsetConverter = 'iconv';
  88. }
  89. }
  90. if ('fallback' === $this->charsetConverter) {
  91. $this->charset = 'ISO-8859-1';
  92. }
  93. restore_error_handler();
  94. return $prev;
  95. }
  96. /**
  97. * Sets the indentation pad string.
  98. *
  99. * @param string $pad A string the will be prepended to dumped lines, repeated by nesting level.
  100. *
  101. * @return string The indent pad.
  102. */
  103. public function setIndentPad($pad)
  104. {
  105. $prev = $this->indentPad;
  106. $this->indentPad = $pad;
  107. return $prev;
  108. }
  109. /**
  110. * Dumps a Data object.
  111. *
  112. * @param Data $data A Data object.
  113. * @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path.
  114. */
  115. public function dump(Data $data, $output = null)
  116. {
  117. $exception = null;
  118. if ($output) {
  119. $prevOutput = $this->setOutput($output);
  120. }
  121. try {
  122. $data->dump($this);
  123. $this->dumpLine(-1);
  124. } catch (\Exception $exception) {
  125. // Re-thrown below
  126. }
  127. if ($output) {
  128. $this->setOutput($prevOutput);
  129. }
  130. if (null !== $exception) {
  131. throw $exception;
  132. }
  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. */
  139. protected function dumpLine($depth)
  140. {
  141. call_user_func($this->lineDumper, $this->line, $depth, $this->indentPad);
  142. $this->line = '';
  143. }
  144. /**
  145. * Generic line dumper callback.
  146. *
  147. * @param string $line The line to write.
  148. * @param int $depth The recursive depth in the dumped structure.
  149. */
  150. protected function echoLine($line, $depth, $indentPad)
  151. {
  152. if (-1 !== $depth) {
  153. fwrite($this->outputStream, str_repeat($indentPad, $depth).$line."\n");
  154. }
  155. }
  156. /**
  157. * Converts a non-UTF-8 string to UTF-8.
  158. *
  159. * @param string $s The non-UTF-8 string to convert.
  160. *
  161. * @return string The string converted to UTF-8.
  162. */
  163. protected function utf8Encode($s)
  164. {
  165. if ('mbstring' === $this->charsetConverter) {
  166. return mb_convert_encoding($s, 'UTF-8', mb_check_encoding($s, $this->charset) ? $this->charset : '8bit');
  167. }
  168. if ('iconv' === $this->charsetConverter) {
  169. $valid = true;
  170. set_error_handler(function () use (&$valid) {$valid = false;});
  171. $c = iconv($this->charset, 'UTF-8', $s);
  172. restore_error_handler();
  173. if ($valid) {
  174. return $c;
  175. }
  176. }
  177. $s .= $s;
  178. $len = strlen($s);
  179. for ($i = $len >> 1, $j = 0; $i < $len; ++$i, ++$j) {
  180. switch (true) {
  181. case $s[$i] < "\x80":
  182. $s[$j] = $s[$i];
  183. break;
  184. case $s[$i] < "\xC0":
  185. $s[$j] = "\xC2";
  186. $s[++$j] = $s[$i];
  187. break;
  188. default:
  189. $s[$j] = "\xC3";
  190. $s[++$j] = chr(ord($s[$i]) - 64);
  191. break;
  192. }
  193. }
  194. return substr($s, 0, $j);
  195. }
  196. }