|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arcanedev\LogViewer\Helpers; |
|
6
|
|
|
|
|
7
|
|
|
use Arcanedev\LogViewer\Utilities\LogLevels; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class LogParser |
|
12
|
|
|
* |
|
13
|
|
|
* @package Arcanedev\LogViewer\Helpers |
|
14
|
|
|
* @author ARCANEDEV <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class LogParser |
|
17
|
|
|
{ |
|
18
|
|
|
/* ----------------------------------------------------------------- |
|
19
|
|
|
| Constants |
|
20
|
|
|
| ----------------------------------------------------------------- |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
const REGEX_DATE_PATTERN = '\d{4}(-\d{2}){2}'; |
|
24
|
|
|
const REGEX_TIME_PATTERN = '\d{2}(:\d{2}){2}'; |
|
25
|
|
|
const REGEX_DATETIME_PATTERN = self::REGEX_DATE_PATTERN.' '.self::REGEX_TIME_PATTERN; |
|
26
|
|
|
|
|
27
|
|
|
/* ----------------------------------------------------------------- |
|
28
|
|
|
| Properties |
|
29
|
|
|
| ----------------------------------------------------------------- |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Parsed data. |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected static $parsed = []; |
|
38
|
|
|
|
|
39
|
|
|
/* ----------------------------------------------------------------- |
|
40
|
|
|
| Main Methods |
|
41
|
|
|
| ----------------------------------------------------------------- |
|
42
|
|
|
*/ |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Parse file content. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $raw |
|
48
|
|
|
* |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
378 |
|
public static function parse($raw) |
|
52
|
|
|
{ |
|
53
|
378 |
|
static::$parsed = []; |
|
54
|
378 |
|
list($headings, $data) = static::parseRawData($raw); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
// @codeCoverageIgnoreStart |
|
57
|
|
|
if ( ! is_array($headings)) { |
|
58
|
|
|
return static::$parsed; |
|
59
|
|
|
} |
|
60
|
|
|
// @codeCoverageIgnoreEnd |
|
61
|
|
|
|
|
62
|
378 |
|
foreach ($headings as $heading) { |
|
63
|
378 |
|
for ($i = 0, $j = count($heading); $i < $j; $i++) { |
|
64
|
378 |
|
static::populateEntries($heading, $data, $i); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
}; |
|
67
|
|
|
|
|
68
|
378 |
|
unset($headings, $data); |
|
69
|
|
|
|
|
70
|
378 |
|
return array_reverse(static::$parsed); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/* ----------------------------------------------------------------- |
|
74
|
|
|
| Other Methods |
|
75
|
|
|
| ----------------------------------------------------------------- |
|
76
|
|
|
*/ |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Extract the date. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $string |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
390 |
|
public static function extractDate(string $string): string |
|
86
|
|
|
{ |
|
87
|
390 |
|
return preg_replace('/.*('.self::REGEX_DATE_PATTERN.').*/', '$1', $string); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Parse raw data. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $raw |
|
94
|
|
|
* |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
378 |
|
private static function parseRawData($raw) |
|
98
|
|
|
{ |
|
99
|
378 |
|
$pattern = '/\['.self::REGEX_DATETIME_PATTERN.'\].*/'; |
|
100
|
378 |
|
preg_match_all($pattern, $raw, $headings); |
|
101
|
378 |
|
$data = preg_split($pattern, $raw); |
|
102
|
|
|
|
|
103
|
378 |
|
if ($data[0] < 1) { |
|
104
|
378 |
|
$trash = array_shift($data); |
|
105
|
378 |
|
unset($trash); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
378 |
|
return [$headings, $data]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Populate entries. |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $heading |
|
115
|
|
|
* @param array $data |
|
116
|
|
|
* @param int $key |
|
117
|
|
|
*/ |
|
118
|
378 |
|
private static function populateEntries($heading, $data, $key) |
|
119
|
|
|
{ |
|
120
|
378 |
|
foreach (LogLevels::all() as $level) { |
|
121
|
378 |
|
if (static::hasLogLevel($heading[$key], $level)) { |
|
|
|
|
|
|
122
|
378 |
|
static::$parsed[] = [ |
|
123
|
378 |
|
'level' => $level, |
|
124
|
378 |
|
'header' => $heading[$key], |
|
125
|
378 |
|
'stack' => $data[$key] |
|
126
|
|
|
]; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
378 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Check if header has a log level. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $heading |
|
135
|
|
|
* @param string $level |
|
136
|
|
|
* |
|
137
|
|
|
* @return bool |
|
138
|
|
|
*/ |
|
139
|
378 |
|
private static function hasLogLevel($heading, $level) |
|
140
|
|
|
{ |
|
141
|
378 |
|
return Str::contains($heading, strtoupper(".{$level}:")); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: