From: Gunnar Wrobel
Date: Wed, 12 Jan 2011 09:30:47 +0000 (+0100)
Subject: Support template directories.
X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=436471788ee4f1787cf69c90227c9f052809d302;p=horde.git
Support template directories.
---
diff --git a/components/TODO b/components/TODO
index f5748cb80..d9edc522e 100644
--- a/components/TODO
+++ b/components/TODO
@@ -41,7 +41,8 @@
- WWW frontend
- The package descriptions need to be escaped when writing them into
- the Hudson config.xml file
+ the Hudson config.xml file (probably means that the templating
+ should be switched to PHP based templates)
- Allow channel dependency detection with external/local package
dependencies.
diff --git a/components/lib/Components/Helper/Templates/Directory.php b/components/lib/Components/Helper/Templates/Directory.php
new file mode 100644
index 000000000..0e4a6f288
--- /dev/null
+++ b/components/lib/Components/Helper/Templates/Directory.php
@@ -0,0 +1,90 @@
+
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Components
+ */
+
+/**
+ * Components_Helper_Templates_Directory:: converts template files from a
+ * directory into files in a target directory.
+ *
+ * Copyright 2011 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Horde
+ * @package Components
+ * @author Gunnar Wrobel
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Components
+ */
+class Components_Helper_Templates_Directory
+extends Components_Helper_Templates
+{
+ /**
+ * The source location.
+ *
+ * @var string
+ */
+ private $_source;
+
+ /**
+ * The target location.
+ *
+ * @var string
+ */
+ private $_target;
+
+ /**
+ * Constructor.
+ *
+ * @param string $sdir The templates source directory.
+ * @param string $tdir The templates target directory.
+ */
+ public function __construct($sdir, $tdir)
+ {
+ if (file_exists($sdir)) {
+ $this->_source = $sdir;
+ } else {
+ throw new Components_Exception("No template directory at $sdir!");
+ }
+ $this->_target = $tdir;
+ }
+
+ /**
+ * Rewrite the template(s) from the source(s) to the target location(s).
+ *
+ * @param array $parameters The template(s) parameters.
+ *
+ * @return NULL
+ */
+ public function write(array $parameters = array())
+ {
+ if (!file_exists($this->_target)) {
+ mkdir($this->_target, 0777, true);
+ }
+ foreach (
+ new RecursiveIteratorIterator(
+ new RecursiveDirectoryIterator($this->_source)
+ )
+ as $file
+ ) {
+ if ($file->isFile()) {
+ $this->writeSourceToTarget(
+ $file->getPathname(),
+ $this->_target . DIRECTORY_SEPARATOR . $file->getBasename(),
+ $parameters
+ );
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/components/package.xml b/components/package.xml
index b1fee86bf..ac789fd14 100644
--- a/components/package.xml
+++ b/components/package.xml
@@ -24,8 +24,8 @@
jan@horde.org
yes
- 2011-01-11
-
+ 2011-01-12
+
0.0.1
0.0.1
@@ -69,6 +69,7 @@
+
@@ -206,6 +207,10 @@
+
+
+
+
@@ -325,6 +330,7 @@
+
@@ -390,6 +396,8 @@
+
+
@@ -414,7 +422,7 @@
alpha
alpha
- 2011-01-11
+ 2011-01-12
LGPL
* Initial release
diff --git a/components/test/Components/Unit/Components/Helper/TemplatesTest.php b/components/test/Components/Unit/Components/Helper/TemplatesTest.php
index 3186d80c2..af7762be0 100644
--- a/components/test/Components/Unit/Components/Helper/TemplatesTest.php
+++ b/components/test/Components/Unit/Components/Helper/TemplatesTest.php
@@ -110,10 +110,9 @@ extends Components_TestCase
*/
public function testMissingPrefixTemplate()
{
- $tdir = $this->getTemporaryDirectory();
$templates = new Components_Helper_Templates_Prefix(
dirname(__FILE__) . '/../../../fixture/templates',
- $tdir,
+ $this->getTemporaryDirectory(),
'NOSUCHPREFIX',
'target'
);
@@ -150,4 +149,52 @@ extends Components_TestCase
file_get_contents($tdir . DIRECTORY_SEPARATOR . 'target')
);
}
+
+ public function testDirectory()
+ {
+ $tdir = $this->getTemporaryDirectory();
+ $templates = new Components_Helper_Templates_Directory(
+ dirname(__FILE__) . '/../../../fixture/templates/dir',
+ $tdir
+ );
+ $templates->write(array('one' => 'One', 'two' => 'Two'));
+ $this->assertEquals(
+ "One",
+ file_get_contents($tdir . DIRECTORY_SEPARATOR . 'one')
+ );
+ $this->assertEquals(
+ "Two",
+ file_get_contents($tdir . DIRECTORY_SEPARATOR . 'two')
+ );
+ }
+
+ /**
+ * @expectedException Components_Exception
+ */
+ public function testMissingDirectory()
+ {
+ new Components_Helper_Templates_Directory(
+ dirname(__FILE__) . '/../../../fixture/templates/NOSUCHDIR',
+ $this->getTemporaryDirectory()
+ );
+ }
+
+ public function testMissingTargetDirectory()
+ {
+ $tdir = $this->getTemporaryDirectory() . DIRECTORY_SEPARATOR
+ . 'a' .DIRECTORY_SEPARATOR . 'b';
+ $templates = new Components_Helper_Templates_Directory(
+ dirname(__FILE__) . '/../../../fixture/templates/dir',
+ $tdir
+ );
+ $templates->write(array('one' => 'One', 'two' => 'Two'));
+ $this->assertEquals(
+ "One",
+ file_get_contents($tdir . DIRECTORY_SEPARATOR . 'one')
+ );
+ $this->assertEquals(
+ "Two",
+ file_get_contents($tdir . DIRECTORY_SEPARATOR . 'two')
+ );
+ }
}
\ No newline at end of file
diff --git a/components/test/Components/fixture/templates/dir/one b/components/test/Components/fixture/templates/dir/one
new file mode 100644
index 000000000..1926d5c51
--- /dev/null
+++ b/components/test/Components/fixture/templates/dir/one
@@ -0,0 +1,3 @@
+