菜谱项目

AbstractDumper.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. const DUMP_COMMA_SEPARATOR = 4;
  23. 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 $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, $charset = null, $flags = 0)
  38. {
  39. $this->flags = (int) $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, 'wb');
  64. }
  65. $this->outputStream = $output;
  66. $this->lineDumper = array($this, 'echoLine');
  67. }
  68. return $prev;
  69. }
  70. /**
  71. * Sets the default character encoding to use for non-UTF8 strings.
  72. *
  73. * @param string $charset The default character encoding to use for non-UTF8 strings
  74. *
  75. * @return string The previous charset
  76. */
  77. public function setCharset($charset)
  78. {
  79. $prev = $this->charset;
  80. $charset = strtoupper($charset);
  81. $charset = null === $charset || 'UTF-8' === $charset || 'UTF8' === $charset ? 'CP1252' : $charset;
  82. $this->charset = $charset;
  83. return $prev;
  84. }
  85. /**
  86. * Sets the indentation pad string.
  87. *
  88. * @param string $pad A string the will be prepended to dumped lines, repeated by nesting level
  89. *
  90. * @return string The indent pad
  91. */
  92. public function setIndentPad($pad)
  93. {
  94. $prev = $this->indentPad;
  95. $this->indentPad = $pad;
  96. return $prev;
  97. }
  98. /**
  99. * Dumps a Data object.
  100. *
  101. * @param Data $data A Data object
  102. * @param callable|resource|string|true|null $output A line dumper callable, an opened stream, an output path or true to return the dump
  103. *
  104. * @return string|null The dump as string when $output is true
  105. */
  106. public function dump(Data $data, $output = null)
  107. {
  108. $this->decimalPoint = localeconv();
  109. $this->decimalPoint = $this->decimalPoint['decimal_point'];
  110. if ($locale = $this->flags & (self::DUMP_COMMA_SEPARATOR | self::DUMP_TRAILING_COMMA) ? setlocale(LC_NUMERIC, 0) : null) {
  111. setlocale(LC_NUMERIC, 'C');
  112. }
  113. if ($returnDump = true === $output) {
  114. $output = fopen('php://memory', 'r+b');
  115. }
  116. if ($output) {
  117. $prevOutput = $this->setOutput($output);
  118. }
  119. try {
  120. $data->dump($this);
  121. $this->dumpLine(-1);
  122. if ($returnDump) {
  123. $result = stream_get_contents($output, -1, 0);
  124. fclose($output);
  125. return $result;
  126. }
  127. } finally {
  128. if ($output) {
  129. $this->setOutput($prevOutput);
  130. }
  131. if ($locale) {
  132. setlocale(LC_NUMERIC, $locale);
  133. }
  134. }
  135. }
  136. /**
  137. * Dumps the current line.
  138. *
  139. * @param int $depth The recursive depth in the dumped structure for the line being dumped,
  140. * or -1 to signal the end-of-dump to the line dumper callable
  141. */
  142. protected function dumpLine($depth)
  143. {
  144. call_user_func($this->lineDumper, $this->line, $depth, $this->indentPad);
  145. $this->line = '';
  146. }
  147. /**
  148. * Generic line dumper callback.
  149. *
  150. * @param string $line The line to write
  151. * @param int $depth The recursive depth in the dumped structure
  152. * @param string $indentPad The line indent pad
  153. */
  154. protected function echoLine($line, $depth, $indentPad)
  155. {
  156. if (-1 !== $depth) {
  157. fwrite($this->outputStream, str_repeat($indentPad, $depth).$line."\n");
  158. }
  159. }
  160. /**
  161. * Converts a non-UTF-8 string to UTF-8.
  162. *
  163. * @param string $s The non-UTF-8 string to convert
  164. *
  165. * @return string The string converted to UTF-8
  166. */
  167. protected function utf8Encode($s)
  168. {
  169. if (preg_match('//u', $s)) {
  170. return $s;
  171. }
  172. if (!function_exists('iconv')) {
  173. 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.');
  174. }
  175. if (false !== $c = @iconv($this->charset, 'UTF-8', $s)) {
  176. return $c;
  177. }
  178. if ('CP1252' !== $this->charset && false !== $c = @iconv('CP1252', 'UTF-8', $s)) {
  179. return $c;
  180. }
  181. return iconv('CP850', 'UTF-8', $s);
  182. }
  183. }