Debug.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\Common\Util;
  22. /**
  23. * Static class containing most used debug methods.
  24. *
  25. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  26. * @link www.doctrine-project.org
  27. * @since 2.0
  28. * @version $Revision: 3938 $
  29. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  30. * @author Jonathan Wage <jonwage@gmail.com>
  31. * @author Roman Borschel <roman@code-factory.org>
  32. * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
  33. */
  34. final class Debug
  35. {
  36. /**
  37. * Private constructor (prevents from instantiation)
  38. *
  39. */
  40. private function __construct() {}
  41. /**
  42. * Prints a dump of the public, protected and private properties of $var.
  43. *
  44. * @static
  45. * @link http://xdebug.org/
  46. * @param mixed $var
  47. * @param integer $maxDepth Maximum nesting level for object properties
  48. */
  49. public static function dump($var, $maxDepth = 2)
  50. {
  51. ini_set('html_errors', 'On');
  52. if (extension_loaded('xdebug')) {
  53. ini_set('xdebug.var_display_max_depth', $maxDepth);
  54. }
  55. $var = self::export($var, $maxDepth++);
  56. ob_start();
  57. var_dump($var);
  58. $dump = ob_get_contents();
  59. ob_end_clean();
  60. echo strip_tags(html_entity_decode($dump));
  61. ini_set('html_errors', 'Off');
  62. }
  63. public static function export($var, $maxDepth)
  64. {
  65. $return = null;
  66. $isObj = is_object($var);
  67. if ($isObj && in_array('Doctrine\Common\Collections\Collection', class_implements($var))) {
  68. $var = $var->toArray();
  69. }
  70. if ($maxDepth) {
  71. if (is_array($var)) {
  72. $return = array();
  73. foreach ($var as $k => $v) {
  74. $return[$k] = self::export($v, $maxDepth - 1);
  75. }
  76. } else if ($isObj) {
  77. if ($var instanceof \DateTime) {
  78. $return = $var->format('c');
  79. } else {
  80. $reflClass = new \ReflectionClass(get_class($var));
  81. $return = new \stdclass();
  82. $return->{'__CLASS__'} = get_class($var);
  83. if ($var instanceof \Doctrine\ORM\Proxy\Proxy && ! $var->__isInitialized__) {
  84. $reflProperty = $reflClass->getProperty('_identifier');
  85. $reflProperty->setAccessible(true);
  86. foreach ($reflProperty->getValue($var) as $name => $value) {
  87. $return->$name = self::export($value, $maxDepth - 1);
  88. }
  89. } else {
  90. $excludeProperties = array();
  91. if ($var instanceof \Doctrine\ORM\Proxy\Proxy) {
  92. $excludeProperties = array('_entityPersister', '__isInitialized__', '_identifier');
  93. }
  94. foreach ($reflClass->getProperties() as $reflProperty) {
  95. $name = $reflProperty->getName();
  96. if ( ! in_array($name, $excludeProperties)) {
  97. $reflProperty->setAccessible(true);
  98. $return->$name = self::export($reflProperty->getValue($var), $maxDepth - 1);
  99. }
  100. }
  101. }
  102. }
  103. } else {
  104. $return = $var;
  105. }
  106. } else {
  107. $return = is_object($var) ? get_class($var)
  108. : (is_array($var) ? 'Array(' . count($var) . ')' : $var);
  109. }
  110. return $return;
  111. }
  112. public static function toString($obj)
  113. {
  114. return method_exists('__toString', $obj) ? (string) $obj : get_class($obj) . '@' . spl_object_hash($obj);
  115. }
  116. }