Debug.php 4.4 KB

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