Dumper.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace Doctrine\DBAL\Tools;
  3. use ArrayIterator;
  4. use ArrayObject;
  5. use DateTimeInterface;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\Common\Persistence\Proxy;
  8. use stdClass;
  9. use function array_keys;
  10. use function assert;
  11. use function class_exists;
  12. use function count;
  13. use function end;
  14. use function explode;
  15. use function extension_loaded;
  16. use function get_class;
  17. use function html_entity_decode;
  18. use function ini_set;
  19. use function is_array;
  20. use function is_object;
  21. use function is_string;
  22. use function ob_get_clean;
  23. use function ob_start;
  24. use function strip_tags;
  25. use function strlen;
  26. use function strrpos;
  27. use function substr;
  28. use function var_dump;
  29. /**
  30. * Static class used to dump the variable to be used on output.
  31. * Simplified port of Util\Debug from doctrine/common.
  32. *
  33. * @internal
  34. */
  35. final class Dumper
  36. {
  37. /**
  38. * Private constructor (prevents instantiation).
  39. *
  40. * @codeCoverageIgnore
  41. */
  42. private function __construct()
  43. {
  44. }
  45. /**
  46. * Returns a dump of the public, protected and private properties of $var.
  47. *
  48. * @link https://xdebug.org/
  49. *
  50. * @param mixed $var The variable to dump.
  51. * @param int $maxDepth The maximum nesting level for object properties.
  52. */
  53. public static function dump($var, int $maxDepth = 2): string
  54. {
  55. $html = ini_set('html_errors', '1');
  56. assert(is_string($html));
  57. if (extension_loaded('xdebug')) {
  58. ini_set('xdebug.var_display_max_depth', (string) $maxDepth);
  59. }
  60. $var = self::export($var, $maxDepth);
  61. ob_start();
  62. var_dump($var);
  63. try {
  64. $output = ob_get_clean();
  65. assert(is_string($output));
  66. return strip_tags(html_entity_decode($output));
  67. } finally {
  68. ini_set('html_errors', $html);
  69. }
  70. }
  71. /**
  72. * @param mixed $var
  73. *
  74. * @return mixed
  75. */
  76. public static function export($var, int $maxDepth)
  77. {
  78. $return = null;
  79. $isObj = is_object($var);
  80. if ($var instanceof Collection) {
  81. $var = $var->toArray();
  82. }
  83. if ($maxDepth === 0) {
  84. return is_object($var) ? get_class($var)
  85. : (is_array($var) ? 'Array(' . count($var) . ')' : $var);
  86. }
  87. if (is_array($var)) {
  88. $return = [];
  89. foreach ($var as $k => $v) {
  90. $return[$k] = self::export($v, $maxDepth - 1);
  91. }
  92. return $return;
  93. }
  94. if (! $isObj) {
  95. return $var;
  96. }
  97. $return = new stdClass();
  98. if ($var instanceof DateTimeInterface) {
  99. $return->__CLASS__ = get_class($var);
  100. $return->date = $var->format('c');
  101. $return->timezone = $var->getTimezone()->getName();
  102. return $return;
  103. }
  104. $return->__CLASS__ = self::getClass($var);
  105. if ($var instanceof Proxy) {
  106. $return->__IS_PROXY__ = true;
  107. $return->__PROXY_INITIALIZED__ = $var->__isInitialized();
  108. }
  109. if ($var instanceof ArrayObject || $var instanceof ArrayIterator) {
  110. $return->__STORAGE__ = self::export($var->getArrayCopy(), $maxDepth - 1);
  111. }
  112. return self::fillReturnWithClassAttributes($var, $return, $maxDepth);
  113. }
  114. /**
  115. * Fill the $return variable with class attributes
  116. * Based on obj2array function from {@see https://secure.php.net/manual/en/function.get-object-vars.php#47075}
  117. *
  118. * @param object $var
  119. *
  120. * @return mixed
  121. */
  122. private static function fillReturnWithClassAttributes($var, stdClass $return, int $maxDepth)
  123. {
  124. $clone = (array) $var;
  125. foreach (array_keys($clone) as $key) {
  126. $aux = explode("\0", $key);
  127. $name = end($aux);
  128. if ($aux[0] === '') {
  129. $name .= ':' . ($aux[1] === '*' ? 'protected' : $aux[1] . ':private');
  130. }
  131. $return->$name = self::export($clone[$key], $maxDepth - 1);
  132. }
  133. return $return;
  134. }
  135. /**
  136. * @param object $object
  137. */
  138. private static function getClass($object): string
  139. {
  140. $class = get_class($object);
  141. if (! class_exists(Proxy::class)) {
  142. return $class;
  143. }
  144. $pos = strrpos($class, '\\' . Proxy::MARKER . '\\');
  145. if ($pos === false) {
  146. return $class;
  147. }
  148. return substr($class, $pos + strlen(Proxy::MARKER) + 2);
  149. }
  150. }