Issue #1979290 by Cottser, soulston: Add static caching to Twig_Environment::getTemplateClass().

8.0.x
Alex Pott 2013-05-08 18:34:44 +01:00
parent 75c407547f
commit dced5e1164
1 changed files with 24 additions and 0 deletions

View File

@ -21,6 +21,13 @@ class TwigEnvironment extends \Twig_Environment {
protected $cache_object = NULL;
protected $storage = NULL;
/**
* Static cache of template classes.
*
* @var array
*/
protected $templateClasses;
/**
* Constructs a TwigEnvironment object and stores cache and storage
* internally.
@ -29,6 +36,8 @@ class TwigEnvironment extends \Twig_Environment {
// @todo Pass as arguments from the DIC.
$this->cache_object = cache();
$this->templateClasses = array();
parent::__construct($loader, $options);
}
@ -110,4 +119,19 @@ class TwigEnvironment extends \Twig_Environment {
return $this->storage;
}
/**
* {@inheritdoc}
*/
public function getTemplateClass($name, $index = null) {
// We override this method to add caching because it gets called multiple
// times when the same template is used more than once. For example, a page
// rendering 50 nodes without any node template overrides will use the same
// node.html.twig for the output of each node and the same compiled class.
$cache_index = $name . (NULL === $index ? '' : '_' . $index);
if (!isset($this->templateClasses[$cache_index])) {
$this->templateClasses[$cache_index] = parent::getTemplateClass($name, $index);
}
return $this->templateClasses[$cache_index];
}
}