merged branch hason/arrayloader (PR #542)

Commits
-------

16d7800 fixed a crash when an object with __toString() method is passed as template name

Discussion
----------

fixed a crash when an object with __toString() method is passed as templ...

...ate name

---------------------------------------------------------------------------

by fabpot at 2011/12/01 03:58:47 -0800

Not sure about this one as the phpdoc clearly state that the name is a string. Do you have a specific user case in mind?

---------------------------------------------------------------------------

by hason at 2011/12/01 04:26:47 -0800

I use the class "Twig_Loader_Chain" as template loader in Symfony2 application. One of the embeded loaders is "Twig_Loader_Array". In Symfony2 is used "Symfony\Component\Templating\TemplateReferenceInterface" for internal representation of a template and it causes a crash.
This commit is contained in:
Fabien Potencier
2011-12-02 15:11:24 +01:00
2 changed files with 30 additions and 1 deletions
+4 -1
View File
@@ -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));
}
+26
View File
@@ -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;
}
}