-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphpconsole.php
More file actions
128 lines (109 loc) · 3.35 KB
/
phpconsole.php
File metadata and controls
128 lines (109 loc) · 3.35 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
class PHPConsole {
function _construct() {
}
function console($contents, $type = 'log', $identifier = null,$line = false) {
/** Get the back trace into a json string and get the line number and the file name */
$bt = debug_backtrace();
$caller = array_shift($bt);
$str = json_encode($caller);
$myObject = json_decode($str);
$lineNo = $myObject->line;
$fileInfo = pathinfo($myObject->file);
$file = $fileInfo['basename'] ;
// convert the type of log to lowercase
strtolower($type);
//if the contents is an array then we encode into a json string and log it to print as an array. It can be an indexed array.
// or an associative array
if(is_array($contents)) {
$array = json_encode($contents);
echo "<script type='text/javascript'>\n";
if($identifier == null) {
if($line == false ) {
echo "console.log('Array : ' );\n";
} else {
echo "console.log('Array : (file:" . $file . ", line: " . $lineNo . ")' );\n";
}
}
else {
if($line == false) {
echo "console.log('Array : $identifier ') ;\n";
}
else {
echo "console.log('Array : $identifier (file:" . $file . ", line: " . $lineNo . ")' ) ;\n";
}
}
echo "console.dir($array);\n";
echo "</script>\n";
}
elseif (is_object($contents)) { // If the content is an object then accordingly the object is printed.
$array = json_encode($contents);
echo "<script type='text/javascript'>\n";
if($identifier == null) {
if($line == false ) {
echo "console.log('Object : ' );\n";
} else {
echo "console.log('Object : (file:" . $file . ", line: " . $lineNo . ")' );\n";
}
}
else {
if($line == false) {
echo "console.log('Object : $identifier ') ;\n";
}
else {
echo "console.log('Object : $identifier (file:" . $file . ", line: " . $lineNo . ")' ) ;\n";
}
}
echo "console.dir($array);\n";
echo "</script>\n";
}
else { // This is for the general variables
$prepender = "";
$data = "";
if($identifier == null) {
$prepender = "";
}
else {
$prepender = "Variable $identifier : ";
}
if($line == true) {
$data = $prepender . $contents . " (file:" . $file . ", line: " . $lineNo . ")";
}
else {
$data = $prepender.$contents;
}
echo "<script type='text/javascript'>\n";
switch($type) {
case "log":
echo "console.log('$data');\n";
break;
case "warning":
echo "console.warn('$data');\n";
break;
case "error":
echo "console.error('$data');\n";
break;
default: echo "console.log('$data');\n";
break;
}
echo "</script>\n";
}
}
// to clear the console .
function clear($line= false) {
/** Get the back trace into a json string and get the line number and the file name */
$bt = debug_backtrace();
$caller = array_shift($bt);
$str = json_encode($caller);
$myObject = json_decode($str);
$lineNo = $myObject->line;
$fileInfo = pathinfo($myObject->file);
$file = $fileInfo['basename'] ;
echo " <script type='text/javascript'>\n
console.clear(); ";
if($line == true) {
echo "console.log('Console cleared. (file: $file , line: $lineNo');";
}
echo "</script>\n";
}
}