comparison vendor/doctrine/common/lib/Doctrine/Common/Util/Debug.php @ 0:4c8ae668cc8c

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