From 1b47b4010445265790331fd2840ffc43ae703f0e Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 8 Apr 2012 14:12:00 +0200 Subject: [PATCH] enhanced error reporting when the template file is an instance of SplFileInfo --- CHANGELOG | 1 + lib/Twig/Error.php | 7 ++++++- test/Twig/Tests/ErrorTest.php | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletions(-) 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 }}")); -- 1.7.2.5