From 16d780043f20c21fdeef409889051a760ed0a6d6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Martin=20Haso=C5=88?= Date: Thu, 1 Dec 2011 12:43:07 +0100 Subject: [PATCH] fixed a crash when an object with __toString() method is passed as template name --- lib/Twig/Loader/Array.php | 5 ++++- test/Twig/Tests/Loader/ArrayTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletions(-) diff --git a/lib/Twig/Loader/Array.php b/lib/Twig/Loader/Array.php index 962d64c..32bb7e4 100644 --- a/lib/Twig/Loader/Array.php +++ b/lib/Twig/Loader/Array.php @@ -47,7 +47,7 @@ class Twig_Loader_Array implements Twig_LoaderInterface */ public function setTemplate($name, $template) { - $this->templates[$name] = $template; + $this->templates[(string) $name] = $template; } /** @@ -59,6 +59,7 @@ class Twig_Loader_Array implements Twig_LoaderInterface */ public function getSource($name) { + $name = (string) $name; if (!isset($this->templates[$name])) { throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name)); } @@ -75,6 +76,7 @@ class Twig_Loader_Array implements Twig_LoaderInterface */ public function getCacheKey($name) { + $name = (string) $name; if (!isset($this->templates[$name])) { throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name)); } @@ -90,6 +92,7 @@ class Twig_Loader_Array implements Twig_LoaderInterface */ public function isFresh($name, $time) { + $name = (string) $name; if (!isset($this->templates[$name])) { throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name)); } diff --git a/test/Twig/Tests/Loader/ArrayTest.php b/test/Twig/Tests/Loader/ArrayTest.php index ae7648f..1369a6b 100644 --- a/test/Twig/Tests/Loader/ArrayTest.php +++ b/test/Twig/Tests/Loader/ArrayTest.php @@ -68,4 +68,30 @@ class Twig_Tests_Loader_ArrayTest extends PHPUnit_Framework_TestCase $loader->isFresh('foo', time()); } + + public function testTemplateReference() + { + $name = new Twig_Test_Loader_TemplateReference('foo'); + $loader = new Twig_Loader_Array(array('foo' => 'bar')); + + $loader->getCacheKey($name); + $loader->getSource($name); + $loader->isFresh($name, time()); + $loader->setTemplate($name, 'foobar'); + } +} + +class Twig_Test_Loader_TemplateReference +{ + private $name; + + public function __construct($name) + { + $this->name = $name; + } + + public function __toString() + { + return $this->name; + } } -- 1.7.2.5