filePath = $filePath; $this->fileHandle = null; } /** * @throws \Exception */ protected function openFile() { if ($this->fileHandle !== null) { return; } if (file_exists($this->filePath) === false) { throw new \Exception('Parser: Input File does not exists: "' . htmlspecialchars($this->filePath) . '"'); } if (is_readable($this->filePath) === false) { throw new \Exception('Parser: File is not readable: "' . htmlspecialchars($this->filePath) . '"'); } $this->fileHandle = @fopen($this->filePath, 'rb'); if ($this->fileHandle===false) { throw new \Exception('Parser: Could not open file: "' . htmlspecialchars($this->filePath) . '"'); } } /** * @return bool|string * @throws \Exception */ public function getNextLine() { $this->openFile(); return fgets($this->fileHandle); } /** * @return bool * @throws \Exception */ public function ended() { $this->openFile(); return feof($this->fileHandle); } public function close() { if ($this->fileHandle === null) { return true; } return @fclose($this->fileHandle); } /** * @param $output * @param $filePath * * @return bool * @throws \Exception */ public function save($output) { $result = file_put_contents($this->filePath, $output); if ($result === false) { throw new \Exception('Could not write into file '.htmlspecialchars($this->filePath)); } return true; } }