. * */ namespace Friendica\Util; /** * An iterator which returns lines from file in reversed order * * original code https://stackoverflow.com/a/10494801 */ class ReversedFileReader implements \Iterator { const BUFFER_SIZE = 4096; const SEPARATOR = "\n"; /** @var resource */ private $fh = null; /** @var int */ private $filesize = -1; /** @var int */ private $pos = -1; /** @var array */ private $buffer = null; /** @var int */ private $key = -1; /** @var string */ private $value = null; /** * Open $filename for read and reset iterator * * @param string $filename File to open * @return $this */ public function open(string $filename) { $this->fh = fopen($filename, 'r'); if (!$this->fh) { // this should use a custom exception. throw \Exception("Unable to open $filename"); } $this->filesize = filesize($filename); $this->pos = -1; $this->buffer = null; $this->key = -1; $this->value = null; return $this; } private function _read($size) { $this->pos -= $size; fseek($this->fh, $this->pos); return fread($this->fh, $size); } private function _readline() { $buffer = & $this->buffer; while (true) { if ($this->pos == 0) { return array_pop($buffer); } if (count($buffer) > 1) { return array_pop($buffer); } $buffer = explode(self::SEPARATOR, $this->_read(self::BUFFER_SIZE) . $buffer[0]); } } public function next() { ++$this->key; $this->value = $this->_readline(); } public function rewind() { if ($this->filesize > 0) { $this->pos = $this->filesize; $this->value = null; $this->key = -1; $this->buffer = explode(self::SEPARATOR, $this->_read($this->filesize % self::BUFFER_SIZE ?: self::BUFFER_SIZE)); $this->next(); } } public function key() { return $this->key; } public function current() { return $this->value; } public function valid() { return ! is_null($this->value); } }