Skip to content

Commit

Permalink
bug #2384 Fix undef. source in exception. (SpacePossum)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.x branch.

Discussion
----------

Fix undef. source in exception.

closes #2383

Two things:
- the tests fails without a corrupt cache, so maybe the message is of or the cache generation is off (i.e. not respecting the `$index`)
- passing the `$source` seems superfluous (but maybe that is only the case in the exception message)

Commits
-------

772373c Fix undef. source in exception.
  • Loading branch information
fabpot committed Mar 5, 2017
2 parents 31bf795 + 772373c commit 82004b8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/Twig/Environment.php
Expand Up @@ -359,7 +359,8 @@ public function loadTemplate($name, $index = null)
}

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);

Expand All @@ -371,10 +372,10 @@ public function loadTemplate($name, $index = null)
*/
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);
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions test/Twig/Tests/EnvironmentTest.php
Expand Up @@ -334,6 +334,18 @@ public function testAddRuntimeLoader()
$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();
Expand All @@ -350,6 +362,27 @@ protected function getMockLoader($templateName, $templateContent)
}
}

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()
Expand Down

0 comments on commit 82004b8

Please sign in to comment.