Fix race condition in Environment::loadTemplate

This commit is contained in:
Lorenz Schori
2015-09-20 18:08:49 +02:00
parent 9fde02fe55
commit 1bb7000a26
4 changed files with 10 additions and 38 deletions
+2 -10
View File
@@ -41,20 +41,12 @@ class Twig_Cache_Filesystem implements Twig_CacheInterface
return $this->directory.'/'.$hash[0].'/'.$hash[1].'/'.$hash.'.php';
}
/**
* {@inheritdoc}
*/
public function has($key)
{
return is_file($key);
}
/**
* {@inheritdoc}
*/
public function load($key)
{
require_once $key;
@include_once $key;
}
/**
@@ -98,6 +90,6 @@ class Twig_Cache_Filesystem implements Twig_CacheInterface
*/
public function getTimestamp($key)
{
return filemtime($key);
return (int) @filemtime($key);
}
}
+1 -16
View File
@@ -16,22 +16,12 @@
*/
class Twig_Cache_Null implements Twig_CacheInterface
{
private $buffer = array();
/**
* {@inheritdoc}
*/
public function generateKey($name, $className)
{
return $className;
}
/**
* {@inheritdoc}
*/
public function has($key)
{
return false;
return '';
}
/**
@@ -39,7 +29,6 @@ class Twig_Cache_Null implements Twig_CacheInterface
*/
public function write($key, $content)
{
$this->buffer[$key] = $content;
}
/**
@@ -47,9 +36,6 @@ class Twig_Cache_Null implements Twig_CacheInterface
*/
public function load($key)
{
eval('?>'.$this->buffer[$key]);
unset($this->buffer[$key]);
}
/**
@@ -57,7 +43,6 @@ class Twig_Cache_Null implements Twig_CacheInterface
*/
public function getTimestamp($key)
{
// never called as has() always returns false
return 0;
}
}
-9
View File
@@ -30,15 +30,6 @@ interface Twig_CacheInterface
*/
public function generateKey($name, $className);
/**
* Checks if the cache key exists.
*
* @param string $key The cache key
*
* @return bool true if the cache key exists, false otherwise
*/
public function has($key);
/**
* Writes the compiled template to cache.
*
+7 -3
View File
@@ -375,15 +375,19 @@ class Twig_Environment
$key = $this->cache->generateKey($name, $cls);
}
if (!$this->cache->has($key) || ($this->isAutoReload() && !$this->isTemplateFresh($name, $this->cache->getTimestamp($key)))) {
if (!$this->isAutoReload() || $this->isTemplateFresh($name, $this->cache->getTimestamp($key))) {
$this->cache->load($key);
}
if (!class_exists($cls, false)) {
$content = $this->compileSource($this->getLoader()->getSource($name), $name);
if ($this->bcWriteCacheFile) {
$this->writeCacheFile($key, $this->compileSource($this->getLoader()->getSource($name), $name));
} else {
$this->cache->write($key, $this->compileSource($this->getLoader()->getSource($name), $name));
}
eval('?>'.$content);
}
$this->cache->load($key);
}
if (!$this->runtimeInitialized) {