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.
This commit is contained in:
Amit Snyderman
2013-09-05 18:56:43 -04:00
parent 71eaed2778
commit 94cd72bfd4
5 changed files with 59 additions and 45 deletions
+2 -2
View File
@@ -2,6 +2,6 @@
interface Mustache_Cache interface Mustache_Cache
{ {
public function get($key); public function load($key);
public function put($key, $value); public function cache($key, $value);
} }
+39 -25
View File
@@ -11,41 +11,55 @@ class Mustache_Cache_FilesystemCache implements Mustache_Cache
$this->fileMode = $fileMode; $this->fileMode = $fileMode;
} }
public function get($key) public function load($key)
{ {
$fileName = $this->getCacheFilename($key); $fileName = $this->getCacheFilename($key);
return (is_file($fileName)) if (!is_file($fileName)) {
? file_get_contents($fileName) return false;
: null;
} }
public function put($key, $value) require_once $fileName;
return true;
}
public function cache($key, $value)
{ {
$fileName = $this->getCacheFilename($key); $fileName = $this->getCacheFilename($key);
$dirName = dirname($fileName); $this->writeFile($fileName, $value);
if (!is_dir($dirName)) { $this->load($key);
@mkdir($dirName, 0777, true);
if (!is_dir($dirName)) {
throw new Mustache_Exception_RuntimeException(sprintf('Failed to create cache directory "%s".', $dirName));
}
}
$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;
}
}
throw new Mustache_Exception_RuntimeException(sprintf('Failed to write cache file "%s".', $fileName));
} }
protected function getCacheFilename($name) protected function getCacheFilename($name)
{ {
return sprintf('%s/%s.php', $this->directory, md5($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 $fileName;
}
}
throw new Mustache_Exception_RuntimeException(sprintf('Failed to write cache file "%s".', $fileName));
}
} }
+9 -2
View File
@@ -2,6 +2,13 @@
class Mustache_Cache_NoopCache implements Mustache_Cache class Mustache_Cache_NoopCache implements Mustache_Cache
{ {
public function get($key) { return null; } public function load($key)
public function put($key, $value) {} {
return false;
}
public function cache($key, $compiled)
{
eval("?>".$compiled);
}
} }
+3 -10
View File
@@ -618,17 +618,10 @@ class Mustache_Engine
if (!isset($this->templates[$className])) { if (!isset($this->templates[$className])) {
if (!class_exists($className, false)) { if (!class_exists($className, false)) {
$cached = $this->getCache()->get($source); if (!$this->getCache()->load($source)) {
if (!$cached) { $compiled = $this->compile($source);
$this->log( $this->getCache()->cache($source, $compiled);
Mustache_Logger::DEBUG,
'Writing "{className}" class to template cache',
array('className' => $className)
);
$cached = $this->compile($source);
$this->getCache()->put($source, $cached);
} }
eval('?>'.$cached);
} }
$this->log( $this->log(
@@ -19,20 +19,20 @@ class Mustache_Test_Cache_FilesystemCacheTest extends PHPUnit_Framework_TestCase
{ {
$key = 'some key'; $key = 'some key';
$cache = new Mustache_Cache_FilesystemCache(self::$tempDir);; $cache = new Mustache_Cache_FilesystemCache(self::$tempDir);;
$cached = $cache->get($key); $loaded = $cache->load($key);
$this->assertNull($cached); $this->assertFalse($loaded);
} }
public function testCachePut() public function testCachePut()
{ {
$key = 'some key'; $key = 'some key';
$value = 'some value'; $value = '<?php /* some value */';
$cache = new Mustache_Cache_FilesystemCache(self::$tempDir);; $cache = new Mustache_Cache_FilesystemCache(self::$tempDir);;
$cache->put($key, $value); $cache->cache($key, $value);
$cached = $cache->get($key); $loaded = $cache->load($key);
$this->assertEquals($cached, $value); $this->assertTrue($loaded);
} }
private static function rmdir($path) private static function rmdir($path)