/vendor/symfony/var-dumper/Symfony/Component/VarDumper/Dumper/AbstractDumper.php

https://gitlab.com/techniconline/kmc · PHP · 226 lines · 137 code · 26 blank · 63 comment · 20 complexity · 894419cb8ef4b1ec8b13503175c93f43 MD5 · raw file

  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) {
  79. $supported = false;
  80. });
  81. if (function_exists('mb_encoding_aliases') && mb_encoding_aliases($charset)) {
  82. $this->charset = $charset;
  83. $this->charsetConverter = 'mbstring';
  84. } elseif (function_exists('iconv')) {
  85. $supported = true;
  86. iconv($charset, 'UTF-8', '');
  87. if ($supported) {
  88. $this->charset = $charset;
  89. $this->charsetConverter = 'iconv';
  90. }
  91. }
  92. if ('fallback' === $this->charsetConverter) {
  93. $this->charset = 'ISO-8859-1';
  94. }
  95. restore_error_handler();
  96. return $prev;
  97. }
  98. /**
  99. * Sets the indentation pad string.
  100. *
  101. * @param string $pad A string the will be prepended to dumped lines, repeated by nesting level.
  102. *
  103. * @return string The indent pad.
  104. */
  105. public function setIndentPad($pad)
  106. {
  107. $prev = $this->indentPad;
  108. $this->indentPad = $pad;
  109. return $prev;
  110. }
  111. /**
  112. * Dumps a Data object.
  113. *
  114. * @param Data $data A Data object.
  115. * @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path.
  116. */
  117. public function dump(Data $data, $output = null)
  118. {
  119. $exception = null;
  120. if ($output) {
  121. $prevOutput = $this->setOutput($output);
  122. }
  123. try {
  124. $data->dump($this);
  125. $this->dumpLine(-1);
  126. } catch (\Exception $exception) {
  127. // Re-thrown below
  128. }
  129. if ($output) {
  130. $this->setOutput($prevOutput);
  131. }
  132. if (null !== $exception) {
  133. throw $exception;
  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. */
  141. protected function dumpLine($depth)
  142. {
  143. call_user_func($this->lineDumper, $this->line, $depth, $this->indentPad);
  144. $this->line = '';
  145. }
  146. /**
  147. * Generic line dumper callback.
  148. *
  149. * @param string $line The line to write.
  150. * @param int $depth The recursive depth in the dumped structure.
  151. */
  152. protected function echoLine($line, $depth, $indentPad)
  153. {
  154. if (-1 !== $depth) {
  155. fwrite($this->outputStream, str_repeat($indentPad, $depth) . $line . "\n");
  156. }
  157. }
  158. /**
  159. * Converts a non-UTF-8 string to UTF-8.
  160. *
  161. * @param string $s The non-UTF-8 string to convert.
  162. *
  163. * @return string The string converted to UTF-8.
  164. */
  165. protected function utf8Encode($s)
  166. {
  167. if ('mbstring' === $this->charsetConverter) {
  168. return mb_convert_encoding($s, 'UTF-8', mb_check_encoding($s, $this->charset) ? $this->charset : '8bit');
  169. }
  170. if ('iconv' === $this->charsetConverter) {
  171. $valid = true;
  172. set_error_handler(function () use (&$valid) {
  173. $valid = false;
  174. });
  175. $c = iconv($this->charset, 'UTF-8', $s);
  176. restore_error_handler();
  177. if ($valid) {
  178. return $c;
  179. }
  180. }
  181. $s .= $s;
  182. $len = strlen($s);
  183. for ($i = $len >> 1, $j = 0; $i < $len; ++$i, ++$j) {
  184. switch (true) {
  185. case $s[$i] < "\x80":
  186. $s[$j] = $s[$i];
  187. break;
  188. case $s[$i] < "\xC0":
  189. $s[$j] = "\xC2";
  190. $s[++$j] = $s[$i];
  191. break;
  192. default:
  193. $s[$j] = "\xC3";
  194. $s[++$j] = chr(ord($s[$i]) - 64);
  195. break;
  196. }
  197. }
  198. return substr($s, 0, $j);
  199. }
  200. }