From 4105c99fbeb2a9ad0bb1104ee0ee7e96886e46a7 Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel Date: Wed, 12 Jan 2011 11:36:25 +0100 Subject: [PATCH] Bail out if the input is not okay. --- components/lib/Components/Helper/Template/Printf.php | 12 ++++++++++++ .../Components/Unit/Components/Helper/TemplatesTest.php | 15 +++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/components/lib/Components/Helper/Template/Printf.php b/components/lib/Components/Helper/Template/Printf.php index 1f26742f3..3103392d3 100644 --- a/components/lib/Components/Helper/Template/Printf.php +++ b/components/lib/Components/Helper/Template/Printf.php @@ -37,6 +37,18 @@ extends Components_Helper_Template */ public function write(array $parameters = array()) { + foreach ($parameters as $key => $value) { + if (!is_string($value)) { + throw new Components_Exception( + sprintf( + 'File %s is a printf() based template and requires string input only. Key "%s" however is of type %s!', + $this->_source, + $key, + gettype($value) + ) + ); + } + } $source = file_get_contents($this->_source); file_put_contents( $this->_target, vsprintf($source, $parameters) diff --git a/components/test/Components/Unit/Components/Helper/TemplatesTest.php b/components/test/Components/Unit/Components/Helper/TemplatesTest.php index af7762be0..d6282a714 100644 --- a/components/test/Components/Unit/Components/Helper/TemplatesTest.php +++ b/components/test/Components/Unit/Components/Helper/TemplatesTest.php @@ -89,6 +89,21 @@ extends Components_TestCase ); } + /** + * @expectedException Components_Exception + */ + public function testNoStringInput() + { + $tdir = $this->getTemporaryDirectory(); + $templates = new Components_Helper_Templates_Single( + dirname(__FILE__) . '/../../../fixture/templates', + $tdir, + 'variables', + 'target' + ); + $templates->write(array('1' => new stdClass, '2' => 'Two')); + } + public function testPrefix() { $tdir = $this->getTemporaryDirectory(); -- 2.11.0