Debug.php
109 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Doctrine\Common\Util; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use ArrayIterator; |
| 5 | use ArrayObject; |
| 6 | use DateTimeInterface; |
| 7 | use MailPoetVendor\Doctrine\Common\Collections\Collection; |
| 8 | use MailPoetVendor\Doctrine\Persistence\Proxy; |
| 9 | use stdClass; |
| 10 | use function array_keys; |
| 11 | use function count; |
| 12 | use function end; |
| 13 | use function explode; |
| 14 | use function extension_loaded; |
| 15 | use function get_class; |
| 16 | use function html_entity_decode; |
| 17 | use function ini_get; |
| 18 | use function ini_set; |
| 19 | use function is_array; |
| 20 | use function is_object; |
| 21 | use function method_exists; |
| 22 | use function ob_end_clean; |
| 23 | use function ob_get_contents; |
| 24 | use function ob_start; |
| 25 | use function spl_object_hash; |
| 26 | use function strip_tags; |
| 27 | use function var_dump; |
| 28 | final class Debug |
| 29 | { |
| 30 | private function __construct() |
| 31 | { |
| 32 | } |
| 33 | public static function dump($var, $maxDepth = 2, $stripTags = \true, $echo = \true) |
| 34 | { |
| 35 | $html = ini_get('html_errors'); |
| 36 | if ($html !== \true) { |
| 37 | ini_set('html_errors', 'on'); |
| 38 | } |
| 39 | if (extension_loaded('xdebug')) { |
| 40 | ini_set('xdebug.var_display_max_depth', $maxDepth); |
| 41 | } |
| 42 | $var = self::export($var, $maxDepth); |
| 43 | ob_start(); |
| 44 | var_dump($var); |
| 45 | $dump = ob_get_contents(); |
| 46 | ob_end_clean(); |
| 47 | $dumpText = $stripTags ? strip_tags(html_entity_decode($dump)) : $dump; |
| 48 | ini_set('html_errors', $html); |
| 49 | if ($echo) { |
| 50 | echo $dumpText; |
| 51 | } |
| 52 | return $dumpText; |
| 53 | } |
| 54 | public static function export($var, $maxDepth) |
| 55 | { |
| 56 | $return = null; |
| 57 | $isObj = is_object($var); |
| 58 | if ($var instanceof Collection) { |
| 59 | $var = $var->toArray(); |
| 60 | } |
| 61 | if (!$maxDepth) { |
| 62 | return is_object($var) ? get_class($var) : (is_array($var) ? 'Array(' . count($var) . ')' : $var); |
| 63 | } |
| 64 | if (is_array($var)) { |
| 65 | $return = []; |
| 66 | foreach ($var as $k => $v) { |
| 67 | $return[$k] = self::export($v, $maxDepth - 1); |
| 68 | } |
| 69 | return $return; |
| 70 | } |
| 71 | if (!$isObj) { |
| 72 | return $var; |
| 73 | } |
| 74 | $return = new stdClass(); |
| 75 | if ($var instanceof DateTimeInterface) { |
| 76 | $return->__CLASS__ = get_class($var); |
| 77 | $return->date = $var->format('c'); |
| 78 | $return->timezone = $var->getTimezone()->getName(); |
| 79 | return $return; |
| 80 | } |
| 81 | $return->__CLASS__ = ClassUtils::getClass($var); |
| 82 | if ($var instanceof Proxy) { |
| 83 | $return->__IS_PROXY__ = \true; |
| 84 | $return->__PROXY_INITIALIZED__ = $var->__isInitialized(); |
| 85 | } |
| 86 | if ($var instanceof ArrayObject || $var instanceof ArrayIterator) { |
| 87 | $return->__STORAGE__ = self::export($var->getArrayCopy(), $maxDepth - 1); |
| 88 | } |
| 89 | return self::fillReturnWithClassAttributes($var, $return, $maxDepth); |
| 90 | } |
| 91 | private static function fillReturnWithClassAttributes($var, stdClass $return, $maxDepth) |
| 92 | { |
| 93 | $clone = (array) $var; |
| 94 | foreach (array_keys($clone) as $key) { |
| 95 | $aux = explode("\x00", $key); |
| 96 | $name = end($aux); |
| 97 | if ($aux[0] === '') { |
| 98 | $name .= ':' . ($aux[1] === '*' ? 'protected' : $aux[1] . ':private'); |
| 99 | } |
| 100 | $return->{$name} = self::export($clone[$key], $maxDepth - 1); |
| 101 | } |
| 102 | return $return; |
| 103 | } |
| 104 | public static function toString($obj) |
| 105 | { |
| 106 | return method_exists($obj, '__toString') ? (string) $obj : get_class($obj) . '@' . spl_object_hash($obj); |
| 107 | } |
| 108 | } |
| 109 |