-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleWriter.class.php
90 lines (82 loc) · 2.27 KB
/
ConsoleWriter.class.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
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
<?php
/**
* ConsoleWriter
* Author: John Vilsack
* Date: 3/2/11
* Time: 9:12 AM
*
* Simple PHP Class to generate javascript blocks, console groupings, and direct output to javascript developer tools.
*
*/
class ConsoleWriter {
public function __construct() {}
/**
* Open/Close Javascript Block
*
* @param string $type ('open'|'close') Determine type of output
* @access public
* @static
*/
public static function consoleBlock($type) {
if ($type === 'open') {
echo PHP_EOL . '<script type="text/javascript">' . PHP_EOL;
return;
} elseif ($type === 'close') {
echo '</script>' . PHP_EOL;
return;
} else {
echo 'ERROR IN CONSOLE START';
exit;
}
}
/**
* Manipulate Console Groupings
*
* @param string $name Name of the Group
* @param string $type ('open'|'end'|'collapsed') Determine type of output
* @access public
* @static
*/
public function consoleGroup($type='open', $name='') {
if ($type === 'open') {
echo 'console.group("'. $name .'");' . PHP_EOL;
return;
} elseif ($type === 'collapsed') {
echo 'console.groupCollapsed("'. $name .'");' . PHP_EOL;
} elseif ($type === 'end') {
echo 'console.groupEnd();' . PHP_EOL;
}
}
/**
* Output to Console when contained within Javascript block
*
* @param string $value Value to be written to the console
* @param string $type ('log'|'info'|'warn'|'error') Determine type of output (default = 'log')
* @param string $key Value of "key" of item being written
* @access public
* @static
*/
public static function consoleLine($value='', $type='log', $key='') {
if ($key !== '') {
echo 'console.' . $type . '("' . $key . '\t\t'. $value . '");' . PHP_EOL;
return;
} else {
echo 'console.' . $type . '("' . $value . '");' . PHP_EOL;
return;
}
}
/**
* Quickly build out a single output line. Great for breakpoints.
*
* @param string $value Value to be written to the console
* @param string $type ('log'|'info'|'warn'|'error') Determine type of output (default = 'log')
* @param string $key Value of "key" of item being written
* @access public
* @static
*/
public static function consoleQuickLine($value='', $type='log', $key='') {
ConsoleWriter::consoleBlock('open');
ConsoleWriter::consoleLine($value, $type, $key);
ConsoleWriter::consoleBlock('close');
}
}