Debug.php 4.3 KB

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