Skip to content

Commit b703b4f

Browse files
committed
saveCache() is atomic [Closes #11]
1 parent f8f87c9 commit b703b4f

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/RobotLoader/RobotLoader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,10 @@ private function loadCache()
410410
private function saveCache()
411411
{
412412
$file = $this->getCacheFile();
413+
$tempFile = $file . uniqid('', true) . '.tmp';
413414
$code = "<?php\nreturn " . var_export([$this->classes, $this->missing], true) . ";\n";
414-
if (file_put_contents("$file.tmp", $code) !== strlen($code) || !rename("$file.tmp", $file)) {
415-
@unlink("$file.tmp"); // @ - file may not exist
415+
if (file_put_contents($tempFile, $code) !== strlen($code) || !rename($tempFile, $file)) {
416+
@unlink($tempFile); // @ - file may not exist
416417
throw new \RuntimeException("Unable to create '$file'.");
417418
}
418419
if (function_exists('opcache_invalidate')) {

0 commit comments

Comments
 (0)