Kaynağa Gözat

Restore original eval vs. require class loading

Pushing the responsibility of class loading onto the `Mustache_Cache`
implementation and changing the semantics of get/put to load/cache.
Amit Snyderman 12 yıl önce
ebeveyn
işleme
94cd72bfd4

+ 2 - 2
src/Mustache/Cache.php

@@ -2,6 +2,6 @@
 
 interface Mustache_Cache
 {
-    public function get($key);
-    public function put($key, $value);
+    public function load($key);
+    public function cache($key, $value);
 }

+ 26 - 12
src/Mustache/Cache/FilesystemCache.php

@@ -11,41 +11,55 @@ class Mustache_Cache_FilesystemCache implements Mustache_Cache
         $this->fileMode = $fileMode;
     }
 
-    public function get($key)
+    public function load($key)
     {
         $fileName = $this->getCacheFilename($key);
-        return (is_file($fileName))
-            ? file_get_contents($fileName)
-            : null;
+        if (!is_file($fileName)) {
+            return false;
+        }
+
+        require_once $fileName;
+
+        return true;
     }
 
-    public function put($key, $value)
+    public function cache($key, $value)
     {
         $fileName = $this->getCacheFilename($key);
+        $this->writeFile($fileName, $value);
+        $this->load($key);
+    }
+
+    protected function getCacheFilename($name)
+    {
+        return sprintf('%s/%s.php', $this->directory, md5($name));
+    }
+
+    private function buildDirectoryForFilename($fileName)
+    {
         $dirName = dirname($fileName);
         if (!is_dir($dirName)) {
             @mkdir($dirName, 0777, true);
             if (!is_dir($dirName)) {
                 throw new Mustache_Exception_RuntimeException(sprintf('Failed to create cache directory "%s".', $dirName));
             }
-
         }
+        return $dirName;
+    }
 
+    private function writeFile($fileName, $value)
+    {
+        $dirName = $this->buildDirectoryForFilename($fileName);
         $tempFile = tempnam($dirName, basename($fileName));
         if (false !== @file_put_contents($tempFile, $value)) {
             if (@rename($tempFile, $fileName)) {
                 $mode = isset($this->fileMode) ? $this->fileMode : (0666 & ~umask());
                 @chmod($fileName, $mode);
 
-                return;
+                return $fileName;
             }
         }
 
         throw new Mustache_Exception_RuntimeException(sprintf('Failed to write cache file "%s".', $fileName));
     }
-
-    protected function getCacheFilename($name)
-    {
-        return sprintf('%s/%s.php', $this->directory, md5($name));
-    }
 }

+ 9 - 2
src/Mustache/Cache/NoopCache.php

@@ -2,6 +2,13 @@
 
 class Mustache_Cache_NoopCache implements Mustache_Cache
 {
-    public function get($key) { return null; }
-    public function put($key, $value) {}
+    public function load($key)
+    {
+        return false;
+    }
+
+    public function cache($key, $compiled)
+    {
+        eval("?>".$compiled);
+    }
 }

+ 3 - 10
src/Mustache/Engine.php

@@ -618,17 +618,10 @@ class Mustache_Engine
 
         if (!isset($this->templates[$className])) {
             if (!class_exists($className, false)) {
-                $cached = $this->getCache()->get($source);
-                if (!$cached) {
-                    $this->log(
-                        Mustache_Logger::DEBUG,
-                        'Writing "{className}" class to template cache',
-                        array('className' => $className)
-                    );
-                    $cached = $this->compile($source);
-                    $this->getCache()->put($source, $cached);
+                if (!$this->getCache()->load($source)) {
+                    $compiled = $this->compile($source);
+                    $this->getCache()->cache($source, $compiled);
                 }
-                eval('?>'.$cached);
             }
 
             $this->log(

+ 6 - 6
test/Mustache/Test/Cache/FilesystemCacheTest.php

@@ -19,20 +19,20 @@ class Mustache_Test_Cache_FilesystemCacheTest extends PHPUnit_Framework_TestCase
     {
         $key = 'some key';
         $cache = new Mustache_Cache_FilesystemCache(self::$tempDir);;
-        $cached = $cache->get($key);
+        $loaded = $cache->load($key);
 
-        $this->assertNull($cached);
+        $this->assertFalse($loaded);
     }
 
     public function testCachePut()
     {
         $key = 'some key';
-        $value = 'some value';
+        $value = '<?php /* some value */';
         $cache = new Mustache_Cache_FilesystemCache(self::$tempDir);;
-        $cache->put($key, $value);
-        $cached = $cache->get($key);
+        $cache->cache($key, $value);
+        $loaded = $cache->load($key);
 
-        $this->assertEquals($cached, $value);
+        $this->assertTrue($loaded);
     }
 
     private static function rmdir($path)