mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 11:27:00 +00:00
Fix race condition in Environment::loadTemplate
This commit is contained in:
@@ -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
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user