Debug.php 4.6 KB

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