diff options
Diffstat (limited to 'vendor/doctrine/orm/src/Tools/Debug.php')
-rw-r--r-- | vendor/doctrine/orm/src/Tools/Debug.php | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Tools/Debug.php b/vendor/doctrine/orm/src/Tools/Debug.php new file mode 100644 index 0000000..8521e53 --- /dev/null +++ b/vendor/doctrine/orm/src/Tools/Debug.php | |||
@@ -0,0 +1,158 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\ORM\Tools; | ||
6 | |||
7 | use ArrayIterator; | ||
8 | use ArrayObject; | ||
9 | use DateTimeInterface; | ||
10 | use Doctrine\Common\Collections\Collection; | ||
11 | use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver; | ||
12 | use Doctrine\Persistence\Proxy; | ||
13 | use stdClass; | ||
14 | |||
15 | use function array_keys; | ||
16 | use function count; | ||
17 | use function end; | ||
18 | use function explode; | ||
19 | use function extension_loaded; | ||
20 | use function html_entity_decode; | ||
21 | use function ini_get; | ||
22 | use function ini_set; | ||
23 | use function is_array; | ||
24 | use function is_object; | ||
25 | use function ob_end_clean; | ||
26 | use function ob_get_contents; | ||
27 | use function ob_start; | ||
28 | use function strip_tags; | ||
29 | use function var_dump; | ||
30 | |||
31 | /** | ||
32 | * Static class containing most used debug methods. | ||
33 | * | ||
34 | * @internal | ||
35 | * | ||
36 | * @link www.doctrine-project.org | ||
37 | */ | ||
38 | final class Debug | ||
39 | { | ||
40 | /** | ||
41 | * Private constructor (prevents instantiation). | ||
42 | */ | ||
43 | private function __construct() | ||
44 | { | ||
45 | } | ||
46 | |||
47 | /** | ||
48 | * Prints a dump of the public, protected and private properties of $var. | ||
49 | * | ||
50 | * @link https://xdebug.org/ | ||
51 | * | ||
52 | * @param mixed $var The variable to dump. | ||
53 | * @param int $maxDepth The maximum nesting level for object properties. | ||
54 | */ | ||
55 | public static function dump(mixed $var, int $maxDepth = 2): string | ||
56 | { | ||
57 | $html = ini_get('html_errors'); | ||
58 | |||
59 | if ($html !== '1') { | ||
60 | ini_set('html_errors', 'on'); | ||
61 | } | ||
62 | |||
63 | if (extension_loaded('xdebug')) { | ||
64 | $previousDepth = ini_get('xdebug.var_display_max_depth'); | ||
65 | ini_set('xdebug.var_display_max_depth', (string) $maxDepth); | ||
66 | } | ||
67 | |||
68 | try { | ||
69 | $var = self::export($var, $maxDepth); | ||
70 | |||
71 | ob_start(); | ||
72 | var_dump($var); | ||
73 | |||
74 | $dump = ob_get_contents(); | ||
75 | |||
76 | ob_end_clean(); | ||
77 | |||
78 | $dumpText = strip_tags(html_entity_decode($dump)); | ||
79 | } finally { | ||
80 | ini_set('html_errors', $html); | ||
81 | |||
82 | if (isset($previousDepth)) { | ||
83 | ini_set('xdebug.var_display_max_depth', $previousDepth); | ||
84 | } | ||
85 | } | ||
86 | |||
87 | return $dumpText; | ||
88 | } | ||
89 | |||
90 | public static function export(mixed $var, int $maxDepth): mixed | ||
91 | { | ||
92 | if ($var instanceof Collection) { | ||
93 | $var = $var->toArray(); | ||
94 | } | ||
95 | |||
96 | if (! $maxDepth) { | ||
97 | return is_object($var) ? $var::class | ||
98 | : (is_array($var) ? 'Array(' . count($var) . ')' : $var); | ||
99 | } | ||
100 | |||
101 | if (is_array($var)) { | ||
102 | $return = []; | ||
103 | |||
104 | foreach ($var as $k => $v) { | ||
105 | $return[$k] = self::export($v, $maxDepth - 1); | ||
106 | } | ||
107 | |||
108 | return $return; | ||
109 | } | ||
110 | |||
111 | if (! is_object($var)) { | ||
112 | return $var; | ||
113 | } | ||
114 | |||
115 | $return = new stdClass(); | ||
116 | if ($var instanceof DateTimeInterface) { | ||
117 | $return->__CLASS__ = $var::class; | ||
118 | $return->date = $var->format('c'); | ||
119 | $return->timezone = $var->getTimezone()->getName(); | ||
120 | |||
121 | return $return; | ||
122 | } | ||
123 | |||
124 | $return->__CLASS__ = DefaultProxyClassNameResolver::getClass($var); | ||
125 | |||
126 | if ($var instanceof Proxy) { | ||
127 | $return->__IS_PROXY__ = true; | ||
128 | $return->__PROXY_INITIALIZED__ = $var->__isInitialized(); | ||
129 | } | ||
130 | |||
131 | if ($var instanceof ArrayObject || $var instanceof ArrayIterator) { | ||
132 | $return->__STORAGE__ = self::export($var->getArrayCopy(), $maxDepth - 1); | ||
133 | } | ||
134 | |||
135 | return self::fillReturnWithClassAttributes($var, $return, $maxDepth); | ||
136 | } | ||
137 | |||
138 | /** | ||
139 | * Fill the $return variable with class attributes | ||
140 | * Based on obj2array function from {@see https://secure.php.net/manual/en/function.get-object-vars.php#47075} | ||
141 | */ | ||
142 | private static function fillReturnWithClassAttributes(object $var, stdClass $return, int $maxDepth): stdClass | ||
143 | { | ||
144 | $clone = (array) $var; | ||
145 | |||
146 | foreach (array_keys($clone) as $key) { | ||
147 | $aux = explode("\0", (string) $key); | ||
148 | $name = end($aux); | ||
149 | if ($aux[0] === '') { | ||
150 | $name .= ':' . ($aux[1] === '*' ? 'protected' : $aux[1] . ':private'); | ||
151 | } | ||
152 | |||
153 | $return->$name = self::export($clone[$key], $maxDepth - 1); | ||
154 | } | ||
155 | |||
156 | return $return; | ||
157 | } | ||
158 | } | ||