Do not throw error on validate or parse name if throw var is false

This commit is contained in:
Yonel Ceruto
2018-07-19 13:57:54 -04:00
parent cb848ee0a4
commit a0e8d58a6e
2 changed files with 25 additions and 2 deletions
+10 -2
View File
@@ -195,9 +195,17 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderI
throw new Twig_Error_Loader($this->errorCache[$name]);
}
$this->validateName($name);
try {
$this->validateName($name);
list($namespace, $shortname) = $this->parseName($name);
list($namespace, $shortname) = $this->parseName($name);
} catch (Twig_Error_Loader $e) {
if (!$throw) {
return false;
}
throw $e;
}
if (!isset($this->paths[$namespace])) {
$this->errorCache[$name] = sprintf('There are no registered paths for namespace "%s".', $namespace);
+15
View File
@@ -223,4 +223,19 @@ class Twig_Tests_Loader_FilesystemTest extends \PHPUnit\Framework\TestCase
$loader->addPath('phar://'.dirname(__FILE__).'/Fixtures/phar/phar-sample.phar');
$this->assertSame('hello from phar', $loader->getSourceContext('hello.twig')->getCode());
}
public function testTemplateExistsAlwaysReturnsBool()
{
$loader = new Twig_Loader_Filesystem(array());
$this->assertFalse($loader->exists("foo\0.twig"));
$this->assertFalse($loader->exists('../foo.twig'));
$this->assertFalse($loader->exists('@foo'));
$this->assertFalse($loader->exists('foo'));
$this->assertFalse($loader->exists('@foo/bar.twig'));
$loader->addPath(__DIR__.'/Fixtures/normal');
$this->assertTrue($loader->exists('index.html'));
$loader->addPath(__DIR__.'/Fixtures/normal', 'foo');
$this->assertTrue($loader->exists('@foo/index.html'));
}
}