From: Gunnar Wrobel Date: Tue, 11 Jan 2011 05:27:41 +0000 (+0100) Subject: Complete printf templating. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=e252bcd18187ff024380a3fb12a904331e160ac4;p=horde.git Complete printf templating. --- diff --git a/components/lib/Components/Helper/Templates.php b/components/lib/Components/Helper/Templates.php index d7d598e65..91b8e2668 100644 --- a/components/lib/Components/Helper/Templates.php +++ b/components/lib/Components/Helper/Templates.php @@ -28,6 +28,13 @@ class Components_Helper_Templates { /** + * The source location. + * + * @var string + */ + private $_source; + + /** * The target location. * * @var string @@ -42,6 +49,11 @@ class Components_Helper_Templates */ public function __construct($source, $target) { + if (file_exists($source . '.template')) { + $this->_source = $source . '.template'; + } else { + throw new Components_Exception("No template at $source!"); + } $this->_target = $target; } @@ -54,6 +66,7 @@ class Components_Helper_Templates */ public function write(array $parameters = array()) { - file_put_contents($this->_target, ''); + $source = file_get_contents($this->_source); + file_put_contents($this->_target, vsprintf($source, $parameters)); } } \ No newline at end of file diff --git a/components/test/Components/Unit/Components/Helper/TemplatesTest.php b/components/test/Components/Unit/Components/Helper/TemplatesTest.php index eeb180548..fffa02069 100644 --- a/components/test/Components/Unit/Components/Helper/TemplatesTest.php +++ b/components/test/Components/Unit/Components/Helper/TemplatesTest.php @@ -37,9 +37,37 @@ extends Components_TestCase { public function testWrite() { + $source = dirname(__FILE__) . '/../../../fixture/templates/simple'; $target = $this->getTemporaryDirectory() . '/target'; - $templates = new Components_Helper_Templates('', $target, array()); + $templates = new Components_Helper_Templates($source, $target); $templates->write(); $this->assertTrue(file_exists($target)); } + + public function testSource() + { + $source = dirname(__FILE__) . '/../../../fixture/templates/simple'; + $target = $this->getTemporaryDirectory() . '/target'; + $templates = new Components_Helper_Templates($source, $target); + $templates->write(); + $this->assertEquals("SIMPLE\n", file_get_contents($target)); + } + + /** + * @expectedException Components_Exception + */ + public function testMissingSource() + { + $source = dirname(__FILE__) . '/NO_SUCH_TEMPLATE'; + $templates = new Components_Helper_Templates($source, ''); + } + + public function testVariables() + { + $source = dirname(__FILE__) . '/../../../fixture/templates/variables'; + $target = $this->getTemporaryDirectory() . '/target'; + $templates = new Components_Helper_Templates($source, $target); + $templates->write(array('1' => 'One', '2' => 'Two')); + $this->assertEquals("One : Two\n", file_get_contents($target)); + } } \ No newline at end of file