1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\BlackBar\Modules; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\BlackBar\Application; |
6
|
|
|
use GeminiLabs\BlackBar\Dump; |
7
|
|
|
|
8
|
|
|
class Console extends Module |
9
|
|
|
{ |
10
|
|
|
public const ERROR_CODES = [ |
11
|
|
|
E_ERROR => 'error', // 1 |
12
|
|
|
E_WARNING => 'warning', // 2 |
13
|
|
|
E_NOTICE => 'notice', // 8 |
14
|
|
|
E_STRICT => 'strict', // 2048 |
15
|
|
|
E_DEPRECATED => 'deprecated', // 8192 |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
public const MAPPED_ERROR_CODES = [ |
19
|
|
|
'debug' => 0, |
20
|
|
|
'info' => E_NOTICE, |
21
|
|
|
'deprecated' => E_DEPRECATED, // 8192 |
22
|
|
|
'error' => E_ERROR, // 1 |
23
|
|
|
'notice' => E_NOTICE, // 8 |
24
|
|
|
'strict' => E_STRICT, // 2048 |
25
|
|
|
'warning' => E_WARNING, // 2 |
26
|
|
|
'critical' => E_ERROR, // 1 |
27
|
|
|
'alert' => E_ERROR, // 1 |
28
|
|
|
'emergency' => E_ERROR, // 1 |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
public function classes(): string |
32
|
|
|
{ |
33
|
|
|
$errno = array_unique(wp_list_pluck($this->entries, 'errno')); |
34
|
|
|
if (in_array(E_ERROR, $errno)) { |
35
|
|
|
return sprintf('%s glbb-error', $this->id()); |
36
|
|
|
} |
37
|
|
|
if (in_array(E_WARNING, $errno)) { |
38
|
|
|
return sprintf('%s glbb-warning', $this->id()); |
39
|
|
|
} |
40
|
|
|
return $this->id(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function entries(): array |
44
|
|
|
{ |
45
|
|
|
$entries = []; |
46
|
|
|
foreach ($this->entries as $entry) { |
47
|
|
|
$entry['name'] = ucfirst($entry['errname']); |
48
|
|
|
if ($entry['count'] > 1) { |
49
|
|
|
$entry['name'] = sprintf('%s (%s)', $entry['name'], $entry['count']); |
50
|
|
|
} |
51
|
|
|
$entries[] = $entry; |
52
|
|
|
} |
53
|
|
|
return $entries; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function info(): string |
57
|
|
|
{ |
58
|
|
|
$counts = array_count_values(wp_list_pluck($this->entries, 'errno')); |
59
|
|
|
$entryCount = count($this->entries); |
60
|
|
|
if (!empty($counts[E_ERROR])) { |
61
|
|
|
return sprintf('%d, %d!', $entryCount, $counts[E_ERROR]); |
62
|
|
|
} |
63
|
|
|
if ($entryCount > 0) { |
64
|
|
|
return (string) $entryCount; |
65
|
|
|
} |
66
|
|
|
return ''; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function label(): string |
70
|
|
|
{ |
71
|
|
|
return __('Console', 'blackbar'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function store(string $message, string $errno = '', string $location = ''): void |
75
|
|
|
{ |
76
|
|
|
if (is_numeric($errno)) { // entry likely stored by set_error_handler() |
77
|
|
|
$errname = 'Unknown'; |
78
|
|
|
if (array_key_exists((int) $errno, static::ERROR_CODES)) { |
79
|
|
|
$errname = static::ERROR_CODES[$errno]; |
80
|
|
|
} |
81
|
|
|
} else { // entry likely stored by filter hook |
82
|
|
|
$errname = 'Debug'; |
83
|
|
|
if (array_key_exists($errno, static::MAPPED_ERROR_CODES)) { |
84
|
|
|
$errname = $errno; |
85
|
|
|
$errno = static::MAPPED_ERROR_CODES[$errno]; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
$errname = strtolower($errname); |
89
|
|
|
$hash = md5($errno.$errname.$message.$location); |
90
|
|
|
if (array_key_exists($hash, $this->entries)) { |
91
|
|
|
++$this->entries[$hash]['count']; |
92
|
|
|
} else { |
93
|
|
|
$this->entries[$hash] = [ |
94
|
|
|
'count' => 0, |
95
|
|
|
'errname' => $errname, |
96
|
|
|
'errno' => (int) $errno, |
97
|
|
|
'message' => $this->normalizeMessage($message, $location), |
98
|
|
|
]; |
99
|
|
|
}; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function normalizeMessage($message, string $location): string |
103
|
|
|
{ |
104
|
|
|
if ($message instanceof \DateTime) { |
105
|
|
|
$message = $message->format('Y-m-d H:i:s'); |
106
|
|
|
} elseif (is_object($message) || is_array($message)) { |
107
|
|
|
$message = (new Dump())->dump($message); |
108
|
|
|
} else { |
109
|
|
|
$message = esc_html(trim((string) $message)); |
110
|
|
|
} |
111
|
|
|
$location = trim($location); |
112
|
|
|
if (!empty($location)) { |
113
|
|
|
$location = str_replace([WP_CONTENT_DIR, ABSPATH], '', $location); |
114
|
|
|
$location = sprintf('[%s]', $location); |
115
|
|
|
} |
116
|
|
|
return trim(sprintf('%s %s', $location, $message)); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|