1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Axstrad library. |
5
|
|
|
* |
6
|
|
|
* (c) Dan Kempster <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2014-2015 Dan Kempster <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Axstrad\Common\Util; |
15
|
|
|
|
16
|
|
|
use Axstrad\Common\Util\ArrayUtil; |
17
|
|
|
use Doctrine\Common\Util\Debug as BaseDebug; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Axstrad\Common\Util\Debug |
21
|
|
|
*/ |
22
|
|
|
class Debug |
23
|
|
|
{ |
24
|
|
|
public static $options = array( |
25
|
|
|
'varDump' => array( |
26
|
|
|
'string' => array( |
27
|
|
|
'max' => 20, |
28
|
|
|
), |
29
|
|
|
), |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
*/ |
34
|
|
|
public static function dump($var, $maxDepth = 2, $stripTags = true) |
35
|
|
|
{ |
36
|
|
|
if (extension_loaded('xdebug')) { |
37
|
|
|
$originalMaxDepth = ini_get('xdebug.var_display_max_depth'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
BaseDebug::dump($var, $maxDepth, $stripTags); |
41
|
|
|
|
42
|
|
|
if (extension_loaded('xdebug')) { |
43
|
|
|
ini_set('xdebug.var_display_max_depth', $originalMaxDepth); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param mixed $var |
49
|
|
|
* @return string |
50
|
|
|
* @uses Doctrine\Common\Util\Debug::toString when $var is an object |
51
|
|
|
* @uses summariseScalar When $var is a scalar value |
52
|
|
|
* @uses summariseArray When $var is an array |
53
|
|
|
*/ |
54
|
|
|
public static function summarise($var) |
55
|
|
|
{ |
56
|
|
|
if (is_object($var)) { |
57
|
|
|
return BaseDebug::toString($var); |
58
|
|
|
} |
59
|
|
|
elseif (is_scalar($var)) { |
60
|
|
|
return self::summariseScalar($var); |
61
|
|
|
} |
62
|
|
|
elseif (is_array($var)) { |
63
|
|
|
return self::summariseArray($var); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $var === null ? 'Null' : (string) $var; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Summarises a scalar value. |
71
|
|
|
* |
72
|
|
|
* @param boolean|integer|float|string $var |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
private static function summariseScalar($var) |
76
|
|
|
{ |
77
|
|
|
if (is_numeric($var)) { |
78
|
|
|
return (string) $var; |
79
|
|
|
} |
80
|
|
|
elseif (is_bool($var)) { |
81
|
|
|
return $var ? 'True' : 'False'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return '"'.StrUtil::ellipse($var, self::getOption('varDump.string.max')).'"'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Summarises an array |
89
|
|
|
* |
90
|
|
|
* @param array $var |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
private static function summariseArray(array $var, $depth = 1) |
94
|
|
|
{ |
95
|
|
|
static $depth = 1; |
96
|
|
|
|
97
|
|
|
if ($depth == 2) { |
98
|
|
|
return 'Array('.count($var).')'; |
99
|
|
|
} |
100
|
|
|
elseif (($count = count($var)) > 0) { |
101
|
|
|
|
102
|
|
|
$key = array_keys($var); |
103
|
|
|
$key = $key[0]; |
104
|
|
|
$value = array_values($var); |
105
|
|
|
$value = $value[0]; |
106
|
|
|
|
107
|
|
|
$mask = 'Array(%1$s){'; |
108
|
|
|
if (is_string($key)) { |
109
|
|
|
$mask .= '%2$s: '; |
110
|
|
|
} |
111
|
|
|
$mask .= '%3$s}'; |
112
|
|
|
|
113
|
|
|
$depth = 2; |
114
|
|
|
$return = sprintf( |
115
|
|
|
$mask, |
116
|
|
|
$count, |
117
|
|
|
$key, |
118
|
|
|
self::summarise($value) |
119
|
|
|
); |
120
|
|
|
$depth = 1; |
121
|
|
|
return $return; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return 'Array(0){}'; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $path |
129
|
|
|
*/ |
130
|
|
|
protected static function getOption($path) |
131
|
|
|
{ |
132
|
|
|
return ArrayUtil::get($path, self::$options); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|