Permalink
Browse files

Mitigate dependency upon ConfigCache

  • Loading branch information...
1 parent 1b01f1e commit 51425845bc7f22eb684102f00f9c00dc6a28e0b0 @mpdude mpdude committed with fabpot Apr 21, 2013
Showing with 33 additions and 0 deletions.
  1. +5 −0 CHANGELOG.md
  2. +28 −0 Filesystem.php
View
@@ -1,6 +1,11 @@
CHANGELOG
=========
+2.3.0
+-----
+
+ * added the dumpFile() method to atomically write files
+
2.2.0
-----
View
@@ -430,4 +430,32 @@ private function toIterator($files)
return $files;
}
+
+ /**
+ * Atomically dumps content into a file.
+ *
+ * @param string $filename The file to be written to.
+ * @param string $content The data to write into the file.
+ * @param integer $mode The file mode (octal).
+ * @throws IOException If the file cannot be written to.
+ */
+ public function dumpFile($filename, $content, $mode = 0666)
+ {
+ $dir = dirname($filename);
+
+ if (!is_dir($dir)) {
+ $this->mkdir($dir);
+ } elseif (!is_writable($dir)) {
+ throw new IOException(sprintf('Unable to write in the %s directory\n', $dir));
+ }
+
+ $tmpFile = tempnam($dir, basename($filename));
+
+ if (false === @file_put_contents($tmpFile, $content)) {
+ throw new IOException(sprintf('Failed to write file "%s".', $filename));
+ }
+
+ $this->rename($tmpFile, $filename);
+ $this->chmod($filename, $mode);
+ }
}

0 comments on commit 5142584

Please sign in to comment.