-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhpBrowserLog.php
50 lines (44 loc) · 1.6 KB
/
PhpBrowserLog.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
class PhpBrowserLog
{
public static function script($data, $command = 'console.log', $variable = false)
{
echo '<script>';
if ($variable === false) {
echo $command . '(' . json_encode($data) . ')';
} else {
echo $command . '("' . trim($variable) . '",' . json_encode($data) . ')';
}
echo '</script>';
}
public static function getVarsNames($arCaller)
{
$sFile = file_get_contents($arCaller['file']);
$arFile = explode("\n", $sFile);
$arArgsNames = explode('(', $arFile[$arCaller[line] - 1])[1];
$arArgsNames = explode(')', $arArgsNames)[0];
return explode(',', $arArgsNames);
}
public static function log()
{
$arBt = debug_backtrace();
$arCaller = array_shift($arBt);
$arArgsNames = self::getVarsNames($arCaller);
self::script('[' . $arCaller['line'] . '] ' . $arCaller['file'], 'console.groupCollapsed');
foreach ($arCaller['args'] as $nKey => $anyArg) {
self::script($anyArg, 'console.log', $arArgsNames[$nKey]);
}
self::script('', 'console.groupEnd');
}
public static function pre()
{
$arBt = debug_backtrace();
$arCaller = array_shift($arBt);
$arArgsNames = self::getVarsNames($arCaller);
self::script('[' . $arCaller['line'] . '] ' . $arCaller['file'], 'console.groupCollapsed');
foreach ($arCaller['args'] as $nKey => $anyArg) {
self::script($anyArg, 'console.log', $arArgsNames[$nKey]);
}
self::script('', 'console.groupEnd');
}
}