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
{
public function get($key);
public function put($key, $value);
public function load($key);
public function cache($key, $value);
}
+39 -25
View File
@@ -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);
$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));
}
}
$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));
$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 $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
{
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
View File
@@ -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(
@@ -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)