X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FLog%2FParsedLogIterator.php;h=d3a5650397d6bd14646b4fb2e5766a28928a8968;hb=e3aed8099c1605e1b15b70f74b94540f4512c829;hp=15714454e7ce667a9631c0c7e477824ae5ed98d2;hpb=5b9aeeeca95165203ed2753ba44ce6d8fbdea321;p=friendica.git diff --git a/src/Model/Log/ParsedLogIterator.php b/src/Model/Log/ParsedLogIterator.php index 15714454e7..d3a5650397 100644 --- a/src/Model/Log/ParsedLogIterator.php +++ b/src/Model/Log/ParsedLogIterator.php @@ -1,6 +1,6 @@ . * */ -namespace Friendica\Model\Log; -use \Friendica\Util\ReversedFileReader; -use \Friendica\Object\Log\ParsedLog; +namespace Friendica\Model\Log; +use Friendica\Util\ReversedFileReader; +use Friendica\Object\Log\ParsedLogLine; /** - * An iterator which returns `\Friendica\Objec\Log\ParsedLog` instances + * An iterator which returns `\Friendica\Objec\Log\ParsedLogLine` instances * * Uses `\Friendica\Util\ReversedFileReader` to fetch log lines * from newest to oldest. @@ -35,51 +35,84 @@ class ParsedLogIterator implements \Iterator /** @var \Iterator */ private $reader; - /** @var ParsedLog current iterator value*/ - private $value; + /** @var ParsedLogLine current iterator value*/ + private $value = null; /** @var int max number of lines to read */ - private $limit; + private $limit = 0; /** @var array filters per column */ - private $filters; + private $filters = []; /** @var string search term */ - private $search; + private $search = ""; + /** + * @param ReversedFileReader $reader + */ + public function __construct(ReversedFileReader $reader) + { + $this->reader = $reader; + } + /** * @param string $filename File to open + * @return $this + */ + public function open(string $filename): ParsedLogIterator + { + $this->reader->open($filename); + return $this; + } + + /** * @param int $limit Max num of lines to read - * @param array $filter filters per column - * @param string $search string to search to filter lines + * @return $this */ - public function __construct(string $filename, int $limit=0, array $filters=[], string $search="") + public function withLimit(int $limit): ParsedLogIterator { - $this->reader = new ReversedFileReader($filename); - $this->value = null; $this->limit = $limit; + return $this; + } + + /** + * @param array $filters filters per column + * @return $this + */ + public function withFilters(array $filters): ParsedLogIterator + { $this->filters = $filters; + return $this; + } + + /** + * @param string $search string to search to filter lines + * @return $this + */ + public function withSearch(string $search): ParsedLogIterator + { $this->search = $search; + return $this; } /** * Check if parsed log line match filters. * Always match if no filters are set. - * - * @param ParsedLog $parsedlog - * @return bool + * + * @param ParsedLogLine $parsedlogline ParsedLogLine instance + * @return bool Wether the parse log line matches */ - private function filter($parsedlog) + private function filter(ParsedLogLine $parsedlogline): bool { $match = true; foreach ($this->filters as $filter => $filtervalue) { - switch($filter) { + switch ($filter) { case "level": - $match = $match && ($parsedlog->level == strtoupper($filtervalue)); + $match = $match && ($parsedlogline->level == strtoupper($filtervalue)); break; case "context": - $match = $match && ($parsedlog->context == $filtervalue); + $match = $match && ($parsedlogline->context == $filtervalue); break; } } @@ -89,49 +122,61 @@ class ParsedLogIterator implements \Iterator /** * Check if parsed log line match search. * Always match if no search query is set. - * - * @param ParsedLog $parsedlog + * + * @param ParsedLogLine $parsedlogline * @return bool */ - private function search($parsedlog) + private function search(ParsedLogLine $parsedlogline): bool { if ($this->search != "") { - return strstr($parsedlog->logline, $this->search) !== false; + return strstr($parsedlogline->logline, $this->search) !== false; } - return True; + return true; } /** * Read a line from reader and parse. * Returns null if limit is reached or the reader is invalid. - * - * @param ParsedLog $parsedlog - * @return ?ParsedLog + * + * @param ParsedLogLine $parsedlogline + * @return ?ParsedLogLine */ private function read() { $this->reader->next(); - if ($this->limit > 0 && $this->reader->key() > $this->limit || !$this->reader->valid()) { + if ($this->limit > 0 && $this->reader->key() > $this->limit || !$this->reader->valid()) { return null; } $line = $this->reader->current(); - return new ParsedLog($this->reader->key(), $line); + return new ParsedLogLine($this->reader->key(), $line); } + + /** + * Fetch next parsed log line which match with filters or search and + * set it as current iterator value. + * + * @see Iterator::next() + * @return void + */ public function next() { $parsed = $this->read(); - // if read() has not retuned none and - // the line don't match filters or search - // read the next line - while(is_null($parsed) == false && !($this->filter($parsed) && $this->search($parsed))) { + while (is_null($parsed) == false && !($this->filter($parsed) && $this->search($parsed))) { $parsed = $this->read(); } $this->value = $parsed; } + + /** + * Rewind the iterator to the first matching log line + * + * @see Iterator::rewind() + * @return void + */ public function rewind() { $this->value = null; @@ -139,20 +184,37 @@ class ParsedLogIterator implements \Iterator $this->next(); } + /** + * Return current parsed log line number + * + * @see Iterator::key() + * @see ReversedFileReader::key() + * @return int + */ public function key() { return $this->reader->key(); } + /** + * Return current iterator value + * + * @see Iterator::current() + * @return ?ParsedLogLing + */ public function current() { return $this->value; } + /** + * Checks if current iterator value is valid, that is, not null + * + * @see Iterator::valid() + * @return bool + */ public function valid() { return ! is_null($this->value); } - } -