|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System Framework package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
// ------------------------------------------------------------------------ |
|
12
|
|
|
/** |
|
13
|
|
|
* Printer Helper |
|
14
|
|
|
* |
|
15
|
|
|
* A collection of helper function to help PHP programmer to prints a human-readable information |
|
16
|
|
|
* about a variable, object and etc into the browser or command line. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
// ------------------------------------------------------------------------ |
|
20
|
|
|
|
|
21
|
|
|
if ( ! function_exists('var_format')) { |
|
22
|
|
|
/** |
|
23
|
|
|
* var_format |
|
24
|
|
|
* |
|
25
|
|
|
* Formats a variable with extra information. |
|
26
|
|
|
* |
|
27
|
|
|
* @param mixed $expression The variable tobe formatted. |
|
28
|
|
|
* |
|
29
|
|
|
* @return mixed |
|
30
|
|
|
*/ |
|
31
|
|
|
function var_format($expression) |
|
32
|
|
|
{ |
|
33
|
|
|
if (is_bool($expression)) { |
|
34
|
|
|
if ($expression === true) { |
|
35
|
|
|
$expression = '(bool) TRUE'; |
|
36
|
|
|
} else { |
|
37
|
|
|
$expression = '(bool) FALSE'; |
|
38
|
|
|
} |
|
39
|
|
|
} elseif (is_resource($expression)) { |
|
40
|
|
|
$expression = '(resource) ' . get_resource_type($expression); |
|
41
|
|
|
} elseif (is_array($expression) || is_object($expression)) { |
|
42
|
|
|
$expression = @print_r($expression, true); |
|
43
|
|
|
} elseif (is_int($expression) OR is_numeric($expression)) { |
|
44
|
|
|
$expression = '(int) ' . $expression; |
|
45
|
|
|
} elseif (is_null($expression)) { |
|
46
|
|
|
$expression = '(null)'; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $expression; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// ------------------------------------------------------------------------ |
|
54
|
|
|
|
|
55
|
|
|
if ( ! function_exists('print_out')) { |
|
56
|
|
|
/** |
|
57
|
|
|
* print_code |
|
58
|
|
|
* |
|
59
|
|
|
* Prints a variable into Gear Browser. |
|
60
|
|
|
* |
|
61
|
|
|
* @param mixed $expression The variable tobe formatted. |
|
62
|
|
|
* @param bool $exit The exit flag of the current script execution. |
|
63
|
|
|
*/ |
|
64
|
|
|
function print_out($expression, $exit = true) |
|
65
|
|
|
{ |
|
66
|
|
|
if (php_sapi_name() === 'cli') { |
|
67
|
|
|
print_cli($expression, $exit); |
|
68
|
|
|
|
|
69
|
|
|
return; |
|
70
|
|
|
} elseif ( ! empty($_SERVER[ 'HTTP_X_REQUESTED_WITH' ]) && strtolower( |
|
71
|
|
|
$_SERVER[ 'HTTP_X_REQUESTED_WITH' ] |
|
72
|
|
|
) === 'xmlhttprequest') { |
|
73
|
|
|
if (is_array($expression)) { |
|
74
|
|
|
echo json_encode($expression, JSON_PRETTY_PRINT); |
|
75
|
|
|
} elseif (is_object($expression)) { |
|
76
|
|
|
print_r($expression); |
|
77
|
|
|
} else { |
|
78
|
|
|
echo $expression; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ($exit) { |
|
82
|
|
|
die; |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
} else { |
|
85
|
|
|
echo (new \O2System\Gear\Browser($expression))->render(); |
|
86
|
|
|
|
|
87
|
|
|
if ($exit) { |
|
88
|
|
|
die; |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// ------------------------------------------------------------------------ |
|
95
|
|
|
|
|
96
|
|
|
if ( ! function_exists('print_console')) { |
|
97
|
|
|
/** |
|
98
|
|
|
* print_console |
|
99
|
|
|
* |
|
100
|
|
|
* Prints a variable into browser console. |
|
101
|
|
|
* |
|
102
|
|
|
* @param mixed $expression The variable tobe formatted. |
|
103
|
|
|
* @param bool $exit The exit flag of the current script execution. |
|
104
|
|
|
*/ |
|
105
|
|
|
function print_console( |
|
106
|
|
|
$expression, |
|
107
|
|
|
$label = null, |
|
108
|
|
|
$messageType = \O2System\Gear\Console::LOG_MESSAGE, |
|
109
|
|
|
$exit = true |
|
110
|
|
|
) { |
|
111
|
|
|
if (php_sapi_name() === 'cli') { |
|
112
|
|
|
print_cli($expression, $exit); |
|
113
|
|
|
|
|
114
|
|
|
return; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
(new \O2System\Gear\Console($label, $expression, $messageType))->send(); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
// ------------------------------------------------------------------------ |
|
122
|
|
|
|
|
123
|
|
|
if ( ! function_exists('print_cli')) { |
|
124
|
|
|
/** |
|
125
|
|
|
* print_cli |
|
126
|
|
|
* |
|
127
|
|
|
* Prints a variable into command line interface output. |
|
128
|
|
|
* |
|
129
|
|
|
* @param mixed $expression The variable tobe formatted. |
|
130
|
|
|
* @param bool $exit The exit flag of the current script execution. |
|
131
|
|
|
*/ |
|
132
|
|
|
function print_cli($expression, $exit = true) |
|
133
|
|
|
{ |
|
134
|
|
|
(new \O2System\Gear\Cli($expression))->send(); |
|
135
|
|
|
|
|
136
|
|
|
if ($exit) { |
|
137
|
|
|
die; |
|
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
// ------------------------------------------------------------------------ |
|
143
|
|
|
|
|
144
|
|
|
if ( ! function_exists('print_code')) { |
|
145
|
|
|
/** |
|
146
|
|
|
* print_code |
|
147
|
|
|
* |
|
148
|
|
|
* Prints a variable inside pre-code html tag. |
|
149
|
|
|
* |
|
150
|
|
|
* @param mixed $expression The variable tobe formatted. |
|
151
|
|
|
* @param bool $exit The exit flag of the current script execution. |
|
152
|
|
|
*/ |
|
153
|
|
|
function print_code($expression, $exit = false) |
|
154
|
|
|
{ |
|
155
|
|
|
$expression = htmlentities(var_format($expression)); |
|
156
|
|
|
$expression = htmlspecialchars(htmlspecialchars_decode($expression, ENT_QUOTES), ENT_QUOTES, 'UTF-8'); |
|
157
|
|
|
|
|
158
|
|
|
echo '<pre>' . $expression . '</pre>'; |
|
159
|
|
|
|
|
160
|
|
|
if ($exit === true) { |
|
161
|
|
|
die; |
|
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
// ------------------------------------------------------------------------ |
|
167
|
|
|
|
|
168
|
|
|
if ( ! function_exists('print_dump')) { |
|
169
|
|
|
/** |
|
170
|
|
|
* print_dump |
|
171
|
|
|
* |
|
172
|
|
|
* Prints a dumps information about a variable inside pre-code html tag. |
|
173
|
|
|
* |
|
174
|
|
|
* @param mixed $expression The variable tobe formatted. |
|
175
|
|
|
* @param bool $exit The exit flag of the current script execution. |
|
176
|
|
|
*/ |
|
177
|
|
|
function print_dump($expression, $exit = false) |
|
178
|
|
|
{ |
|
179
|
|
|
ob_start(); |
|
180
|
|
|
var_dump($expression); |
|
|
|
|
|
|
181
|
|
|
$output = ob_get_contents(); |
|
182
|
|
|
ob_end_clean(); |
|
183
|
|
|
|
|
184
|
|
|
if (strpos($output, 'xdebug-var-dump') !== false) { |
|
185
|
|
|
if (defined('PATH_ROOT')) { |
|
186
|
|
|
$helper_file = implode(DIRECTORY_SEPARATOR, |
|
187
|
|
|
['vendor', 'o2system', 'gear', 'src', 'Helper.php:170:']); |
|
188
|
|
|
$output = str_replace('<small>' . PATH_ROOT . $helper_file . '</small>', '', $output); |
|
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
echo $output; |
|
192
|
|
|
|
|
193
|
|
|
if ($exit) { |
|
194
|
|
|
die; |
|
|
|
|
|
|
195
|
|
|
} |
|
196
|
|
|
} else { |
|
197
|
|
|
print_code($output, $exit); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
// ------------------------------------------------------------------------ |
|
203
|
|
|
|
|
204
|
|
|
if ( ! function_exists('print_json')) { |
|
205
|
|
|
/** |
|
206
|
|
|
* print_json |
|
207
|
|
|
* |
|
208
|
|
|
* Prints a variable into json formatted string. |
|
209
|
|
|
* |
|
210
|
|
|
* @param mixed $expression The variable tobe formatted. |
|
211
|
|
|
* @param int $options The optional json_encode options. |
|
212
|
|
|
* @param bool $exit The exit flag of the current script execution. |
|
213
|
|
|
*/ |
|
214
|
|
|
function print_json($expression, $options = null, $exit = true) |
|
215
|
|
|
{ |
|
216
|
|
|
if (is_bool($options)) { |
|
|
|
|
|
|
217
|
|
|
$exit = $options; |
|
218
|
|
|
$options = null; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
if (is_array($expression) || is_object($expression)) { |
|
222
|
|
|
if (empty($options)) { |
|
223
|
|
|
$output = json_encode($expression); |
|
224
|
|
|
} else { |
|
225
|
|
|
$output = json_encode($expression, $options); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
print_out($output, $exit); |
|
229
|
|
|
} else { |
|
230
|
|
|
print_out('Invalid Expression!', $exit); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
// ------------------------------------------------------------------------ |
|
236
|
|
|
|
|
237
|
|
|
if ( ! function_exists('print_serialize')) { |
|
238
|
|
|
/** |
|
239
|
|
|
* print_serialize |
|
240
|
|
|
* |
|
241
|
|
|
* Prints a variable into serialized formatted string. |
|
242
|
|
|
* |
|
243
|
|
|
* @param mixed $expression The variable tobe formatted. |
|
244
|
|
|
* @param bool $exit The exit flag of the current script execution. |
|
245
|
|
|
*/ |
|
246
|
|
|
function print_serialize($expression, $exit = true) |
|
247
|
|
|
{ |
|
248
|
|
|
if (is_array($expression) || is_object($expression)) { |
|
249
|
|
|
print_out(serialize($expression), $exit); |
|
250
|
|
|
} else { |
|
251
|
|
|
print_out('Invalid Expression!', $exit); |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
// ------------------------------------------------------------------------ |
|
257
|
|
|
|
|
258
|
|
|
if ( ! function_exists('print_line')) { |
|
259
|
|
|
/** |
|
260
|
|
|
* print_line |
|
261
|
|
|
* |
|
262
|
|
|
* Prints a multiple stacked lines of variable. |
|
263
|
|
|
* |
|
264
|
|
|
* The print_line command can be placed in various places in the source code program, |
|
265
|
|
|
* the print_line will stacked all variable into a static memory and will be printed when |
|
266
|
|
|
* the expression is fill with --- string. |
|
267
|
|
|
* |
|
268
|
|
|
* @param mixed $expression The variable tobe formatted. |
|
269
|
|
|
* @param bool $exit The exit flag of the current script execution. |
|
270
|
|
|
*/ |
|
271
|
|
|
function print_line($expression, $exit = false) |
|
272
|
|
|
{ |
|
273
|
|
|
static $lines; |
|
274
|
|
|
|
|
275
|
|
|
if (strtoupper($exit) === 'FLUSH') { |
|
276
|
|
|
$lines = []; |
|
277
|
|
|
$lines[] = $expression; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
if (is_array($expression) || is_object($expression)) { |
|
281
|
|
|
$lines[] = print_r($expression, true); |
|
282
|
|
|
} else { |
|
283
|
|
|
$lines[] = var_format($expression); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
if ($exit === true || $expression === '---') { |
|
287
|
|
|
$expression = implode(PHP_EOL, $lines); |
|
288
|
|
|
$lines = []; |
|
289
|
|
|
|
|
290
|
|
|
print_out($expression, $exit); |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
// ------------------------------------------------------------------------ |
|
296
|
|
|
|
|
297
|
|
|
if ( ! function_exists('pre_open')) { |
|
298
|
|
|
/** |
|
299
|
|
|
* pre_open |
|
300
|
|
|
* |
|
301
|
|
|
* Prints an open pre HTML tag. |
|
302
|
|
|
*/ |
|
303
|
|
|
function pre_open() |
|
304
|
|
|
{ |
|
305
|
|
|
echo '<pre>'; |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
// ------------------------------------------------------------------------ |
|
310
|
|
|
|
|
311
|
|
|
if ( ! function_exists('pre_line')) { |
|
312
|
|
|
/** |
|
313
|
|
|
* pre_line |
|
314
|
|
|
* |
|
315
|
|
|
* Prints a variable into pre HTML tag. |
|
316
|
|
|
* |
|
317
|
|
|
* @param mixed $expression The variable tobe formatted. |
|
318
|
|
|
* @param bool $implode The flag to implode lines. |
|
319
|
|
|
*/ |
|
320
|
|
|
function pre_line($expression, $implode = true) |
|
321
|
|
|
{ |
|
322
|
|
|
if (is_array($expression) AND $implode === true) { |
|
323
|
|
|
$expression = implode(PHP_EOL, $expression); |
|
324
|
|
|
} elseif (is_bool($expression)) { |
|
325
|
|
|
if ($expression === true) { |
|
326
|
|
|
$expression = '(bool) TRUE'; |
|
327
|
|
|
} else { |
|
328
|
|
|
$expression = '(bool) FALSE'; |
|
329
|
|
|
} |
|
330
|
|
|
} elseif (is_resource($expression)) { |
|
331
|
|
|
$expression = '(resource) ' . get_resource_type($expression); |
|
332
|
|
|
} elseif (is_array($expression) || is_object($expression)) { |
|
333
|
|
|
$expression = @print_r($expression, true); |
|
334
|
|
|
} elseif (is_int($expression) OR is_numeric($expression)) { |
|
335
|
|
|
$expression = '(int) ' . $expression; |
|
336
|
|
|
} elseif (is_null($expression)) { |
|
337
|
|
|
$expression = '(null)'; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
echo $expression . PHP_EOL; |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
// ------------------------------------------------------------------------ |
|
345
|
|
|
|
|
346
|
|
|
if ( ! function_exists('pre_close')) { |
|
347
|
|
|
/** |
|
348
|
|
|
* pre_close |
|
349
|
|
|
* |
|
350
|
|
|
* Prints a close pre HTML tag. |
|
351
|
|
|
*/ |
|
352
|
|
|
function pre_close($exit = false) |
|
353
|
|
|
{ |
|
354
|
|
|
echo '</pre>'; |
|
355
|
|
|
|
|
356
|
|
|
if ($exit) { |
|
357
|
|
|
die; |
|
|
|
|
|
|
358
|
|
|
} |
|
359
|
|
|
} |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
// ------------------------------------------------------------------------ |
|
363
|
|
|
|
|
364
|
|
|
if ( ! function_exists('debug_start')) { |
|
365
|
|
|
/** |
|
366
|
|
|
* debug_start |
|
367
|
|
|
* |
|
368
|
|
|
* Starts a debugger stacks. |
|
369
|
|
|
*/ |
|
370
|
|
|
function debug_start() |
|
371
|
|
|
{ |
|
372
|
|
|
\O2System\Gear\Debugger::start(); |
|
373
|
|
|
} |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
// ------------------------------------------------------------------------ |
|
377
|
|
|
|
|
378
|
|
|
if ( ! function_exists('debug_line')) { |
|
379
|
|
|
/** |
|
380
|
|
|
* debug_line |
|
381
|
|
|
* |
|
382
|
|
|
* Add a debug line variable into debugger stacks. |
|
383
|
|
|
* |
|
384
|
|
|
* @param mixed $expression The variable tobe formatted. |
|
385
|
|
|
* @param bool $export The variable export flag, to export the variable into parsable string representation. |
|
386
|
|
|
*/ |
|
387
|
|
|
function debug_line($expression, $export = false) |
|
388
|
|
|
{ |
|
389
|
|
|
\O2System\Gear\Debugger::line($expression, $export); |
|
390
|
|
|
} |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
// ------------------------------------------------------------------------ |
|
394
|
|
|
|
|
395
|
|
|
if ( ! function_exists('debug_marker')) { |
|
396
|
|
|
/** |
|
397
|
|
|
* debug_marker |
|
398
|
|
|
* |
|
399
|
|
|
* Add a debug marker into debugger stacks. |
|
400
|
|
|
*/ |
|
401
|
|
|
function debug_marker() |
|
402
|
|
|
{ |
|
403
|
|
|
\O2System\Gear\Debugger::marker(); |
|
404
|
|
|
} |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
// ------------------------------------------------------------------------ |
|
408
|
|
|
|
|
409
|
|
|
if ( ! function_exists('debug_stop')) { |
|
410
|
|
|
/** |
|
411
|
|
|
* debug_stop |
|
412
|
|
|
* |
|
413
|
|
|
* Stop the script execution and prints the debugger output into browser. |
|
414
|
|
|
*/ |
|
415
|
|
|
function debug_stop() |
|
416
|
|
|
{ |
|
417
|
|
|
\O2System\Gear\Debugger::stop(); |
|
418
|
|
|
die; |
|
|
|
|
|
|
419
|
|
|
} |
|
420
|
|
|
} |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.