Dumper.php 4.3 KB

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