Skip to content

Commit

Permalink
Mitigate dependency upon ConfigCache
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdude authored and fabpot committed Apr 21, 2013
1 parent 1b01f1e commit 5142584
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

2.3.0
-----

* added the dumpFile() method to atomically write files

2.2.0
-----

Expand Down
28 changes: 28 additions & 0 deletions Filesystem.php
Expand Up @@ -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.