-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsole_log.php
More file actions
210 lines (190 loc) · 7.72 KB
/
console_log.php
File metadata and controls
210 lines (190 loc) · 7.72 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* Dump PHP variables to the js console
* @param: (mixed)
*/
function console_log()
{
$argv = func_get_args();
$json = __ConsoleLog__::getFormattedJson($argv);
// add the js helper script if not already included
if (!__ConsoleLog__::$script_included) {
echo "<script>" . __ConsoleLog__::getJsPrettyPrintFunction() . "</script>";
__ConsoleLog__::$script_included = true;
}
__ConsoleLog__::callJsPrettyPrint($json);
}
// Wrapper class for console_log
class __ConsoleLog__
{
public static $script_included = false;
/**
* export a variable to a json string
* @param $argv mixed the parameters to format
* @return mixed|string
*/
public static function getFormattedJson($argv)
{
if (sizeof($argv) === 1 && is_scalar($argv[0])) {
$argv = $argv[0];
} else {
// Make sure keys are converted to strings to be later formatted in js
foreach ($argv as $k => $v) {
$argv[gettype($v)] = $v;
unset($argv[$k]);
}
// <HACK (lines 40 to 57): expose non public object properties and class names
$argv_str = var_export($argv, true);
// replace objects identifiers by their respective class names
$object_identifier = '/((\')?(\S*?)(\')?\s*?=>\s*?(\S*))::__set_state\(/';
$argv_str = preg_replace_callback(
$object_identifier,
function ($matches) {
$key = $matches[3];
$className = '(' . $matches[sizeof($matches) - 1] . ')';
return "'$key:$className' => ";
},
$argv_str
);
// Remove extra parentheses
$argv_str = preg_replace('/\)\)/', ')', $argv_str);
// evaluate the result string (reverse var_export)
eval('$argv =' . $argv_str . ';');
// HACK/>
}
// export to json
return json_encode($argv);
}
/**
* echo a script tag containing a call to the js formatter function on the provided json
* @param $json
*/
public static function callJsPrettyPrint($json)
{
// Call the js formatter on the json
echo ("<script>
(function() {
var jsonDump = JSON.parse('$json');
prettyPrintPHPJson(jsonDump);
})();
</script>");
}
/**
* echo the js formatter function
*/
public static function getJsPrettyPrintFunction()
{
echo "<script>
function prettyPrintPHPJson(jsonDump) {
var consoleArgs = [],
indentationStr = '| ',
defaultColors = [
'#820597',
'#3e49a8',
'#357FC9',
'#2D86AD',
'#35b0bc',
'#a3af67',
],
styles = {
property : {
base: 'font-weight: 600;',
colorsByIndentLevel: defaultColors
},
brace : {
base: 'color: #555555; font-weight: 600;',
colorsByIndentLevel: defaultColors
},
bracket : {
base: 'color: #ba576b; font-weight: 400;',
colorsByIndentLevel: defaultColors
},
nullValue : {
base: 'color: #aaa; font-weight: 600;',
colorsByIndentLevel: []
},
pipe: {
base: 'color: #00000022; font-size: 70%;font-weight: 100;',
colorsByIndentLevel: []
}
},
regexes = {
stylable: /\".+?\":|\{|\}|\[|\]|null|\|/gm,
breakAfter: /null,|\",|[0-9],|\],|\},|\{|\[/gm,
breakBefore: /\]|\}/gm,
argsKey: /(\s{2})\"([0-9]*)\"( →)/gm
};
function getStyle(type, indentLevel) {
var baseStyle = styles[type].base;
var computedStyle = '';
var color = styles[type].colorsByIndentLevel[indentLevel - 1];
if (color) {
computedStyle += 'color:' + color + ';'
}
return baseStyle + computedStyle;
}
function addStyles(match, offset, str, consoleArgs){
var replacement = '%c%s%c';
var lastChar = match[match.length - 1];
var indentLevel = getIndetationLevel(str.substr(0, offset));
var closingIndentation = indentLevel ? indentLevel - 1 : 0;
switch (lastChar) {
case ':':
var argKey = /\"(.*?)\":/i;
match = match.replace(argKey, indentLevel > 0 ? '$1 → ' : '$1: ');
match = match.replace('.', '');
style = getStyle('property', indentLevel);
break;
case '{':
style = getStyle('brace', indentLevel);
break;
case '}':
style = getStyle('brace', closingIndentation);
break;
case '[':
style = getStyle('bracket', indentLevel);
break;
case ']':
style = getStyle('bracket', closingIndentation);
break;
case '|':
style = getStyle('pipe');
break;
default:
style = getStyle('nullValue', indentLevel);
break;
}
consoleArgs.push(style, match, '');
return replacement;
}
function getIndetationLevel(str) {
var openingSymbolsCount = (str.match(/\{|\[/gm) || []).length;
var closingSymbolsCount = (str.match(/\}|\]/gm) || []).length;
return openingSymbolsCount - closingSymbolsCount;
}
function breakAndIndentBefore(match, offset, str, indentationStr) {
var indentLevel = getIndetationLevel(str.substr(0, offset + 1));
var indent = getIndent(indentLevel, indentationStr);
return '\\n' + indent + match;
}
function breakAndIndentAfter(match, offset, str, indentationStr) {
var indentLevel = getIndetationLevel(str.substr(0, offset + 1));
var indent = getIndent(indentLevel, indentationStr);
return match + '\\n' + indent;
}
function getIndent(level, str) {
return Array(level).fill(str).join('');
}
var jsonString = JSON.stringify(jsonDump).replace(/^\{|\}$/gm, '')
.replace(regexes.breakAfter, function(match, offset, str) { return breakAndIndentAfter(match, offset, str, indentationStr) })
.replace(regexes.breakBefore, function(match, offset, str) { return breakAndIndentBefore(match, offset, str, indentationStr)})
.replace(regexes.stylable, function(match, offset, str) { return addStyles(match, offset, str, consoleArgs) });
consoleArgs.unshift(jsonString);
console.group('PHP');
console.log.apply(this, consoleArgs);
console.groupEnd();
}
</script>";
}
}
?>