From a0e8d58a6e7ba0edcb90cb824acdc9048de155bb Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Thu, 19 Jul 2018 13:57:54 -0400 Subject: [PATCH] Do not throw error on validate or parse name if throw var is false --- lib/Twig/Loader/Filesystem.php | 12 ++++++++++-- test/Twig/Tests/Loader/FilesystemTest.php | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/Twig/Loader/Filesystem.php b/lib/Twig/Loader/Filesystem.php index 4e8be0d53..f80d67d9f 100644 --- a/lib/Twig/Loader/Filesystem.php +++ b/lib/Twig/Loader/Filesystem.php @@ -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); diff --git a/test/Twig/Tests/Loader/FilesystemTest.php b/test/Twig/Tests/Loader/FilesystemTest.php index bd2acbd75..3200fd6e3 100644 --- a/test/Twig/Tests/Loader/FilesystemTest.php +++ b/test/Twig/Tests/Loader/FilesystemTest.php @@ -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')); + } }