forked from peoplepath/homework-modern-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogCounter.php
More file actions
155 lines (134 loc) · 3.83 KB
/
LogCounter.php
File metadata and controls
155 lines (134 loc) · 3.83 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
<?php
const LEVEL_PATTERN = '/test\.(\w+)/';
class LogCounter
{
private string $filename; // log filename
private ?SplFileObject $file; // file handler
private array $userFilters; // array of UserFilters
private array $stats; // results statistics array
private array $watchers; // list of callback functions
/**
* @param string $filename filename of the log
*/
public function __construct(string $filename)
{
$this->filename = $filename;
$this->userFilters = [];
$this->stats = [];
$this->watchers = [];
}
/**
* Main cycle for reading file
* @throws Exception on file not opening or if lines are too long
*/
public function readFile(): array
{
// open file
$this->openFile();
// read line
while (($line = $this->getLine()) !== false) {
// skip empty lines
if (strlen($line)<=0) continue;
// apply filters
$line = $this->applyFilters($line);
if ($line !== false) { // line is not marked as ignored
// add to statistics
$this->categorize($line);
}
// run callbacks
$this->updateWatchers();
}
// close the file
$this->file = null;
// return final count
return $this->stats;
}
/**
* Opens file of throws exception if file does not exist or is not readable
* @throws Exception
*/
public function openFile()
{
if (!$this->filename) {
throw new Exception("Filename not set.");
}
if (!is_readable($this->filename)) {
throw new Exception("File cannot be read.");
}
$this->file = new SplFileObject($this->filename);
}
/**
* @return bool|string false for end of file or next file line
*/
public function getLine(): bool|string
{
if (!$this->file->eof()) {
return $this->file->fgets();
} else {
return false;
}
}
/**
* @param $line String to be evaluated
* @return bool|string false if line should be ignored otherwise modified string based on user filters
*/
public function applyFilters(string $line): bool|string
{
/** @var UserFilter $userFilter */
foreach ($this->userFilters as $userFilter) {
if ($userFilter->testIgnorePattern($line)) {
return false;
} else {
$line = $userFilter->performReplace($line);
}
}
return $line;
}
/**
* Determines the correct category and increases its counter
* @param string $line String to be categorized
*/
private function categorize(string $line)
{
if (preg_match(LEVEL_PATTERN, $line, $matches)) {
$level = strtolower($matches[1]);
} else {
$level = "Unknown";
}
if (array_key_exists($level, $this->stats)) {
$this->stats[$level]++;
} else {
$this->stats[$level] = 1;
}
}
/**
* Runs all current callback functions with up-to-date data
*/
private function updateWatchers()
{
foreach ($this->watchers as $watcher) {
call_user_func($watcher, $this->stats);
}
}
/**
* @param String $watcher
*/
public function setWatcher(string $watcher): void
{
array_push($this->watchers, $watcher);
}
/**
* @param String $watcher
*/
public function unsetWatcher(string $watcher): void
{
unset($this->watchers[$watcher]);
}
/**
* @param UserFilter $userFilter
*/
public function addUserFilter(UserFilter $userFilter): void
{
array_push($this->userFilters, $userFilter);
}
}