. * */ namespace Friendica\Model\Log; use \Friendica\Util\ReversedFileReader; use \Friendica\Object\Log\ParsedLog; /** * An iterator which returns `\Friendica\Objec\Log\ParsedLog` instances * * Uses `\Friendica\Util\ReversedFileReader` to fetch log lines * from newest to oldest */ class ParsedLogIterator implements \Iterator { public function __construct(string $filename, int $limit=0) { $this->reader = new ReversedFileReader($filename); $this->_value = null; $this->_limit = $limit; } public function next() { $this->reader->next(); if ($this->_limit > 0 && $this->reader->key() > $this->_limit) { $this->_value = null; return; } if ($this->reader->valid()) { $line = $this->reader->current(); $this->_value = new ParsedLog($this->reader->key(), $line); } else { $this->_value = null; } } public function rewind() { $this->_value = null; $this->reader->rewind(); $this->next(); } public function key() { return $this->reader->key(); } public function current() { return $this->_value; } public function valid() { return ! is_null($this->_value); } }