Support template directories.
authorGunnar Wrobel <p@rdus.de>
Wed, 12 Jan 2011 09:30:47 +0000 (10:30 +0100)
committerGunnar Wrobel <p@rdus.de>
Wed, 12 Jan 2011 09:30:47 +0000 (10:30 +0100)
components/TODO
components/lib/Components/Helper/Templates/Directory.php [new file with mode: 0644]
components/package.xml
components/test/Components/Unit/Components/Helper/TemplatesTest.php
components/test/Components/fixture/templates/dir/one [new file with mode: 0644]
components/test/Components/fixture/templates/dir/two [new file with mode: 0644]

index f5748cb..d9edc52 100644 (file)
@@ -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 (file)
index 0000000..0e4a6f2
--- /dev/null
@@ -0,0 +1,90 @@
+<?php
+/**
+ * Components_Helper_Templates_Directory:: converts template files from a
+ * directory into files in a target directory.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Components
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @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 <wrobel@pardus.de>
+ * @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
index b1fee86..ac789fd 100644 (file)
@@ -24,8 +24,8 @@
   <email>jan@horde.org</email>
   <active>yes</active>
  </lead>
- <date>2011-01-11</date>
- <time>11:29:20</time>
+ <date>2011-01-12</date>
+ <time>10:29:42</time>
  <version>
   <release>0.0.1</release>
   <api>0.0.1</api>
@@ -69,6 +69,7 @@
        <file name="Printf.php" role="php" />
       </dir> <!-- /lib/Components/Helper/Template -->
       <dir name="Templates">
+       <file name="Directory.php" role="php" />
        <file name="Prefix.php" role="php" />
        <file name="Single.php" role="php" />
       </dir> <!-- /lib/Components/Helper/Templates -->
        <file name="IGNORE.txt" role="test" />
       </dir> <!-- /test/Components/fixture/simple -->
       <dir name="templates">
+       <dir name="dir">
+        <file name="one" role="test" />
+        <file name="two" role="test" />
+       </dir> <!-- /test/Components/fixture/templates/dir -->
        <file name="hudson-component-build.xml.template" role="test" />
        <file name="hudson-component-config.xml.template" role="test" />
        <file name="hudson-component-phpunit.xml.template" role="test" />
    <install as="Components/Helper/Tree.php" name="lib/Components/Helper/Tree.php" />
    <install as="Components/Helper/Template/Php.php" name="lib/Components/Helper/Template/Php.php" />
    <install as="Components/Helper/Template/Printf.php" name="lib/Components/Helper/Template/Printf.php" />
+   <install as="Components/Helper/Templates/Directory.php" name="lib/Components/Helper/Templates/Directory.php" />
    <install as="Components/Helper/Templates/Prefix.php" name="lib/Components/Helper/Templates/Prefix.php" />
    <install as="Components/Helper/Templates/Single.php" name="lib/Components/Helper/Templates/Single.php" />
    <install as="Components/Module/Base.php" name="lib/Components/Module/Base.php" />
    <install as="Components/fixture/templates/php.template" name="test/Components/fixture/templates/php.template" />
    <install as="Components/fixture/templates/simple.template" name="test/Components/fixture/templates/simple.template" />
    <install as="Components/fixture/templates/variables.template" name="test/Components/fixture/templates/variables.template" />
+   <install as="Components/fixture/templates/dir/one" name="test/Components/fixture/templates/dir/one" />
+   <install as="Components/fixture/templates/dir/two" name="test/Components/fixture/templates/dir/two" />
    <install as="Components/Integration/ComponentsTest.php" name="test/Components/Integration/ComponentsTest.php" />
    <install as="Components/Integration/Components/Module/CiSetupTest.php" name="test/Components/Integration/Components/Module/CiSetupTest.php" />
    <install as="Components/Integration/Components/Module/DependenciesTest.php" name="test/Components/Integration/Components/Module/DependenciesTest.php" />
     <release>alpha</release>
     <api>alpha</api>
    </stability>
-   <date>2011-01-11</date>
+   <date>2011-01-12</date>
    <license uri="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">LGPL</license>
    <notes>
 * Initial release
index 3186d80..af7762b 100644 (file)
@@ -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 (file)
index 0000000..1926d5c
--- /dev/null
@@ -0,0 +1,3 @@
+<?php
+
+echo $one;
diff --git a/components/test/Components/fixture/templates/dir/two b/components/test/Components/fixture/templates/dir/two
new file mode 100644 (file)
index 0000000..eecf243
--- /dev/null
@@ -0,0 +1,3 @@
+<?php
+
+echo $two;