bug #2192 fixed Twig_Loader_Chain logic (fabpot)

This PR was merged into the 1.x branch.

Discussion
----------

fixed Twig_Loader_Chain logic

Commits
-------

a2b9426 fixed Twig_Loader_Chain logic
This commit is contained in:
Fabien Potencier
2016-10-20 08:17:54 -07:00
2 changed files with 13 additions and 11 deletions
+6 -10
View File
@@ -70,26 +70,22 @@ class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExistsLoaderInterf
{
$exceptions = array();
foreach ($this->loaders as $loader) {
if (!$loader instanceof Twig_SourceContextLoaderInterface) {
continue;
}
if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
continue;
}
try {
return $loader->getSourceContext($name);
if ($loader instanceof Twig_SourceContextLoaderInterface) {
return $loader->getSourceContext($name);
}
return new Twig_Source($loader->getSource($name), $name);
} catch (Twig_Error_Loader $e) {
$exceptions[] = $e->getMessage();
}
}
if ($exceptions) {
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
}
return new Twig_Source($this->getSource($name), $name);
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
}
/**
+7 -1
View File
@@ -27,6 +27,7 @@ class Twig_Tests_Loader_ChainTest extends PHPUnit_Framework_TestCase
$path = dirname(__FILE__).'/../Fixtures';
$loader = new Twig_Loader_Chain(array(
new Twig_Loader_Array(array('foo' => 'bar')),
new Twig_Loader_Array(array('errors/index.html' => 'baz')),
new Twig_Loader_Filesystem(array($path)),
));
@@ -34,7 +35,12 @@ class Twig_Tests_Loader_ChainTest extends PHPUnit_Framework_TestCase
$this->assertNull($loader->getSourceContext('foo')->getPath());
$this->assertEquals('errors/index.html', $loader->getSourceContext('errors/index.html')->getName());
$this->assertEquals(realpath($path.'/errors/index.html'), realpath($loader->getSourceContext('errors/index.html')->getPath()));
$this->assertNull($loader->getSourceContext('errors/index.html')->getPath());
$this->assertEquals('baz', $loader->getSourceContext('errors/index.html')->getCode());
$this->assertEquals('errors/base.html', $loader->getSourceContext('errors/base.html')->getName());
$this->assertEquals(realpath($path.'/errors/base.html'), realpath($loader->getSourceContext('errors/base.html')->getPath()));
$this->assertNotEquals('baz', $loader->getSourceContext('errors/base.html')->getCode());
}
/**