Added a cache for extension freshness test

Freshness of each extension was been checked everytime a template was loaded.
Projects with many templates and extension benefit noticeably with this cache.
Only environments with debug or autoload set are affected.
This commit is contained in:
David Stone
2015-10-06 20:10:54 -06:00
committed by Fabien Potencier
parent e624d0849e
commit 77c94ea205
+10 -5
View File
@@ -48,6 +48,7 @@ class Twig_Environment
private $originalCache;
private $bcWriteCacheFile = false;
private $bcGetCacheFilename = false;
private $lastModifiedExtension = 0;
/**
* Constructor.
@@ -454,14 +455,16 @@ class Twig_Environment
*/
public function isTemplateFresh($name, $time)
{
foreach ($this->extensions as $extension) {
$r = new ReflectionObject($extension);
if (filemtime($r->getFileName()) > $time) {
return false;
if(0 === $this->lastModifiedExtension) {
foreach ($this->extensions as $extension) {
$r = new ReflectionObject($extension);
if (($extensionTime = filemtime($r->getFileName())) > $this->lastModifiedExtension) {
$this->lastModifiedExtension = $extensionTime;
}
}
}
return $this->getLoader()->isFresh($name, $time);
return $this->lastModifiedExtension <= $time && $this->getLoader()->isFresh($name, $time);
}
/**
@@ -767,6 +770,8 @@ class Twig_Environment
throw new LogicException(sprintf('Unable to register extension "%s" as extensions have already been initialized.', $extension->getName()));
}
$this->lastModifiedExtension = 0;
$this->extensions[$extension->getName()] = $extension;
}