vendor/doctrine-common/lib/Doctrine/Common/Util/Debug.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     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 
       
    22 namespace Doctrine\Common\Util;
       
    23 
       
    24 /**
       
    25  * Static class containing most used debug methods.
       
    26  *
       
    27  * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
       
    28  * @link    www.doctrine-project.org
       
    29  * @since   2.0
       
    30  * @version $Revision: 3938 $
       
    31  * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
       
    32  * @author  Jonathan Wage <jonwage@gmail.com>
       
    33  * @author  Roman Borschel <roman@code-factory.org>
       
    34  * @author  Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
       
    35  */
       
    36 final class Debug
       
    37 {
       
    38     /**
       
    39      * Private constructor (prevents from instantiation)
       
    40      *
       
    41      */
       
    42     private function __construct() {}
       
    43 
       
    44     /**
       
    45      * Prints a dump of the public, protected and private properties of $var.
       
    46      *
       
    47      * @static
       
    48      * @link http://xdebug.org/
       
    49      * @param mixed $var
       
    50      * @param integer $maxDepth Maximum nesting level for object properties
       
    51      */
       
    52     public static function dump($var, $maxDepth = 2)
       
    53     {
       
    54         ini_set('html_errors', 'On');
       
    55         
       
    56         if (extension_loaded('xdebug')) {
       
    57             ini_set('xdebug.var_display_max_depth', $maxDepth);
       
    58         }
       
    59         
       
    60         $var = self::export($var, $maxDepth++);
       
    61         
       
    62         ob_start();
       
    63         var_dump($var);
       
    64         $dump = ob_get_contents();
       
    65         ob_end_clean();
       
    66         
       
    67         echo strip_tags(html_entity_decode($dump));
       
    68         
       
    69         ini_set('html_errors', 'Off');
       
    70     }
       
    71     
       
    72     public static function export($var, $maxDepth)
       
    73     {
       
    74         $return = null;
       
    75         $isObj = is_object($var);
       
    76     
       
    77         if ($isObj && in_array('Doctrine\Common\Collections\Collection', class_implements($var))) {
       
    78             $var = $var->toArray();
       
    79         }
       
    80         
       
    81         if ($maxDepth) {
       
    82             if (is_array($var)) {
       
    83                 $return = array();
       
    84             
       
    85                 foreach ($var as $k => $v) {
       
    86                     $return[$k] = self::export($v, $maxDepth - 1);
       
    87                 }
       
    88             } else if ($isObj) {
       
    89                 if ($var instanceof \DateTime) {
       
    90                     $return = $var->format('c');
       
    91                 } else {
       
    92                     $reflClass = new \ReflectionClass(get_class($var));
       
    93                     $return = new \stdclass();
       
    94                     $return->{'__CLASS__'} = get_class($var);
       
    95 
       
    96                     if ($var instanceof \Doctrine\ORM\Proxy\Proxy && ! $var->__isInitialized__) {
       
    97                         $reflProperty = $reflClass->getProperty('_identifier');
       
    98                         $reflProperty->setAccessible(true);
       
    99 
       
   100                         foreach ($reflProperty->getValue($var) as $name => $value) {
       
   101                             $return->$name = self::export($value, $maxDepth - 1);
       
   102                         }
       
   103                     } else {
       
   104                         $excludeProperties = array();
       
   105 
       
   106                         if ($var instanceof \Doctrine\ORM\Proxy\Proxy) {
       
   107                             $excludeProperties = array('_entityPersister', '__isInitialized__', '_identifier');
       
   108                         }
       
   109 
       
   110                         foreach ($reflClass->getProperties() as $reflProperty) {
       
   111                             $name  = $reflProperty->getName();
       
   112 
       
   113                             if ( ! in_array($name, $excludeProperties)) {
       
   114                                 $reflProperty->setAccessible(true);
       
   115 
       
   116                                 $return->$name = self::export($reflProperty->getValue($var), $maxDepth - 1);
       
   117                             }
       
   118                         }
       
   119                     }
       
   120                 }
       
   121             } else {
       
   122                 $return = $var;
       
   123             }
       
   124         } else {
       
   125             $return = is_object($var) ? get_class($var) 
       
   126                 : (is_array($var) ? 'Array(' . count($var) . ')' : $var);
       
   127         }
       
   128         
       
   129         return $return;
       
   130     }
       
   131 
       
   132     public static function toString($obj)
       
   133     {
       
   134         return method_exists('__toString', $obj) ? (string) $obj : get_class($obj) . '@' . spl_object_hash($obj);
       
   135     }
       
   136 }