file = new SplFileObject($filename); $this->file->setFlags( SplFileObject::DROP_NEW_LINE | SplFileObject::READ_AHEAD ); $this->fields = $fields; } public function rewind(): void { $this->file->rewind(); $this->index = 0; $this->nextMessage(); } public function next(): void { $this->file->next(); ++$this->index; $this->nextMessage(); } public function current(): array { return $this->current; } public function key(): int { return $this->index; } public function valid(): bool { return $this->valid; } protected function nextMessage() { $message = $this->next === null ? array() : array($this->next); $this->valid = null; while ($this->file->valid()) { if (false === ($res = preg_match( $this->fields, $current = $this->file->current() ))) { throw new IcingaException('Failed at preg_match()'); } if (empty($message)) { if ($res === 1) { $message[] = $current; } } elseif ($res === 1) { $this->next = $current; $this->valid = true; break; } else { $message[] = $current; } $this->file->next(); } if ($this->valid === null) { $this->next = null; $this->valid = ! empty($message); } if ($this->valid) { while (! empty($message)) { $matches = array(); if (false === ($res = preg_match( $this->fields, implode(PHP_EOL, $message), $matches ))) { throw new IcingaException('Failed at preg_match()'); } if ($res === 1) { $this->current = $matches; return; } array_pop($message); } $this->valid = false; } } }