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.
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>.
20 namespace Doctrine\Common\Util;
22 use Doctrine\Common\Collections\Collection;
23 use Doctrine\Common\Persistence\Proxy;
26 * Static class containing most used debug methods.
28 * @link www.doctrine-project.org
30 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
31 * @author Jonathan Wage <jonwage@gmail.com>
32 * @author Roman Borschel <roman@code-factory.org>
33 * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
38 * Private constructor (prevents instantiation).
40 private function __construct()
45 * Prints a dump of the public, protected and private properties of $var.
47 * @link http://xdebug.org/
49 * @param mixed $var The variable to dump.
50 * @param integer $maxDepth The maximum nesting level for object properties.
51 * @param boolean $stripTags Whether output should strip HTML tags.
52 * @param boolean $echo Send the dumped value to the output buffer
56 public static function dump($var, $maxDepth = 2, $stripTags = true, $echo = true)
58 $html = ini_get('html_errors');
61 ini_set('html_errors', true);
64 if (extension_loaded('xdebug')) {
65 ini_set('xdebug.var_display_max_depth', $maxDepth);
68 $var = self::export($var, $maxDepth);
73 $dump = ob_get_contents();
77 $dumpText = ($stripTags ? strip_tags(html_entity_decode($dump)) : $dump);
79 ini_set('html_errors', $html);
90 * @param int $maxDepth
94 public static function export($var, $maxDepth)
97 $isObj = is_object($var);
99 if ($var instanceof Collection) {
100 $var = $var->toArray();
104 return is_object($var) ? get_class($var)
105 : (is_array($var) ? 'Array(' . count($var) . ')' : $var);
108 if (is_array($var)) {
111 foreach ($var as $k => $v) {
112 $return[$k] = self::export($v, $maxDepth - 1);
122 $return = new \stdclass();
123 if ($var instanceof \DateTimeInterface) {
124 $return->__CLASS__ = get_class($var);
125 $return->date = $var->format('c');
126 $return->timezone = $var->getTimezone()->getName();
131 $return->__CLASS__ = ClassUtils::getClass($var);
133 if ($var instanceof Proxy) {
134 $return->__IS_PROXY__ = true;
135 $return->__PROXY_INITIALIZED__ = $var->__isInitialized();
138 if ($var instanceof \ArrayObject || $var instanceof \ArrayIterator) {
139 $return->__STORAGE__ = self::export($var->getArrayCopy(), $maxDepth - 1);
142 return self::fillReturnWithClassAttributes($var, $return, $maxDepth);
146 * Fill the $return variable with class attributes
147 * Based on obj2array function from {@see http://php.net/manual/en/function.get-object-vars.php#47075}
150 * @param \stdClass $return
151 * @param int $maxDepth
155 private static function fillReturnWithClassAttributes($var, \stdClass $return, $maxDepth)
157 $clone = (array) $var;
159 foreach (array_keys($clone) as $key) {
160 $aux = explode("\0", $key);
162 if ($aux[0] === '') {
163 $name.= ':' . ($aux[1] === '*' ? 'protected' : $aux[1].':private');
165 $return->$name = self::export($clone[$key], $maxDepth - 1);;
172 * Returns a string representation of an object.
178 public static function toString($obj)
180 return method_exists($obj, '__toString') ? (string) $obj : get_class($obj) . '@' . spl_object_hash($obj);