/** * Store all tmx and content that has been set. * This is performed as a transaction, reverting * changes already written if one write fails. * * @return bool Returns true on success, false on failure. */ public function store() { $result = true; $tmxBackup = $savedContent = array(); try { if (!empty($this->tmxContents)) { $this->verifyWriteAccess($this->tmxTarget); $tmxFile = new Util_TmxFile(); if (is_file($this->tmxTarget)) { $tmxFile->load($this->tmxTarget); // backup in case a write operation fails $tmxBackup = $tmxFile->toArray(); } $tmxFile->fromArray($this->tmxContents); $result = $tmxFile->save($this->tmxTarget); if (!$result) { throw new Setup_Model_Exception("Saving File '{$this->tmxTarget}' failed"); } } foreach ($this->contentSources as $filename => $contents) { if (!is_null($contents)) { $this->verifyWriteAccess($filename); // backup stored content to restore in case a write operation fails $savedContent[$filename] = file_get_contents($filename); $result = false !== file_put_contents($filename, $contents) && $result; if (!$result) { throw new Setup_Model_Exception("Saving File '{$filename}' failed"); } } } } catch (Setup_Model_Exception $se) { if (!empty($tmxBackup)) { $tmxFile = new Util_TmxFile(); $tmxFile->fromArray($tmxBackup); $tmxFile->save($this->tmxTarget); } if (!empty($savedContent)) { foreach ($savedContent as $filename => $contents) { file_put_contents($filename, $contents); } } $this->log($se->getMessage()); $result = false; } return $result; }