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@0
|
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@0
|
80
|
Chris@0
|
81 if ($echo) {
|
Chris@0
|
82 echo $dumpText;
|
Chris@0
|
83 }
|
Chris@0
|
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@0
|
103 if ($maxDepth) {
|
Chris@0
|
104 if (is_array($var)) {
|
Chris@0
|
105 $return = [];
|
Chris@0
|
106
|
Chris@0
|
107 foreach ($var as $k => $v) {
|
Chris@0
|
108 $return[$k] = self::export($v, $maxDepth - 1);
|
Chris@0
|
109 }
|
Chris@0
|
110 } else if ($isObj) {
|
Chris@0
|
111 $return = new \stdclass();
|
Chris@0
|
112 if ($var instanceof \DateTime) {
|
Chris@0
|
113 $return->__CLASS__ = "DateTime";
|
Chris@0
|
114 $return->date = $var->format('c');
|
Chris@0
|
115 $return->timezone = $var->getTimeZone()->getName();
|
Chris@0
|
116 } else {
|
Chris@0
|
117 $reflClass = ClassUtils::newReflectionObject($var);
|
Chris@0
|
118 $return->__CLASS__ = ClassUtils::getClass($var);
|
Chris@0
|
119
|
Chris@0
|
120 if ($var instanceof Proxy) {
|
Chris@0
|
121 $return->__IS_PROXY__ = true;
|
Chris@0
|
122 $return->__PROXY_INITIALIZED__ = $var->__isInitialized();
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 if ($var instanceof \ArrayObject || $var instanceof \ArrayIterator) {
|
Chris@0
|
126 $return->__STORAGE__ = self::export($var->getArrayCopy(), $maxDepth - 1);
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 foreach ($reflClass->getProperties() as $reflProperty) {
|
Chris@0
|
130 $name = $reflProperty->getName();
|
Chris@0
|
131
|
Chris@0
|
132 $reflProperty->setAccessible(true);
|
Chris@0
|
133 $return->$name = self::export($reflProperty->getValue($var), $maxDepth - 1);
|
Chris@0
|
134 }
|
Chris@0
|
135 }
|
Chris@0
|
136 } else {
|
Chris@0
|
137 $return = $var;
|
Chris@0
|
138 }
|
Chris@0
|
139 } else {
|
Chris@0
|
140 $return = is_object($var) ? get_class($var)
|
Chris@0
|
141 : (is_array($var) ? 'Array(' . count($var) . ')' : $var);
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 return $return;
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 /**
|
Chris@0
|
148 * Returns a string representation of an object.
|
Chris@0
|
149 *
|
Chris@0
|
150 * @param object $obj
|
Chris@0
|
151 *
|
Chris@0
|
152 * @return string
|
Chris@0
|
153 */
|
Chris@0
|
154 public static function toString($obj)
|
Chris@0
|
155 {
|
Chris@0
|
156 return method_exists($obj, '__toString') ? (string) $obj : get_class($obj) . '@' . spl_object_hash($obj);
|
Chris@0
|
157 }
|
Chris@0
|
158 }
|