Merge pull request #4415 from kinglozzer/pulls/lazyload-template-parser

Lazy load template parser to save memory
This commit is contained in:
Daniel Hensby 2015-07-22 10:52:23 +01:00
commit 589a39f6ba

View File

@ -786,7 +786,9 @@ class SSViewer implements Flushable {
* </code> * </code>
*/ */
public function __construct($templateList, TemplateParser $parser = null) { public function __construct($templateList, TemplateParser $parser = null) {
$this->setParser($parser ?: Injector::inst()->get('SSTemplateParser')); if ($parser) {
$this->setParser($parser);
}
if(!is_array($templateList) && substr((string) $templateList,-3) == '.ss') { if(!is_array($templateList) && substr((string) $templateList,-3) == '.ss') {
$this->chosenTemplates['main'] = $templateList; $this->chosenTemplates['main'] = $templateList;
@ -830,6 +832,9 @@ class SSViewer implements Flushable {
*/ */
public function getParser() public function getParser()
{ {
if (!$this->parser) {
$this->setParser(Injector::inst()->get('SSTemplateParser'));
}
return $this->parser; return $this->parser;
} }
@ -1106,9 +1111,11 @@ class SSViewer implements Flushable {
// through $Content and $Layout placeholders. // through $Content and $Layout placeholders.
foreach(array('Content', 'Layout') as $subtemplate) { foreach(array('Content', 'Layout') as $subtemplate) {
if(isset($this->chosenTemplates[$subtemplate])) { if(isset($this->chosenTemplates[$subtemplate])) {
$subtemplateViewer = new SSViewer($this->chosenTemplates[$subtemplate], $this->parser); $subtemplateViewer = clone $this;
// Disable requirements - this will be handled by the parent template
$subtemplateViewer->includeRequirements(false); $subtemplateViewer->includeRequirements(false);
$subtemplateViewer->setPartialCacheStore($this->getPartialCacheStore()); // The subtemplate is the only file we want to process, so set it as the "main" template file
$subtemplateViewer->chosenTemplates = array('main' => $this->chosenTemplates[$subtemplate]);
$underlay[$subtemplate] = $subtemplateViewer->process($item, $arguments); $underlay[$subtemplate] = $subtemplateViewer->process($item, $arguments);
} }
@ -1174,7 +1181,7 @@ class SSViewer implements Flushable {
} }
public function parseTemplateContent($content, $template="") { public function parseTemplateContent($content, $template="") {
return $this->parser->compileString( return $this->getParser()->compileString(
$content, $content,
$template, $template,
Director::isDev() && Config::inst()->get('SSViewer', 'source_file_comments') Director::isDev() && Config::inst()->get('SSViewer', 'source_file_comments')
@ -1243,7 +1250,10 @@ class SSViewer_FromString extends SSViewer {
protected $cacheTemplate; protected $cacheTemplate;
public function __construct($content, TemplateParser $parser = null) { public function __construct($content, TemplateParser $parser = null) {
$this->setParser($parser ?: Injector::inst()->get('SSTemplateParser')); if ($parser) {
$this->setParser($parser);
}
$this->content = $content; $this->content = $content;
} }