瀏覽代碼

simplified some code

Fabien Potencier 15 年之前
父節點
當前提交
b514d767ef

+ 4 - 13
src/Symfony/Components/Routing/Router.php

@@ -246,21 +246,12 @@ class Router implements RouterInterface
     protected function writeCacheFile($file, $content)
     {
         $tmpFile = tempnam(dirname($file), basename($file));
-        if (!$fp = @fopen($tmpFile, 'wb')) {
-            throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $tmpFile));
-        }
-
-        @fwrite($fp, $content);
-        @fclose($fp);
-
-        if ($content != file_get_contents($tmpFile)) {
-            throw new \RuntimeException(sprintf('Failed to write cache file "%s" (cache corrupted).', $tmpFile));
-        }
+        if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
+            chmod($file, 0644);
 
-        if (!@rename($tmpFile, $file)) {
-            throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
+            return;
         }
 
-        chmod($file, 0644);
+        throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
     }
 }

+ 4 - 12
src/Symfony/Framework/ClassCollectionLoader.php

@@ -94,20 +94,12 @@ class ClassCollectionLoader
     static protected function writeCacheFile($file, $content)
     {
         $tmpFile = tempnam(dirname($file), basename($file));
-        if (!$fp = @fopen($tmpFile, 'wb')) {
-            die(sprintf('Failed to write cache file "%s".', $tmpFile));
-        }
-        @fwrite($fp, $content);
-        @fclose($fp);
-
-        if ($content != file_get_contents($tmpFile)) {
-            die(sprintf('Failed to write cache file "%s" (cache corrupted).', $tmpFile));
-        }
+        if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
+            chmod($file, 0644);
 
-        if (!@rename($tmpFile, $file)) {
-            throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
+            return;
         }
 
-        chmod($file, 0644);
+        throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
     }
 }

+ 4 - 12
src/Symfony/Framework/Kernel.php

@@ -462,21 +462,13 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
     protected function writeCacheFile($file, $content)
     {
         $tmpFile = tempnam(dirname($file), basename($file));
-        if (!$fp = @fopen($tmpFile, 'wb')) {
-            die(sprintf('Failed to write cache file "%s".', $tmpFile));
-        }
-        @fwrite($fp, $content);
-        @fclose($fp);
-
-        if ($content != file_get_contents($tmpFile)) {
-            die(sprintf('Failed to write cache file "%s" (cache corrupted).', $tmpFile));
-        }
+        if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
+            chmod($file, 0644);
 
-        if (!@rename($tmpFile, $file)) {
-            throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
+            return;
         }
 
-        chmod($file, 0644);
+        throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
     }
 
     public function serialize()