Skip to content
Snippets Groups Projects
Commit dced5e11 authored by Alex Pott's avatar Alex Pott
Browse files

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

parent 75c40754
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -21,6 +21,13 @@ class TwigEnvironment extends \Twig_Environment { ...@@ -21,6 +21,13 @@ class TwigEnvironment extends \Twig_Environment {
protected $cache_object = NULL; protected $cache_object = NULL;
protected $storage = NULL; protected $storage = NULL;
/**
* Static cache of template classes.
*
* @var array
*/
protected $templateClasses;
/** /**
* Constructs a TwigEnvironment object and stores cache and storage * Constructs a TwigEnvironment object and stores cache and storage
* internally. * internally.
...@@ -29,6 +36,8 @@ public function __construct(\Twig_LoaderInterface $loader = NULL, $options = arr ...@@ -29,6 +36,8 @@ public function __construct(\Twig_LoaderInterface $loader = NULL, $options = arr
// @todo Pass as arguments from the DIC. // @todo Pass as arguments from the DIC.
$this->cache_object = cache(); $this->cache_object = cache();
$this->templateClasses = array();
parent::__construct($loader, $options); parent::__construct($loader, $options);
} }
...@@ -110,4 +119,19 @@ protected function storage() { ...@@ -110,4 +119,19 @@ protected function storage() {
return $this->storage; 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];
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment