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 MIT license. For more information, see 17 * <http://www.doctrine-project.org>. 18 */ 19 20namespace Doctrine\Common\Util; 21 22use Doctrine\Common\Persistence\Proxy; 23 24/** 25 * Static class containing most used debug methods. 26 * 27 * @link www.doctrine-project.org 28 * @since 2.0 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 */ 34final class Debug 35{ 36 /** 37 * Private constructor (prevents instantiation). 38 */ 39 private function __construct() 40 { 41 } 42 43 /** 44 * Prints a dump of the public, protected and private properties of $var. 45 * 46 * @link http://xdebug.org/ 47 * 48 * @param mixed $var The variable to dump. 49 * @param integer $maxDepth The maximum nesting level for object properties. 50 * @param boolean $stripTags Whether output should strip HTML tags. 51 * @param boolean $echo Send the dumped value to the output buffer 52 * 53 * @return string 54 */ 55 public static function dump($var, $maxDepth = 2, $stripTags = true, $echo = true) 56 { 57 $html = ini_get('html_errors'); 58 59 if ($html !== true) { 60 ini_set('html_errors', true); 61 } 62 63 if (extension_loaded('xdebug')) { 64 ini_set('xdebug.var_display_max_depth', $maxDepth); 65 } 66 67 $var = self::export($var, $maxDepth++); 68 69 ob_start(); 70 var_dump($var); 71 72 $dump = ob_get_contents(); 73 74 ob_end_clean(); 75 76 $dumpText = ($stripTags ? strip_tags(html_entity_decode($dump)) : $dump); 77 78 ini_set('html_errors', $html); 79 80 if ($echo) { 81 echo $dumpText; 82 } 83 84 return $dumpText; 85 } 86 87 /** 88 * @param mixed $var 89 * @param int $maxDepth 90 * 91 * @return mixed 92 */ 93 public static function export($var, $maxDepth) 94 { 95 $return = null; 96 $isObj = is_object($var); 97 98 if ($isObj && in_array('Doctrine\Common\Collections\Collection', class_implements($var))) { 99 $var = $var->toArray(); 100 } 101 102 if ($maxDepth) { 103 if (is_array($var)) { 104 $return = array(); 105 106 foreach ($var as $k => $v) { 107 $return[$k] = self::export($v, $maxDepth - 1); 108 } 109 } else if ($isObj) { 110 $return = new \stdclass(); 111 if ($var instanceof \DateTime) { 112 $return->__CLASS__ = "DateTime"; 113 $return->date = $var->format('c'); 114 $return->timezone = $var->getTimeZone()->getName(); 115 } else { 116 $reflClass = ClassUtils::newReflectionObject($var); 117 $return->__CLASS__ = ClassUtils::getClass($var); 118 119 if ($var instanceof Proxy) { 120 $return->__IS_PROXY__ = true; 121 $return->__PROXY_INITIALIZED__ = $var->__isInitialized(); 122 } 123 124 if ($var instanceof \ArrayObject || $var instanceof \ArrayIterator) { 125 $return->__STORAGE__ = self::export($var->getArrayCopy(), $maxDepth - 1); 126 } 127 128 foreach ($reflClass->getProperties() as $reflProperty) { 129 $name = $reflProperty->getName(); 130 131 $reflProperty->setAccessible(true); 132 $return->$name = self::export($reflProperty->getValue($var), $maxDepth - 1); 133 } 134 } 135 } else { 136 $return = $var; 137 } 138 } else { 139 $return = is_object($var) ? get_class($var) 140 : (is_array($var) ? 'Array(' . count($var) . ')' : $var); 141 } 142 143 return $return; 144 } 145 146 /** 147 * Returns a string representation of an object. 148 * 149 * @param object $obj 150 * 151 * @return string 152 */ 153 public static function toString($obj) 154 { 155 return method_exists($obj, '__toString') ? (string) $obj : get_class($obj) . '@' . spl_object_hash($obj); 156 } 157} 158