PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 5ms RepoModel.GetById 35ms app.codeStats 0ms

/protected/extensions/doctrine/vendors/Doctrine/Common/Util/Debug.php

https://bitbucket.org/NordLabs/yiidoctrine
PHP | 123 lines | 65 code | 17 blank | 41 comment | 12 complexity | 84b26ef556f0175fb1f2a8037d54c5db MD5 | raw file
  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
 20namespace Doctrine\Common\Util;
 21
 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 * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
 29 * @author  Jonathan Wage <jonwage@gmail.com>
 30 * @author  Roman Borschel <roman@code-factory.org>
 31 * @author  Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
 32 */
 33final class Debug
 34{
 35    /**
 36     * Private constructor (prevents from instantiation)
 37     *
 38     */
 39    private function __construct() {}
 40
 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     * @param boolean $stripTags Flag that indicate if output should strip HTML tags
 49     */
 50    public static function dump($var, $maxDepth = 2, $stripTags = true)
 51    {
 52        ini_set('html_errors', 'On');
 53
 54        if (extension_loaded('xdebug')) {
 55            ini_set('xdebug.var_display_max_depth', $maxDepth);
 56        }
 57
 58        $var = self::export($var, $maxDepth++);
 59
 60        ob_start();
 61        var_dump($var);
 62        $dump = ob_get_contents();
 63        ob_end_clean();
 64
 65        echo ($stripTags ? strip_tags(html_entity_decode($dump)) : $dump);
 66
 67        ini_set('html_errors', 'Off');
 68    }
 69
 70    public static function export($var, $maxDepth)
 71    {
 72        $return = null;
 73        $isObj = is_object($var);
 74
 75        if ($isObj && in_array('Doctrine\Common\Collections\Collection', class_implements($var))) {
 76            $var = $var->toArray();
 77        }
 78
 79        if ($maxDepth) {
 80            if (is_array($var)) {
 81                $return = array();
 82
 83                foreach ($var as $k => $v) {
 84                    $return[$k] = self::export($v, $maxDepth - 1);
 85                }
 86            } else if ($isObj) {
 87                $return = new \stdclass();
 88                if ($var instanceof \DateTime) {
 89                    $return->__CLASS__ = "DateTime";
 90                    $return->date = $var->format('c');
 91                    $return->timezone = $var->getTimeZone()->getName();
 92                } else {
 93                    $reflClass = ClassUtils::newReflectionObject($var);
 94                    $return->__CLASS__ = ClassUtils::getClass($var);
 95
 96                    if ($var instanceof \Doctrine\Common\Persistence\Proxy) {
 97                        $return->__IS_PROXY__ = true;
 98                        $return->__PROXY_INITIALIZED__ = $var->__isInitialized();
 99                    }
100
101                    foreach ($reflClass->getProperties() as $reflProperty) {
102                        $name  = $reflProperty->getName();
103
104                        $reflProperty->setAccessible(true);
105                        $return->$name = self::export($reflProperty->getValue($var), $maxDepth - 1);
106                    }
107                }
108            } else {
109                $return = $var;
110            }
111        } else {
112            $return = is_object($var) ? get_class($var)
113                : (is_array($var) ? 'Array(' . count($var) . ')' : $var);
114        }
115
116        return $return;
117    }
118
119    public static function toString($obj)
120    {
121        return method_exists('__toString', $obj) ? (string) $obj : get_class($obj) . '@' . spl_object_hash($obj);
122    }
123}