diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index e7b5dc45b..7a3ac5f72 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -416,9 +416,17 @@ class Twig_Environment $this->writeCacheFile($key, $content); } else { $this->cache->write($key, $content); + $this->cache->load($key); } - eval('?>'.$content); + if (!class_exists($cls, false)) { + /* Last line of defense if either $this->bcWriteCacheFile was used, + * $this->cache is implemented as a no-op or we have a race condition + * where the cache was cleared between the above calls to write to and load from + * the cache. + */ + eval('?>'.$content); + } } } diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index 340ec453b..fac0d5337 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -217,7 +217,9 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase ->will($this->returnValue(0)); $loader->expects($this->never()) ->method('isFresh'); - $cache->expects($this->never()) + $cache->expects($this->once()) + ->method('write'); + $cache->expects($this->once()) ->method('load'); $twig->loadTemplate($templateName); @@ -245,7 +247,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase $loader->expects($this->once()) ->method('isFresh') ->will($this->returnValue(true)); - $cache->expects($this->once()) + $cache->expects($this->atLeastOnce()) ->method('load'); $twig->loadTemplate($templateName); @@ -271,7 +273,9 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase $loader->expects($this->once()) ->method('isFresh') ->will($this->returnValue(false)); - $cache->expects($this->never()) + $cache->expects($this->once()) + ->method('write'); + $cache->expects($this->once()) ->method('load'); $twig->loadTemplate($templateName);