Merge pull request #2957 from camspiers/ssviewer-from-string-cache

Cache templates created from string SSViewer_FromString
This commit is contained in:
Simon Welsh 2014-03-16 13:23:31 +13:00
commit 7d24f0ed40
2 changed files with 93 additions and 14 deletions

View File

@ -4,6 +4,7 @@ class SSViewerTest extends SapphireTest {
public function setUp() {
parent::setUp();
Config::inst()->update('SSViewer', 'source_file_comments', false);
Config::inst()->update('SSViewer_FromString', 'cache_template', false);
}
/**
@ -84,8 +85,8 @@ class SSViewerTest extends SapphireTest {
/**
* Small helper to render templates from strings
*/
public function render($templateString, $data = null) {
$t = SSViewer::fromString($templateString);
public function render($templateString, $data = null, $cacheTemplate = false) {
$t = SSViewer::fromString($templateString, $cacheTemplate);
if(!$data) $data = new SSViewerTestFixture();
return $t->process($data);
}
@ -1340,6 +1341,34 @@ after')
$this->assertEquals(1, $count);
}
/**
* Tests if caching for SSViewer_FromString is working
*/
public function testFromStringCaching() {
$content = 'Test content';
$cacheFile = TEMP_FOLDER . '/.cache.' . sha1($content);
if (file_exists($cacheFile)) {
unlink($cacheFile);
}
// Test global behaviors
$this->render($content, null, null);
$this->assertFalse(file_exists($cacheFile), 'Cache file was created when caching was off');
Config::inst()->update('SSViewer_FromString', 'cache_template', true);
$this->render($content, null, null);
$this->assertTrue(file_exists($cacheFile), 'Cache file wasn\'t created when it was meant to');
unlink($cacheFile);
// Test instance behaviors
$this->render($content, null, false);
$this->assertFalse(file_exists($cacheFile), 'Cache file was created when caching was off');
$this->render($content, null, true);
$this->assertTrue(file_exists($cacheFile), 'Cache file wasn\'t created when it was meant to');
unlink($cacheFile);
}
}
/**

View File

@ -633,11 +633,17 @@ class SSViewer {
/**
* Create a template from a string instead of a .ss file
*
*
* @param string $content The template content
* @param bool|void $cacheTemplate Whether or not to cache the template from string
* @return SSViewer
*/
public static function fromString($content) {
return new SSViewer_FromString($content);
public static function fromString($content, $cacheTemplate = null) {
$viewer = new SSViewer_FromString($content);
if ($cacheTemplate !== null) {
$viewer->setCacheTemplate($cacheTemplate);
}
return $viewer;
}
/**
@ -1147,13 +1153,31 @@ class SSViewer {
* @subpackage view
*/
class SSViewer_FromString extends SSViewer {
/**
* The global template caching behaviour if no instance override is specified
* @config
* @var bool
*/
private static $cache_template = true;
/**
* The template to use
* @var string
*/
protected $content;
/**
* Indicates whether templates should be cached
* @var bool
*/
protected $cacheTemplate;
public function __construct($content, TemplateParser $parser = null) {
$this->setParser($parser ?: Injector::inst()->get('SSTemplateParser'));
$this->setParser($parser ?: Injector::inst()->get('SSTemplateParser'));
$this->content = $content;
}
public function process($item, $arguments = null, $scope = null) {
if ($arguments && $arguments instanceof Zend_Cache_Core) {
Deprecation::notice('3.0', 'Use setPartialCacheStore to override the partial cache storage backend, ' .
@ -1162,16 +1186,42 @@ class SSViewer_FromString extends SSViewer {
$arguments = null;
}
$template = $this->parseTemplateContent($this->content, "string sha1=".sha1($this->content));
$hash = sha1($this->content);
$cacheFile = TEMP_FOLDER . "/.cache.$hash";
$tmpFile = tempnam(TEMP_FOLDER,"");
$fh = fopen($tmpFile, 'w');
fwrite($fh, $template);
fclose($fh);
if(!file_exists($cacheFile) || isset($_GET['flush'])) {
$content = $this->parseTemplateContent($this->content, "string sha1=$hash");
$fh = fopen($cacheFile,'w');
fwrite($fh, $content);
fclose($fh);
}
$val = $this->includeGeneratedTemplate($tmpFile, $item, $arguments, null, $scope);
$val = $this->includeGeneratedTemplate($cacheFile, $item, $arguments, null, $scope);
if ($this->cacheTemplate !== null) {
$cacheTemplate = $this->cacheTemplate;
} else {
$cacheTemplate = Config::inst()->get('SSViewer_FromString', 'cache_template');
}
if (!$cacheTemplate) {
unlink($cacheFile);
}
unlink($tmpFile);
return $val;
}
/**
* @param boolean $cacheTemplate
*/
public function setCacheTemplate($cacheTemplate) {
$this->cacheTemplate = (bool) $cacheTemplate;
}
/**
* @return boolean
*/
public function getCacheTemplate() {
return $this->cacheTemplate;
}
}