From ae7fa11c9179517007419ffc0b44741fbdfc4d18 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 23 Jul 2013 14:30:22 +0200 Subject: [PATCH] [Twig] fixed TwigEngine::exists() method when a template contains a syntax error (closes #88546) --- .../Bridge/Twig/Tests/TwigEngineTest.php | 56 +++++++++++++++++++ src/Symfony/Bridge/Twig/TwigEngine.php | 14 ++++- 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Bridge/Twig/Tests/TwigEngineTest.php diff --git a/src/Symfony/Bridge/Twig/Tests/TwigEngineTest.php b/src/Symfony/Bridge/Twig/Tests/TwigEngineTest.php new file mode 100644 index 0000000000..1e6a3c4933 --- /dev/null +++ b/src/Symfony/Bridge/Twig/Tests/TwigEngineTest.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Twig\Tests; + +use Symfony\Bridge\Twig\TwigEngine; + +class TwigEngineTest extends TestCase +{ + public function testExistsWithTemplateInstances() + { + $engine = $this->getTwig(); + + $this->assertTrue($engine->exists($this->getMockForAbstractClass('Twig_Template', array(), '', false))); + } + + public function testExistsWithNonExistentTemplates() + { + $engine = $this->getTwig(); + + $this->assertFalse($engine->exists('foobar')); + } + + public function testExistsWithTemplateWithSyntaxErrors() + { + $engine = $this->getTwig(); + + $this->assertTrue($engine->exists('error')); + } + + public function testExists() + { + $engine = $this->getTwig(); + + $this->assertTrue($engine->exists('index')); + } + + protected function getTwig() + { + $twig = new \Twig_Environment(new \Twig_Loader_Array(array( + 'index' => 'foo', + 'error' => '{{ foo }', + ))); + $parser = $this->getMock('Symfony\Component\Templating\TemplateNameParserInterface'); + + return new TwigEngine($twig, $parser); + } +} diff --git a/src/Symfony/Bridge/Twig/TwigEngine.php b/src/Symfony/Bridge/Twig/TwigEngine.php index 955d4e0bae..41e15bacaa 100644 --- a/src/Symfony/Bridge/Twig/TwigEngine.php +++ b/src/Symfony/Bridge/Twig/TwigEngine.php @@ -75,9 +75,19 @@ class TwigEngine implements EngineInterface, StreamingEngineInterface */ public function exists($name) { + if ($name instanceof \Twig_Template) { + return true; + } + + $loader = $this->environment->getLoader(); + + if ($loader instanceof \Twig_ExistsLoaderInterface) { + return $loader->exists($name); + } + try { - $this->load($name); - } catch (\InvalidArgumentException $e) { + $loader->getSource($name); + } catch (\Twig_Error_Loader $e) { return false; }