From: Fabien Potencier Date: Sun, 8 Apr 2012 12:12:00 +0000 (+0200) Subject: enhanced error reporting when the template file is an instance of SplFileInfo X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=1b47b4010445265790331fd2840ffc43ae703f0e;p=web%2Fkonrad%2Ftwig.git enhanced error reporting when the template file is an instance of SplFileInfo --- diff --git a/CHANGELOG b/CHANGELOG index e457b35..da772b5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.7.0 (2012-XX-XX) + * enhanced error reporting when the template file is an instance of SplFileInfo * added Twig_Environment::mergeGlobals() * fixed a regression when a template only extends another one without defining any blocks * added compilation checks to avoid misuses of the sandbox tag diff --git a/lib/Twig/Error.php b/lib/Twig/Error.php index b945706..a19aca6 100644 --- a/lib/Twig/Error.php +++ b/lib/Twig/Error.php @@ -140,7 +140,12 @@ class Twig_Error extends Exception } if (null !== $this->filename) { - $this->message .= sprintf(' in %s', is_string($this->filename) ? '"'.$this->filename.'"' : json_encode($this->filename)); + if (is_string($this->filename) || (is_object($this->filename) && method_exists($this->filename, '__toString'))) { + $filename = sprintf('"%s"', $this->filename); + } else { + $filename = json_encode($this->filename); + } + $this->message .= sprintf(' in %s', $filename); } if ($this->lineno >= 0) { diff --git a/test/Twig/Tests/ErrorTest.php b/test/Twig/Tests/ErrorTest.php index a63b5fa..45d4053 100644 --- a/test/Twig/Tests/ErrorTest.php +++ b/test/Twig/Tests/ErrorTest.php @@ -11,6 +11,22 @@ class Twig_Tests_ErrorTest extends PHPUnit_Framework_TestCase { + public function testErrorWithObjectFilename() + { + $error = new Twig_Error('foo'); + $error->setTemplateFile(new SplFileInfo(__FILE__)); + + $this->assertContains('test/Twig/Tests/ErrorTest.php', $error->getMessage()); + } + + public function testErrorWithArrayFilename() + { + $error = new Twig_Error('foo'); + $error->setTemplateFile(array('foo' => 'bar')); + + $this->assertEquals('foo in {"foo":"bar"}', $error->getMessage()); + } + public function testTwigExceptionAddsFileAndLineWhenMissing() { $loader = new Twig_Loader_Array(array('index' => "\n\n{{ foo.bar }}"));