From 145d78edcadbb6750167c22d9fc82760e3fbabab Mon Sep 17 00:00:00 2001 From: Cam Spiers Date: Thu, 11 Jul 2013 13:58:45 +1200 Subject: [PATCH] Cache templates created from string SSViewer_FromString --- tests/view/SSViewerTest.php | 33 ++++++++++++++++- view/SSViewer.php | 74 +++++++++++++++++++++++++++++++------ 2 files changed, 93 insertions(+), 14 deletions(-) diff --git a/tests/view/SSViewerTest.php b/tests/view/SSViewerTest.php index f79704918..2bb045f36 100644 --- a/tests/view/SSViewerTest.php +++ b/tests/view/SSViewerTest.php @@ -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); + } } /** diff --git a/view/SSViewer.php b/view/SSViewer.php index f32bcf414..4dd6c7b52 100644 --- a/view/SSViewer.php +++ b/view/SSViewer.php @@ -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; + } }