1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Admin Page Framework |
4
|
|
|
* |
5
|
|
|
* http://en.michaeluno.jp/admin-page-framework/ |
6
|
|
|
* Copyright (c) 2013-2016 Michael Uno; Licensed MIT |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A base class of the debug class. |
12
|
|
|
* |
13
|
|
|
* @since 3.8.9 |
14
|
|
|
* @extends AdminPageFramework_FrameworkUtility |
15
|
|
|
* @package AdminPageFramework |
16
|
|
|
* @subpackage Common/Utility |
17
|
|
|
*/ |
18
|
|
|
class AdminPageFramework_Debug_Base extends AdminPageFramework_FrameworkUtility { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Returns a legible value representation with value details. |
22
|
|
|
* @since 3.8.9 |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
static protected function _getLegibleDetails( $mValue ) { |
|
|
|
|
26
|
|
|
if ( is_array( $mValue ) ) { |
27
|
|
|
return '(array, length: ' . count( $mValue ).') ' |
28
|
|
|
. print_r( self::_getLegibleArray( $mValue ) , true ); |
29
|
|
|
} |
30
|
|
|
return print_r( self::_getLegibleValue( $mValue ), true ); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Returns a string representation of the given value. |
35
|
|
|
* @since 3.8.9 |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
static protected function _getLegible( $mValue ) { |
|
|
|
|
39
|
|
|
|
40
|
|
|
$mValue = is_object( $mValue ) |
41
|
|
|
? ( method_exists( $mValue, '__toString' ) |
42
|
|
|
? ( string ) $mValue // cast string |
43
|
|
|
: ( array ) $mValue // cast array |
44
|
|
|
) |
45
|
|
|
: $mValue; |
46
|
|
|
$mValue = is_array( $mValue ) |
47
|
|
|
? self::_getArrayMappedRecursive( |
48
|
|
|
self::_getSlicedByDepth( $mValue, 10 ), |
49
|
|
|
array( __CLASS__, '_getObjectName' ) |
50
|
|
|
) |
51
|
|
|
: $mValue; |
52
|
|
|
return self::_getArrayRepresentationSanitized( print_r( $mValue, true ) ); |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns a object name if it is an object. Otherwise, the value itself. |
58
|
|
|
* This is used to convert objects into a string in array-walk functions |
59
|
|
|
* as objects tent to get large when they are converted to a string representation. |
60
|
|
|
* @since 3.8.9 |
61
|
|
|
*/ |
62
|
|
|
static private function _getObjectName( $mItem ) { |
|
|
|
|
63
|
|
|
if ( is_object( $mItem ) ) { |
64
|
|
|
return '(object) ' . get_class( $mItem ); |
65
|
|
|
} |
66
|
|
|
return $mItem; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @since 3.8.9 |
71
|
|
|
* @param callable $asoCallable |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
static private function _getLegibleCallable( $asoCallable ) { |
|
|
|
|
75
|
|
|
|
76
|
|
|
if ( is_string( $asoCallable ) ) { |
77
|
|
|
return '(callable) ' . $asoCallable; |
78
|
|
|
} |
79
|
|
|
if ( is_object( $asoCallable ) ) { |
80
|
|
|
return '(callable) ' . get_class( $asoCallable ); |
81
|
|
|
} |
82
|
|
|
$_sSubject = is_object( $asoCallable[ 0 ] ) |
83
|
|
|
? get_class( $asoCallable[ 0 ] ) |
84
|
|
|
: ( string ) $asoCallable[ 0 ]; |
85
|
|
|
|
86
|
|
|
return '(callable) ' . $_sSubject . '::' . ( string ) $asoCallable[ 1 ]; |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
/** |
90
|
|
|
* @since 3.8.9 |
91
|
|
|
* @param object $oObject |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
static public function _getLegibleObject( $oObject ) { |
|
|
|
|
95
|
|
|
|
96
|
|
|
if ( method_exists( $oObject, '__toString' ) ) { |
97
|
|
|
return ( string ) $oObject; |
98
|
|
|
} |
99
|
|
|
return '(object) ' . get_class( $oObject ) . ' ' |
100
|
|
|
. count( get_object_vars( $oObject ) ) . ' properties.'; |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
/** |
104
|
|
|
* Returns an array representation with value types in each element. |
105
|
|
|
* The element deeper than 10 dimensions will be dropped. |
106
|
|
|
* @since 3.8.9 |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
static public function _getLegibleArray( array $aArray ) { |
|
|
|
|
110
|
|
|
return self::_getArrayMappedRecursive( |
111
|
|
|
self::_getSlicedByDepth( $aArray, 10 ), |
112
|
|
|
array( __CLASS__, '_getLegibleValue' ) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
/** |
116
|
|
|
* @since 3.8.9 |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
static private function _getLegibleValue( $mItem ) { |
|
|
|
|
120
|
|
|
if ( is_callable( $mItem ) ) { |
121
|
|
|
return self::_getLegibleCallable( $mItem ); |
122
|
|
|
} |
123
|
|
|
return is_scalar( $mItem ) |
124
|
|
|
? self::_getLegibleScalar( $mItem ) |
125
|
|
|
: self::_getLegibleNonScalar( $mItem ); |
126
|
|
|
} |
127
|
|
|
/** |
128
|
|
|
* @since 3.8.9 |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
static private function _getLegibleNonScalar( $mNonScalar ) { |
|
|
|
|
132
|
|
|
|
133
|
|
|
$_sType = gettype( $mNonScalar ); |
134
|
|
|
if ( is_null( $mNonScalar ) ) { |
135
|
|
|
return '(null)'; |
136
|
|
|
} |
137
|
|
|
if ( is_object( $mNonScalar ) ) { |
138
|
|
|
return '(' . $_sType . ') ' . get_class( $mNonScalar ); |
139
|
|
|
} |
140
|
|
|
if ( is_array( $mNonScalar ) ) { |
141
|
|
|
return '(' . $_sType . ') ' . count( $mNonScalar ) . ' elements'; |
142
|
|
|
} |
143
|
|
|
return '(' . $_sType . ') ' . ( string ) $mNonScalar; |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
/** |
147
|
|
|
* @return string |
148
|
|
|
* @param scalar $sScalar |
149
|
|
|
* @param integer $iCharLimit Character length limit to truncate. |
|
|
|
|
150
|
|
|
* @since 3.8.9 |
151
|
|
|
*/ |
152
|
|
|
static private function _getLegibleScalar( $sScalar ) { |
|
|
|
|
153
|
|
|
|
154
|
|
|
if ( is_bool( $sScalar ) ) { |
155
|
|
|
return '(boolean) ' . ( $sScalar ? 'true' : 'false' ); |
156
|
|
|
} |
157
|
|
|
return is_string( $sScalar ) |
158
|
|
|
? self::_getLegibleString( $sScalar ) |
159
|
|
|
: '(' . gettype( $sScalar ) . ', length: ' . self::_getValueLength( $sScalar ) . ') ' . $sScalar; |
160
|
|
|
} |
161
|
|
|
/** |
162
|
|
|
* Returns a length of a value. |
163
|
|
|
* @since 3.5.3 |
164
|
|
|
* @internal |
165
|
|
|
* @return integer|null For string or integer, the string length. For array, the element lengths. For other types, null. |
166
|
|
|
*/ |
167
|
|
|
static private function _getValueLength( $mValue ) { |
|
|
|
|
168
|
|
|
$_sVariableType = gettype( $mValue ); |
169
|
|
|
if ( in_array( $_sVariableType, array( 'string', 'integer' ) ) ) { |
170
|
|
|
return strlen( $mValue ); |
171
|
|
|
} |
172
|
|
|
if ( 'array' === $_sVariableType ) { |
173
|
|
|
return count( $mValue ); |
174
|
|
|
} |
175
|
|
|
return null; |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
/** |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
static private function _getLegibleString( $sString, $iCharLimit=200 ) { |
|
|
|
|
182
|
|
|
|
183
|
|
|
static $_iMBSupport; |
184
|
|
|
$_iMBSupport = isset( $_iMBSupport ) ? $_iMBSupport : ( integer ) function_exists( 'mb_strlen' ); |
185
|
|
|
$_aStrLenMethod = array( 'strlen', 'mb_strlen' ); |
186
|
|
|
$_aSubstrMethod = array( 'substr', 'mb_substr' ); |
187
|
|
|
|
188
|
|
|
$_iCharLength = call_user_func_array( $_aStrLenMethod[ $_iMBSupport ], array( $sString ) ); |
189
|
|
|
return $_iCharLength <= $iCharLimit |
190
|
|
|
? '(string, length: ' . $_iCharLength . ') ' . $sString |
191
|
|
|
: '(string, length: ' . $_iCharLength . ') ' . call_user_func_array( $_aSubstrMethod[ $_iMBSupport ], array( $sString, 0, $iCharLimit ) ) |
192
|
|
|
. '...'; |
193
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return string |
198
|
|
|
* @since 3.8.9 |
199
|
|
|
*/ |
200
|
|
|
static protected function _getArrayRepresentationSanitized( $sString ) { |
|
|
|
|
201
|
|
|
|
202
|
|
|
// Fix extra line breaks after `Array()` |
203
|
|
|
$sString = preg_replace( |
204
|
|
|
'/\)(\r\n?|\n)(?=(\r\n?|\n)\s+[\[\)])/', // needle |
205
|
|
|
')', // replacement |
206
|
|
|
$sString // subject |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
// Fix empty array output |
210
|
|
|
$sString = preg_replace( |
211
|
|
|
'/Array(\r\n?|\n)\s+\((\r\n?|\n)\s+\)/', // needle |
212
|
|
|
'Array()', // replacement |
213
|
|
|
$sString // subject |
214
|
|
|
); |
215
|
|
|
return $sString; |
216
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Slices an array by the given depth. |
221
|
|
|
* |
222
|
|
|
* @since 3.4.4 |
223
|
|
|
* @since 3.8.9 Changed it not to convert an object into an array. |
224
|
|
|
* @since 3.8.9 Changed the scope to private. |
225
|
|
|
* @since 3.8.9 Renamed from `getSliceByDepth()`. |
226
|
|
|
* @return array |
227
|
|
|
* @internal |
228
|
|
|
*/ |
229
|
|
|
static private function _getSlicedByDepth( array $aSubject, $iDepth=0 ) { |
|
|
|
|
230
|
|
|
|
231
|
|
|
foreach ( $aSubject as $_sKey => $_vValue ) { |
232
|
|
|
if ( is_array( $_vValue ) ) { |
233
|
|
|
$_iDepth = $iDepth; |
234
|
|
|
if ( $iDepth > 0 ) { |
235
|
|
|
$aSubject[ $_sKey ] = self::_getSlicedByDepth( $_vValue, --$iDepth ); |
236
|
|
|
$iDepth = $_iDepth; |
237
|
|
|
continue; |
238
|
|
|
} |
239
|
|
|
unset( $aSubject[ $_sKey ] ); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
return $aSubject; |
243
|
|
|
|
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Performs `array_map()` recursively. |
248
|
|
|
* @return array |
249
|
|
|
* @since 3.8.9 |
250
|
|
|
*/ |
251
|
|
|
static private function _getArrayMappedRecursive( array $aArray, $oCallable ) { |
|
|
|
|
252
|
|
|
|
253
|
|
|
self::$_oCurrentCallableForArrayMapRecursive = $oCallable; |
254
|
|
|
$_aArray = array_map( array( __CLASS__, '_getArrayMappedNested' ), $aArray ); |
255
|
|
|
self::$_oCurrentCallableForArrayMapRecursive = null; |
256
|
|
|
return $_aArray; |
257
|
|
|
|
258
|
|
|
} |
259
|
|
|
static private $_oCurrentCallableForArrayMapRecursive; |
260
|
|
|
/** |
261
|
|
|
* @internal |
262
|
|
|
* @return mixed A modified value. |
263
|
|
|
* @since 3.8.9 |
264
|
|
|
*/ |
265
|
|
|
static private function _getArrayMappedNested( $mItem ) { |
|
|
|
|
266
|
|
|
return is_array( $mItem ) |
267
|
|
|
? array_map( array( __CLASS__, '_getArrayMappedNested' ), $mItem ) |
268
|
|
|
: call_user_func( self::$_oCurrentCallableForArrayMapRecursive, $mItem ); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
|
272
|
|
|
/* Deprecated Methods */ |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Prints out the given variable contents. |
276
|
|
|
* |
277
|
|
|
* If a file pass is given, it saves the output in the file. |
278
|
|
|
* |
279
|
|
|
* @since unknown |
280
|
|
|
* @deprecated 3.2.0 |
281
|
|
|
*/ |
282
|
|
|
static public function dumpArray( $asArray, $sFilePath=null ) { |
|
|
|
|
283
|
|
|
self::showDeprecationNotice( 'AdminPageFramework_Debug::' . __FUNCTION__, 'AdminPageFramework_Debug::dump()' ); |
284
|
|
|
AdminPageFramework_Debug::dump( $asArray, $sFilePath ); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Retrieves the output of the given array contents. |
289
|
|
|
* |
290
|
|
|
* If a file pass is given, it saves the output in the file. |
291
|
|
|
* |
292
|
|
|
* @since 2.1.6 The $bEncloseInTag parameter is added. |
293
|
|
|
* @since 3.0.0 Changed the $bEncloseInTag parameter to bEscape. |
294
|
|
|
* @deprecated 3.2.0 |
295
|
|
|
*/ |
296
|
|
|
static public function getArray( $asArray, $sFilePath=null, $bEscape=true ) { |
|
|
|
|
297
|
|
|
self::showDeprecationNotice( 'AdminPageFramework_Debug::' . __FUNCTION__, 'AdminPageFramework_Debug::get()' ); |
298
|
|
|
return AdminPageFramework_Debug::get( $asArray, $sFilePath, $bEscape ); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Logs the given array output into the given file. |
303
|
|
|
* |
304
|
|
|
* @since 2.1.1 |
305
|
|
|
* @since 3.0.3 Changed the default log location and file name. |
306
|
|
|
* @deprecated 3.1.0 Use the `log()` method instead. |
307
|
|
|
*/ |
308
|
|
|
static public function logArray( $asArray, $sFilePath=null ) { |
|
|
|
|
309
|
|
|
self::showDeprecationNotice( 'AdminPageFramework_Debug::' . __FUNCTION__, 'AdminPageFramework_Debug::log()' ); |
310
|
|
|
AdminPageFramework_Debug::log( $asArray, $sFilePath ); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Returns a string representation of the given value. |
315
|
|
|
* @since 3.5.0 |
316
|
|
|
* @param mixed $mValue The value to get as a string |
317
|
|
|
* @internal |
318
|
|
|
* @return string |
319
|
|
|
* @deprecated 3.8.9 |
320
|
|
|
*/ |
321
|
|
|
static public function getAsString( $mValue ) { |
|
|
|
|
322
|
|
|
self::showDeprecationNotice( 'AdminPageFramework_Debug::' . __FUNCTION__ ); |
323
|
|
|
return self::_getLegible( $mValue ); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
} |
327
|
|
|
|