AbstractDumper.php 5.6 KB

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