annotate vendor/doctrine/common/lib/Doctrine/Common/Util/Debug.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 7a779792577d
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /*
Chris@0 3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@0 4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Chris@0 5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Chris@0 6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Chris@0 7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Chris@0 8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Chris@0 9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Chris@0 10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Chris@0 11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Chris@0 12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Chris@0 13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@0 14 *
Chris@0 15 * This software consists of voluntary contributions made by many individuals
Chris@0 16 * and is licensed under the MIT license. For more information, see
Chris@0 17 * <http://www.doctrine-project.org>.
Chris@0 18 */
Chris@0 19
Chris@0 20 namespace Doctrine\Common\Util;
Chris@0 21
Chris@0 22 use Doctrine\Common\Collections\Collection;
Chris@0 23 use Doctrine\Common\Persistence\Proxy;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Static class containing most used debug methods.
Chris@0 27 *
Chris@0 28 * @link www.doctrine-project.org
Chris@0 29 * @since 2.0
Chris@0 30 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
Chris@0 31 * @author Jonathan Wage <jonwage@gmail.com>
Chris@0 32 * @author Roman Borschel <roman@code-factory.org>
Chris@0 33 * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
Chris@0 34 */
Chris@0 35 final class Debug
Chris@0 36 {
Chris@0 37 /**
Chris@0 38 * Private constructor (prevents instantiation).
Chris@0 39 */
Chris@0 40 private function __construct()
Chris@0 41 {
Chris@0 42 }
Chris@0 43
Chris@0 44 /**
Chris@0 45 * Prints a dump of the public, protected and private properties of $var.
Chris@0 46 *
Chris@0 47 * @link http://xdebug.org/
Chris@0 48 *
Chris@0 49 * @param mixed $var The variable to dump.
Chris@0 50 * @param integer $maxDepth The maximum nesting level for object properties.
Chris@0 51 * @param boolean $stripTags Whether output should strip HTML tags.
Chris@0 52 * @param boolean $echo Send the dumped value to the output buffer
Chris@0 53 *
Chris@0 54 * @return string
Chris@0 55 */
Chris@0 56 public static function dump($var, $maxDepth = 2, $stripTags = true, $echo = true)
Chris@0 57 {
Chris@0 58 $html = ini_get('html_errors');
Chris@0 59
Chris@0 60 if ($html !== true) {
Chris@0 61 ini_set('html_errors', true);
Chris@0 62 }
Chris@0 63
Chris@0 64 if (extension_loaded('xdebug')) {
Chris@0 65 ini_set('xdebug.var_display_max_depth', $maxDepth);
Chris@0 66 }
Chris@0 67
Chris@12 68 $var = self::export($var, $maxDepth);
Chris@0 69
Chris@0 70 ob_start();
Chris@0 71 var_dump($var);
Chris@0 72
Chris@0 73 $dump = ob_get_contents();
Chris@0 74
Chris@0 75 ob_end_clean();
Chris@0 76
Chris@0 77 $dumpText = ($stripTags ? strip_tags(html_entity_decode($dump)) : $dump);
Chris@0 78
Chris@0 79 ini_set('html_errors', $html);
Chris@12 80
Chris@0 81 if ($echo) {
Chris@0 82 echo $dumpText;
Chris@0 83 }
Chris@12 84
Chris@0 85 return $dumpText;
Chris@0 86 }
Chris@0 87
Chris@0 88 /**
Chris@0 89 * @param mixed $var
Chris@0 90 * @param int $maxDepth
Chris@0 91 *
Chris@0 92 * @return mixed
Chris@0 93 */
Chris@0 94 public static function export($var, $maxDepth)
Chris@0 95 {
Chris@0 96 $return = null;
Chris@0 97 $isObj = is_object($var);
Chris@0 98
Chris@0 99 if ($var instanceof Collection) {
Chris@0 100 $var = $var->toArray();
Chris@0 101 }
Chris@0 102
Chris@12 103 if (! $maxDepth) {
Chris@12 104 return is_object($var) ? get_class($var)
Chris@0 105 : (is_array($var) ? 'Array(' . count($var) . ')' : $var);
Chris@0 106 }
Chris@0 107
Chris@12 108 if (is_array($var)) {
Chris@12 109 $return = [];
Chris@12 110
Chris@12 111 foreach ($var as $k => $v) {
Chris@12 112 $return[$k] = self::export($v, $maxDepth - 1);
Chris@12 113 }
Chris@12 114
Chris@12 115 return $return;
Chris@12 116 }
Chris@12 117
Chris@12 118 if (! $isObj) {
Chris@12 119 return $var;
Chris@12 120 }
Chris@12 121
Chris@12 122 $return = new \stdclass();
Chris@12 123 if ($var instanceof \DateTimeInterface) {
Chris@12 124 $return->__CLASS__ = get_class($var);
Chris@12 125 $return->date = $var->format('c');
Chris@12 126 $return->timezone = $var->getTimezone()->getName();
Chris@12 127
Chris@12 128 return $return;
Chris@12 129 }
Chris@12 130
Chris@12 131 $return->__CLASS__ = ClassUtils::getClass($var);
Chris@12 132
Chris@12 133 if ($var instanceof Proxy) {
Chris@12 134 $return->__IS_PROXY__ = true;
Chris@12 135 $return->__PROXY_INITIALIZED__ = $var->__isInitialized();
Chris@12 136 }
Chris@12 137
Chris@12 138 if ($var instanceof \ArrayObject || $var instanceof \ArrayIterator) {
Chris@12 139 $return->__STORAGE__ = self::export($var->getArrayCopy(), $maxDepth - 1);
Chris@12 140 }
Chris@12 141
Chris@12 142 return self::fillReturnWithClassAttributes($var, $return, $maxDepth);
Chris@12 143 }
Chris@12 144
Chris@12 145 /**
Chris@12 146 * Fill the $return variable with class attributes
Chris@12 147 *
Chris@12 148 * @param object $var
Chris@12 149 * @param stdClass $return
Chris@12 150 * @param int $maxDepth
Chris@12 151 *
Chris@12 152 * @return mixed
Chris@12 153 */
Chris@12 154 private static function fillReturnWithClassAttributes($var, \stdClass $return, $maxDepth)
Chris@12 155 {
Chris@12 156 $reflClass = ClassUtils::newReflectionObject($var);
Chris@12 157 $parsedAttributes = array();
Chris@12 158 do {
Chris@12 159 $currentClassName = $reflClass->getName();
Chris@12 160
Chris@12 161 foreach ($reflClass->getProperties() as $reflProperty) {
Chris@12 162 $attributeKey = $reflProperty->isPrivate() ? $currentClassName . '#' : '';
Chris@12 163 $attributeKey .= $reflProperty->getName();
Chris@12 164
Chris@12 165 if (isset($parsedAttributes[$attributeKey])) {
Chris@12 166 continue;
Chris@12 167 }
Chris@12 168
Chris@12 169 $parsedAttributes[$attributeKey] = true;
Chris@12 170
Chris@12 171 $name =
Chris@12 172 $reflProperty->getName()
Chris@12 173 . ($return->__CLASS__ !== $currentClassName || $reflProperty->isPrivate() ? ':' . $currentClassName : '')
Chris@12 174 . ($reflProperty->isPrivate() ? ':private' : '')
Chris@12 175 . ($reflProperty->isProtected() ? ':protected' : '')
Chris@12 176 ;
Chris@12 177
Chris@12 178 $reflProperty->setAccessible(true);
Chris@12 179 $return->$name = self::export($reflProperty->getValue($var), $maxDepth - 1);
Chris@12 180 }
Chris@12 181 } while ($reflClass = $reflClass->getParentClass());
Chris@12 182
Chris@0 183 return $return;
Chris@0 184 }
Chris@0 185
Chris@0 186 /**
Chris@0 187 * Returns a string representation of an object.
Chris@0 188 *
Chris@0 189 * @param object $obj
Chris@0 190 *
Chris@0 191 * @return string
Chris@0 192 */
Chris@0 193 public static function toString($obj)
Chris@0 194 {
Chris@0 195 return method_exists($obj, '__toString') ? (string) $obj : get_class($obj) . '@' . spl_object_hash($obj);
Chris@0 196 }
Chris@0 197 }