Security update to Drupal 8.4.6
[yaffs-website] / vendor / doctrine / common / lib / Doctrine / Common / Util / Debug.php
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             return is_object($var) ? get_class($var)
105                 : (is_array($var) ? 'Array(' . count($var) . ')' : $var);
106         }
107
108         if (is_array($var)) {
109             $return = [];
110
111             foreach ($var as $k => $v) {
112                 $return[$k] = self::export($v, $maxDepth - 1);
113             }
114
115             return $return;
116         }
117
118         if (! $isObj) {
119             return $var;
120         }
121
122         $return = new \stdclass();
123         if ($var instanceof \DateTimeInterface) {
124             $return->__CLASS__ = get_class($var);
125             $return->date = $var->format('c');
126             $return->timezone = $var->getTimezone()->getName();
127
128             return $return;
129         }
130
131         $return->__CLASS__ = ClassUtils::getClass($var);
132
133         if ($var instanceof Proxy) {
134             $return->__IS_PROXY__ = true;
135             $return->__PROXY_INITIALIZED__ = $var->__isInitialized();
136         }
137
138         if ($var instanceof \ArrayObject || $var instanceof \ArrayIterator) {
139             $return->__STORAGE__ = self::export($var->getArrayCopy(), $maxDepth - 1);
140         }
141
142         return self::fillReturnWithClassAttributes($var, $return, $maxDepth);
143     }
144
145     /**
146      * Fill the $return variable with class attributes
147      * Based on obj2array function from {@see http://php.net/manual/en/function.get-object-vars.php#47075}
148      *
149      * @param object   $var
150      * @param \stdClass $return
151      * @param int      $maxDepth
152      *
153      * @return mixed
154      */
155     private static function fillReturnWithClassAttributes($var, \stdClass $return, $maxDepth)
156     {
157         $clone = (array) $var;
158
159         foreach (array_keys($clone) as $key) {
160             $aux = explode("\0", $key);
161             $name = end($aux);
162             if ($aux[0] === '') {
163                 $name.= ':' . ($aux[1] === '*' ? 'protected' : $aux[1].':private');
164             }
165             $return->$name = self::export($clone[$key], $maxDepth - 1);;
166         }
167
168         return $return;
169     }
170
171     /**
172      * Returns a string representation of an object.
173      *
174      * @param object $obj
175      *
176      * @return string
177      */
178     public static function toString($obj)
179     {
180         return method_exists($obj, '__toString') ? (string) $obj : get_class($obj) . '@' . spl_object_hash($obj);
181     }
182 }