mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Cache templates created from string SSViewer_FromString
This commit is contained in:
parent
6b53368814
commit
145d78edca
@ -4,6 +4,7 @@ class SSViewerTest extends SapphireTest {
|
|||||||
public function setUp() {
|
public function setUp() {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
Config::inst()->update('SSViewer', 'source_file_comments', false);
|
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
|
* Small helper to render templates from strings
|
||||||
*/
|
*/
|
||||||
public function render($templateString, $data = null) {
|
public function render($templateString, $data = null, $cacheTemplate = false) {
|
||||||
$t = SSViewer::fromString($templateString);
|
$t = SSViewer::fromString($templateString, $cacheTemplate);
|
||||||
if(!$data) $data = new SSViewerTestFixture();
|
if(!$data) $data = new SSViewerTestFixture();
|
||||||
return $t->process($data);
|
return $t->process($data);
|
||||||
}
|
}
|
||||||
@ -1340,6 +1341,34 @@ after')
|
|||||||
|
|
||||||
$this->assertEquals(1, $count);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -634,10 +634,16 @@ class SSViewer {
|
|||||||
/**
|
/**
|
||||||
* Create a template from a string instead of a .ss file
|
* 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
|
* @return SSViewer
|
||||||
*/
|
*/
|
||||||
public static function fromString($content) {
|
public static function fromString($content, $cacheTemplate = null) {
|
||||||
return new SSViewer_FromString($content);
|
$viewer = new SSViewer_FromString($content);
|
||||||
|
if ($cacheTemplate !== null) {
|
||||||
|
$viewer->setCacheTemplate($cacheTemplate);
|
||||||
|
}
|
||||||
|
return $viewer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1147,8 +1153,26 @@ class SSViewer {
|
|||||||
* @subpackage view
|
* @subpackage view
|
||||||
*/
|
*/
|
||||||
class SSViewer_FromString extends SSViewer {
|
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;
|
protected $content;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates whether templates should be cached
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected $cacheTemplate;
|
||||||
|
|
||||||
public function __construct($content, TemplateParser $parser = null) {
|
public function __construct($content, TemplateParser $parser = null) {
|
||||||
$this->setParser($parser ?: Injector::inst()->get('SSTemplateParser'));
|
$this->setParser($parser ?: Injector::inst()->get('SSTemplateParser'));
|
||||||
$this->content = $content;
|
$this->content = $content;
|
||||||
@ -1162,16 +1186,42 @@ class SSViewer_FromString extends SSViewer {
|
|||||||
$arguments = null;
|
$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,"");
|
if(!file_exists($cacheFile) || isset($_GET['flush'])) {
|
||||||
$fh = fopen($tmpFile, 'w');
|
$content = $this->parseTemplateContent($this->content, "string sha1=$hash");
|
||||||
fwrite($fh, $template);
|
$fh = fopen($cacheFile,'w');
|
||||||
|
fwrite($fh, $content);
|
||||||
fclose($fh);
|
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;
|
return $val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param boolean $cacheTemplate
|
||||||
|
*/
|
||||||
|
public function setCacheTemplate($cacheTemplate) {
|
||||||
|
$this->cacheTemplate = (bool) $cacheTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function getCacheTemplate() {
|
||||||
|
return $this->cacheTemplate;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user