1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Mantas Varatiejus <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace MVar\LogParser; |
11
|
|
|
|
12
|
|
|
use MVar\LogParser\Exception\MatchException; |
13
|
|
|
use MVar\LogParser\Exception\ParserException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* This is the class that helps to iterate through log file. |
17
|
|
|
*/ |
18
|
|
|
class LogIterator implements \Iterator |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var LineParserInterface |
22
|
|
|
*/ |
23
|
|
|
private $parser; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $logFile; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var resource |
32
|
|
|
*/ |
33
|
|
|
private $fileHandler; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $currentLine; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
private $skipEmptyLines; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor. |
47
|
|
|
* |
48
|
|
|
* @param string $logFile |
49
|
|
|
* @param LineParserInterface $parser |
50
|
|
|
* @param bool $skipEmptyLines |
51
|
|
|
*/ |
52
|
5 |
|
public function __construct($logFile, $parser, $skipEmptyLines = true) |
53
|
|
|
{ |
54
|
5 |
|
$this->logFile = $logFile; |
55
|
5 |
|
$this->parser = $parser; |
56
|
5 |
|
$this->skipEmptyLines = $skipEmptyLines; |
57
|
5 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Destructor. |
61
|
|
|
*/ |
62
|
5 |
|
public function __destruct() |
63
|
|
|
{ |
64
|
5 |
|
@fclose($this->fileHandler); |
|
|
|
|
65
|
5 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns file handler. |
69
|
|
|
* |
70
|
|
|
* @return resource |
71
|
|
|
* @throws ParserException |
72
|
|
|
*/ |
73
|
5 |
|
protected function getFileHandler() |
74
|
|
|
{ |
75
|
5 |
|
if ($this->fileHandler === null) { |
76
|
5 |
|
$fileHandler = @fopen($this->logFile, 'r'); |
77
|
|
|
|
78
|
5 |
|
if ($fileHandler === false) { |
79
|
1 |
|
throw new ParserException('Can not open log file.'); |
80
|
|
|
} |
81
|
|
|
|
82
|
4 |
|
$this->fileHandler = $fileHandler; |
83
|
4 |
|
} |
84
|
|
|
|
85
|
4 |
|
return $this->fileHandler; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Reads single line from file. |
90
|
|
|
* |
91
|
|
|
* @throws ParserException |
92
|
|
|
*/ |
93
|
4 |
|
protected function readLine() |
94
|
|
|
{ |
95
|
4 |
|
$buffer = ''; |
96
|
|
|
|
97
|
4 |
|
while ($buffer === '') { |
98
|
4 |
|
if (($buffer = fgets($this->getFileHandler())) === false) { |
99
|
4 |
|
$this->currentLine = null; |
100
|
|
|
|
101
|
4 |
|
return; |
102
|
|
|
} |
103
|
4 |
|
$buffer = trim($buffer, "\n\r\0"); |
104
|
|
|
|
105
|
4 |
|
if (!$this->skipEmptyLines) { |
106
|
1 |
|
break; |
107
|
|
|
} |
108
|
3 |
|
} |
109
|
|
|
|
110
|
4 |
|
$this->currentLine = $buffer; |
111
|
4 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns parsed current line. |
115
|
|
|
* |
116
|
|
|
* @return array|null |
117
|
|
|
*/ |
118
|
4 |
|
public function current() |
119
|
|
|
{ |
120
|
4 |
|
if ($this->currentLine === null) { |
121
|
4 |
|
$this->readLine(); |
122
|
4 |
|
} |
123
|
|
|
|
124
|
|
|
try { |
125
|
4 |
|
$data = $this->parser->parseLine($this->currentLine); |
126
|
4 |
|
} catch (MatchException $exception) { |
127
|
1 |
|
$data = null; |
128
|
|
|
} |
129
|
|
|
|
130
|
4 |
|
return $data; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
4 |
|
public function next() |
137
|
|
|
{ |
138
|
4 |
|
$this->readLine(); |
139
|
4 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns current line |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
2 |
|
public function key() |
147
|
|
|
{ |
148
|
2 |
|
return $this->currentLine; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
4 |
|
public function valid() |
155
|
|
|
{ |
156
|
4 |
|
return !feof($this->getFileHandler()) || $this->currentLine; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
5 |
|
public function rewind() |
163
|
|
|
{ |
164
|
5 |
|
rewind($this->getFileHandler()); |
165
|
4 |
|
} |
166
|
|
|
} |
167
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: