Fix undef. source in exception.

This commit is contained in:
SpacePossum
2017-01-31 10:46:11 +01:00
parent 2b0d2cb71c
commit 772373cfd3
2 changed files with 38 additions and 4 deletions
+5 -4
View File
@@ -359,7 +359,8 @@ class Twig_Environment
}
if (!class_exists($cls, false)) {
$content = $this->compileSource($this->getLoader()->getSourceContext($name));
$source = $this->getLoader()->getSourceContext($name);
$content = $this->compileSource($source);
$this->cache->write($key, $content);
$this->cache->load($key);
@@ -371,10 +372,10 @@ class Twig_Environment
*/
eval('?>'.$content);
}
}
if (!class_exists($cls, false)) {
throw new Twig_Error_Runtime(sprintf('Failed to load Twig template "%s", index "%s": cache is corrupted.', $name, $index), -1, $source);
if (!class_exists($cls, false)) {
throw new Twig_Error_Runtime(sprintf('Failed to load Twig template "%s", index "%s": cache is corrupted.', $name, $index), -1, $source);
}
}
}
+33
View File
@@ -334,6 +334,18 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
$this->assertEquals('foo', $twig->render('func_string_named_args'));
}
/**
* @expectedException Twig_Error_Runtime
* @expectedExceptionMessage Failed to load Twig template "testFailLoadTemplate.twig", index "abc": cache is corrupted in "testFailLoadTemplate.twig".
*/
public function testFailLoadTemplate()
{
$template = 'testFailLoadTemplate.twig';
$twig = new Twig_Environment(new Twig_Loader_Array(array($template => false)));
//$twig->setCache(new CorruptCache());
$twig->loadTemplate($template, 'abc');
}
protected function getMockLoader($templateName, $templateContent)
{
$loader = $this->getMockBuilder('Twig_LoaderInterface')->getMock();
@@ -350,6 +362,27 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
}
}
class CorruptCache implements Twig_CacheInterface
{
public function generateKey($name, $className)
{
return $name.':'.$className;
}
public function write($key, $content)
{
}
public function load($key)
{
}
public function getTimestamp($key)
{
time();
}
}
class Twig_Tests_EnvironmentTest_Extension_WithGlobals extends Twig_Extension
{
public function getGlobals()