]> git.mxchange.org Git - friendica.git/blob - src/Model/Log/ParsedLogIterator.php
Add DI to ParsedLogIterator, replace constructors with fluent api
[friendica.git] / src / Model / Log / ParsedLogIterator.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2021, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model\Log;
23
24 use Friendica\Util\ReversedFileReader;
25 use Friendica\Object\Log\ParsedLog;
26
27 /**
28  * An iterator which returns `\Friendica\Objec\Log\ParsedLog` instances
29  *
30  * Uses `\Friendica\Util\ReversedFileReader` to fetch log lines
31  * from newest to oldest.
32  */
33 class ParsedLogIterator implements \Iterator
34 {
35         /** @var \Iterator */
36         private $reader;
37
38         /** @var ParsedLog current iterator value*/
39         private $value = null;
40
41         /** @var int max number of lines to read */
42         private $limit = 0;
43
44         /** @var array filters per column */
45         private $filters = [];
46
47         /** @var string search term */
48         private $search = "";
49
50
51         /**
52          * @param ReversedFileReader $reader
53          */
54         public function __construct(ReversedFileReader $reader)
55         {
56                 $this->reader = $reader;
57         }
58
59         /**
60          * @param string $filename      File to open
61          * @return $this
62          */
63         public function open(string $filename)
64         {
65                 $this->reader->open($filename);
66                 return $this;
67         }
68
69         /**
70          * @param int $limit            Max num of lines to read
71          * @return $this
72          */
73         public function withLimit(int $limit)
74         {
75                 $this->limit = $limit;
76                 return $this;
77         }
78
79         /**
80          * @param array $filters                filters per column
81          * @return $this
82          */
83         public function withFilters(array $filters)
84         {
85                 $this->filters = $filters;
86                 return $this;
87         }
88
89         /**
90          * @param string $search        string to search to filter lines
91          * @return $this
92          */
93         public function withSearch(string $search)
94         {
95                 $this->search = $search;
96                 return $this;
97         }
98
99         /**
100          * Check if parsed log line match filters.
101          * Always match if no filters are set.
102          *
103          * @param ParsedLog $parsedlog
104          * @return bool
105          */
106         private function filter($parsedlog)
107         {
108                 $match = true;
109                 foreach ($this->filters as $filter => $filtervalue) {
110                         switch ($filter) {
111                                 case "level":
112                                         $match = $match && ($parsedlog->level == strtoupper($filtervalue));
113                                         break;
114                                 case "context":
115                                         $match = $match && ($parsedlog->context == $filtervalue);
116                                         break;
117                         }
118                 }
119                 return $match;
120         }
121
122         /**
123          * Check if parsed log line match search.
124          * Always match if no search query is set.
125          *
126          * @param ParsedLog $parsedlog
127          * @return bool
128          */
129         private function search($parsedlog)
130         {
131                 if ($this->search != "") {
132                         return strstr($parsedlog->logline, $this->search) !== false;
133                 }
134                 return true;
135         }
136
137         /**
138          * Read a line from reader and parse.
139          * Returns null if limit is reached or the reader is invalid.
140          *
141          * @param ParsedLog $parsedlog
142          * @return ?ParsedLog
143          */
144         private function read()
145         {
146                 $this->reader->next();
147                 if ($this->limit > 0 && $this->reader->key() > $this->limit || !$this->reader->valid()) {
148                         return null;
149                 }
150
151                 $line = $this->reader->current();
152                 return new ParsedLog($this->reader->key(), $line);
153         }
154
155         public function next()
156         {
157                 $parsed = $this->read();
158
159                 // if read() has not retuned none and
160                 // the line don't match filters or search
161                 //      read the next line
162                 while (is_null($parsed) == false && !($this->filter($parsed) && $this->search($parsed))) {
163                         $parsed = $this->read();
164                 }
165                 $this->value = $parsed;
166         }
167
168         public function rewind()
169         {
170                 $this->value = null;
171                 $this->reader->rewind();
172                 $this->next();
173         }
174
175         public function key()
176         {
177                 return $this->reader->key();
178         }
179
180         public function current()
181         {
182                 return $this->value;
183         }
184
185         public function valid()
186         {
187                 return ! is_null($this->value);
188         }
189 }