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
|
|
|
* Provides debugging methods. |
12
|
|
|
* |
13
|
|
|
* Use the methods of this class to check variable contents. |
14
|
|
|
* |
15
|
|
|
* @image http://admin-page-framework.michaeluno.jp/image/common/utility/debug.png |
16
|
|
|
* @since 2.0.0 |
17
|
|
|
* @since 3.1.3 Extends AdminPageFramework_WPUtility |
18
|
|
|
* @since 3.7.1 Extends AdminPageFramework_FrameworkUtility |
19
|
|
|
* @extends AdminPageFramework_FrameworkUtility |
20
|
|
|
* @package AdminPageFramework |
21
|
|
|
* @subpackage Common/Utility |
22
|
|
|
*/ |
23
|
|
|
class AdminPageFramework_Debug extends AdminPageFramework_Debug_Base { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Prints out the given variable contents |
27
|
|
|
* |
28
|
|
|
* If a file pass is given to the second parameter, it saves the output in the file. |
29
|
|
|
* |
30
|
|
|
* @since 3.2.0 |
31
|
|
|
* @remark An alias of the dumpArray() method. |
32
|
|
|
* @param array|string $asArray The variable to check its contents. |
33
|
|
|
* @param string $sFilePath The file path for a log file. |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
static public function dump( $asArray, $sFilePath=null ) { |
|
|
|
|
37
|
|
|
echo self::get( $asArray, $sFilePath ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns a string representation of a given value with details. |
42
|
|
|
* @since 3.8.9 |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
static public function getDetails( $mValue, $bEscape=true ) { |
|
|
|
|
46
|
|
|
$_sValueWithDetails = self::_getArrayRepresentationSanitized( |
47
|
|
|
self::_getLegibleDetails( $mValue ) |
48
|
|
|
); |
49
|
|
|
return $bEscape |
50
|
|
|
? "<pre class='dump-array'>" |
51
|
|
|
. htmlspecialchars( $_sValueWithDetails ) |
52
|
|
|
. "</pre>" |
53
|
|
|
: $_sValueWithDetails; // non-escape is used for exporting data into file. |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Retrieves the output of the given variable contents. |
58
|
|
|
* |
59
|
|
|
* If a file pass is given to the second parameter, it saves the output in the file. |
60
|
|
|
* |
61
|
|
|
* @remark An alias of getArray() method. |
62
|
|
|
* @since 3.2.0 |
63
|
|
|
* @param array|string $asArray The variable to check its contents. |
64
|
|
|
* @param string $sFilePath The file path for a log file. |
65
|
|
|
* @param boolean $bEscape Whether to escape characters. |
66
|
|
|
*/ |
67
|
|
|
static public function get( $asArray, $sFilePath=null, $bEscape=true ) { |
|
|
|
|
68
|
|
|
|
69
|
|
|
if ( $sFilePath ) { |
|
|
|
|
70
|
|
|
self::log( $asArray, $sFilePath ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $bEscape |
74
|
|
|
? "<pre class='dump-array'>" |
75
|
|
|
. htmlspecialchars( self::_getLegible( $asArray ) ) // `esc_html()` breaks with complex HTML code. |
76
|
|
|
. "</pre>" |
77
|
|
|
: self::_getLegible( $asArray ); // non-escape is used for exporting data into file. |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Logs the given variable output to a file. |
83
|
|
|
* |
84
|
|
|
* <h4>Example</h4> |
85
|
|
|
* <code> |
86
|
|
|
* $_aValues = array( 'foo', 'bar' ); |
87
|
|
|
* AdminPageFramework_Debug::log( $aValues ); |
88
|
|
|
* </code> |
89
|
|
|
* |
90
|
|
|
* @remark The alias of the `logArray()` method. |
91
|
|
|
* @since 3.1.0 |
92
|
|
|
* @since 3.1.3 Made it leave milliseconds and elapsed time from the last call of the method. |
93
|
|
|
* @since 3.3.0 Made it indicate the data type. |
94
|
|
|
* @since 3.3.1 Made it indicate the data length. |
95
|
|
|
* @param mixed $mValue The value to log. |
96
|
|
|
* @param string $sFilePath The log file path. |
97
|
|
|
* @return void |
98
|
|
|
**/ |
99
|
|
|
static public function log( $mValue, $sFilePath=null ) { |
|
|
|
|
100
|
|
|
|
101
|
|
|
static $_fPreviousTimeStamp = 0; |
102
|
|
|
|
103
|
|
|
$_oCallerInfo = debug_backtrace(); |
104
|
|
|
$_sCallerFunction = self::getElement( |
105
|
|
|
$_oCallerInfo, // subject array |
106
|
|
|
array( 1, 'function' ), // key |
107
|
|
|
'' // default |
108
|
|
|
); |
109
|
|
|
$_sCallerClass = self::getElement( |
110
|
|
|
$_oCallerInfo, // subject array |
111
|
|
|
array( 1, 'class' ), // key |
112
|
|
|
'' // default |
113
|
|
|
); |
114
|
|
|
$_fCurrentTimeStamp = microtime( true ); |
115
|
|
|
|
116
|
|
|
file_put_contents( |
117
|
|
|
self::_getLogFilePath( $sFilePath, $_sCallerClass ), |
118
|
|
|
self::_getLogHeadingLine( |
119
|
|
|
$_fCurrentTimeStamp, |
120
|
|
|
round( $_fCurrentTimeStamp - $_fPreviousTimeStamp, 3 ), // elapsed time |
121
|
|
|
$_sCallerClass, |
122
|
|
|
$_sCallerFunction |
123
|
|
|
) . PHP_EOL |
124
|
|
|
. self::_getLegibleDetails( $mValue ) . PHP_EOL . PHP_EOL, |
125
|
|
|
FILE_APPEND |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$_fPreviousTimeStamp = $_fCurrentTimeStamp; |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
/** |
132
|
|
|
* Determines the log file path. |
133
|
|
|
* @since 3.5.3 |
134
|
|
|
* @internal |
135
|
|
|
* @return string The path of the file to log the contents. |
136
|
|
|
*/ |
137
|
|
|
static private function _getLogFilePath( $bsFilePath, $sCallerClass ) { |
|
|
|
|
138
|
|
|
|
139
|
|
|
$_bFileExists = self::_createFile( $bsFilePath ); |
140
|
|
|
if ( $_bFileExists ) { |
141
|
|
|
return $bsFilePath; |
142
|
|
|
} |
143
|
|
|
// Return a generated default log path. |
144
|
|
|
if ( true === $bsFilePath ) { |
145
|
|
|
return WP_CONTENT_DIR . DIRECTORY_SEPARATOR . basename( get_class() ) . '_' . date( "Ymd" ) . '.log'; |
146
|
|
|
} |
147
|
|
|
return WP_CONTENT_DIR . DIRECTORY_SEPARATOR . basename( get_class() ) . '_' . basename( $sCallerClass ) . '_' . date( "Ymd" ) . '.log'; |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
/** |
151
|
|
|
* Creates a file. |
152
|
|
|
* @return boolean |
153
|
|
|
* @internal |
154
|
|
|
*/ |
155
|
|
|
static private function _createFile( $sFilePath ) { |
|
|
|
|
156
|
|
|
if ( ! $sFilePath || true === $sFilePath ) { |
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
if ( file_exists( $sFilePath ) ) { |
160
|
|
|
return true; |
161
|
|
|
} |
162
|
|
|
// Otherwise, create a file. |
163
|
|
|
$_bhResrouce = fopen( $sFilePath, 'w' ); |
164
|
|
|
return ( boolean ) $_bhResrouce; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Returns the heading part of a log item. |
169
|
|
|
* @since 3.5.3 |
170
|
|
|
* @internal |
171
|
|
|
* @return string the heading part of a log item. |
172
|
|
|
*/ |
173
|
|
|
static private function _getLogHeadingLine( $fCurrentTimeStamp, $nElapsed, $sCallerClass, $sCallerFunction ) { |
|
|
|
|
174
|
|
|
|
175
|
|
|
static $_iPageLoadID; // identifies the page load. |
176
|
|
|
static $_nGMTOffset; |
177
|
|
|
|
178
|
|
|
$_nGMTOffset = isset( $_nGMTOffset ) |
179
|
|
|
? $_nGMTOffset |
180
|
|
|
: get_option( 'gmt_offset' ); |
181
|
|
|
$_iPageLoadID = $_iPageLoadID |
182
|
|
|
? $_iPageLoadID |
183
|
|
|
: uniqid(); |
184
|
|
|
$_nNow = $fCurrentTimeStamp + ( $_nGMTOffset * 60 * 60 ); |
185
|
|
|
$_nMicroseconds = str_pad( round( ( $_nNow - floor( $_nNow ) ) * 10000 ), 4, '0' ); |
186
|
|
|
|
187
|
|
|
$_aOutput = array( |
188
|
|
|
date( "Y/m/d H:i:s", $_nNow ) . '.' . $_nMicroseconds, |
189
|
|
|
self::_getFormattedElapsedTime( $nElapsed ), |
190
|
|
|
$_iPageLoadID, |
191
|
|
|
AdminPageFramework_Registry::getVersion(), |
192
|
|
|
$sCallerClass . '::' . $sCallerFunction, |
193
|
|
|
current_filter(), |
194
|
|
|
self::getCurrentURL(), |
195
|
|
|
); |
196
|
|
|
return implode( ' ', $_aOutput ); |
197
|
|
|
|
198
|
|
|
} |
199
|
|
|
/** |
200
|
|
|
* Returns formatted elapsed time. |
201
|
|
|
* @since 3.5.3 |
202
|
|
|
* @internal |
203
|
|
|
* @return string Formatted elapsed time. |
204
|
|
|
*/ |
205
|
|
|
static private function _getFormattedElapsedTime( $nElapsed ) { |
|
|
|
|
206
|
|
|
|
207
|
|
|
$_aElapsedParts = explode( ".", ( string ) $nElapsed ); |
208
|
|
|
$_sElapsedFloat = str_pad( |
209
|
|
|
self::getElement( |
210
|
|
|
$_aElapsedParts, // subject array |
211
|
|
|
1, // key |
212
|
|
|
0 // default |
213
|
|
|
), |
214
|
|
|
3, |
215
|
|
|
'0' |
216
|
|
|
); |
217
|
|
|
$_sElapsed = self::getElement( |
218
|
|
|
$_aElapsedParts, // subject array |
219
|
|
|
0, // key |
220
|
|
|
0 // default |
221
|
|
|
); |
222
|
|
|
$_sElapsed = strlen( $_sElapsed ) > 1 |
223
|
|
|
? '+' . substr( $_sElapsed, -1, 2 ) |
224
|
|
|
: ' ' . $_sElapsed; |
225
|
|
|
return $_sElapsed . '.' . $_sElapsedFloat; |
226
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
} |
230
|
|
|
|