--- /dev/null
+<?php
+/**
+ * Components_Helper_Template:: converts a template into a target file.
+ *
+ * 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_Template:: converts a template into a target file.
+ *
+ * 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_Template
+{
+ /**
+ * Source template.
+ *
+ * @var string
+ */
+ protected $_source;
+
+ /**
+ * Target file.
+ *
+ * @var string
+ */
+ protected $_target;
+
+ /**
+ * Constructor.
+ *
+ * @param string $source The source location.
+ * @param string $target The target location.
+ */
+ public function __construct($source, $target)
+ {
+ $this->_source = $source;
+ $this->_target = $target;
+ }
+
+ /**
+ * Rewrite the template from the source to the target location.
+ *
+ * @param array $parameters The template parameters.
+ *
+ * @return NULL
+ */
+ public function write(array $parameters = array())
+ {
+ throw new Horde_Component_Exception('Overwrite in the extending class!');
+ }
+
+ /**
+ * A factory for the specific template type.
+ */
+ static public function factory($source, $target)
+ {
+ $sh = fopen($source, 'r');
+ $lead = fread($sh, 5);
+ fclose($sh);
+ if ($lead == '<?php') {
+ return new Components_Helper_Template_Php($source, $target);
+ } else {
+ return new Components_Helper_Template_Printf($source, $target);
+ }
+ }
+}
\ No newline at end of file
--- /dev/null
+<?php
+/**
+ * Components_Helper_Template_Php:: converts a PHP template into a target file.
+ *
+ * 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_Template_Php:: converts a PHP template into a target file.
+ *
+ * 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_Template_Php
+extends Components_Helper_Template
+{
+ /**
+ * Rewrite the template from the source to the target location.
+ *
+ * @param array $parameters The template parameters.
+ *
+ * @return NULL
+ */
+ public function write(array $parameters = array())
+ {
+ foreach ($parameters as $key => $value) {
+ ${$key} = $value;
+ }
+ ob_start();
+ include $this->_source;
+ file_put_contents($this->_target, ob_get_clean());
+ }
+}
\ No newline at end of file
--- /dev/null
+<?php
+/**
+ * Components_Helper_Template_Printf:: converts a template into a target file using vsprintf().
+ *
+ * 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_Template_Printf:: converts a template into a target file using vsprintf().
+ *
+ * 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_Template_Printf
+extends Components_Helper_Template
+{
+ /**
+ * Rewrite the template from the source to the target location.
+ *
+ * @param array $parameters The template parameters.
+ *
+ * @return NULL
+ */
+ public function write(array $parameters = array())
+ {
+ $source = file_get_contents($this->_source);
+ file_put_contents(
+ $this->_target, vsprintf($source, $parameters)
+ );
+ }
+}
\ No newline at end of file
* @license http://www.fsf.org/copyleft/lgpl.html LGPL
* @link http://pear.horde.org/index.php?package=Components
*/
-class Components_Helper_Templates
+abstract class 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.
- * @param string $sfile The exact template source file.
- * @param string $tfile The exact template target file.
- */
- public function __construct($sdir, $tdir, $sfile = '', $tfile = '')
- {
- $source = $sdir . DIRECTORY_SEPARATOR . $sfile . '.template';
- if (file_exists($source)) {
- $this->_source = $source;
- } else {
- throw new Components_Exception("No template at $source!");
- }
- $this->_target = $tdir . DIRECTORY_SEPARATOR . $tfile;
- }
-
- /**
- * Rewrite the template(s) from the source(s) to the target location(s).
+ * Rewrite the template from the source to the target location.
*
+ * @param string $source The source location.
+ * @param string $target The target location.
* @param array $parameters The template(s) parameters.
*
* @return NULL
*/
- public function write(array $parameters = array())
+ protected function writeSourceToTarget($source, $target, array $parameters = array())
{
- $source = file_get_contents($this->_source);
- file_put_contents($this->_target, vsprintf($source, $parameters));
+ $template = Components_Helper_Template::factory($source, $target)
+ ->write($parameters);
}
}
\ No newline at end of file
--- /dev/null
+<?php
+/**
+ * Components_Helper_Templates_Single:: converts a single template file into a
+ * target file.
+ *
+ * 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_Single:: converts a single template file into a
+ * target file.
+ *
+ * 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_Prefix
+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.
+ * @param string $sfile The exact template source file.
+ * @param string $tfile The exact template target file.
+ */
+ public function __construct($sdir, $tdir, $prefix, $tfile)
+ {
+ $template = null;
+ foreach (
+ new RecursiveIteratorIterator(
+ new RecursiveDirectoryIterator($sdir),
+ RecursiveIteratorIterator::CHILD_FIRST
+ )
+ as $file
+ ) {
+ if (strpos($file->getBasename(), $prefix) === 0) {
+ $template = $file;
+ break;
+ }
+ }
+ if (empty($template)) {
+ throw new Components_Exception(
+ sprintf(
+ 'No packaging template starting with "%s" was found in the template directory %s!',
+ $prefix,
+ $sdir
+ )
+ );
+ }
+ $this->_source = $template->getPathname();
+ $this->_target = $tdir . DIRECTORY_SEPARATOR . $tfile;
+ }
+
+ /**
+ * 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())
+ {
+ $this->writeSourceToTarget($this->_source, $this->_target, $parameters);
+ }
+}
\ No newline at end of file
--- /dev/null
+<?php
+/**
+ * Components_Helper_Templates_Single:: converts a single template file into a
+ * target file.
+ *
+ * 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_Single:: converts a single template file into a
+ * target file.
+ *
+ * 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_Single
+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.
+ * @param string $sfile The exact template source file.
+ * @param string $tfile The exact template target file.
+ */
+ public function __construct($sdir, $tdir, $sfile, $tfile)
+ {
+ $source = $sdir . DIRECTORY_SEPARATOR . $sfile . '.template';
+ if (file_exists($source)) {
+ $this->_source = $source;
+ } else {
+ throw new Components_Exception("No template at $source!");
+ }
+ $this->_target = $tdir . DIRECTORY_SEPARATOR . $tfile;
+ }
+
+ /**
+ * 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())
+ {
+ $this->writeSourceToTarget($this->_source, $this->_target, $parameters);
+ }
+}
\ No newline at end of file
);
}
- $build_template = new Components_Helper_Templates(
+ $build_template = new Components_Helper_Templates_Single(
$this->_config_application->getTemplateDirectory(),
$options['ciprebuild'],
'hudson-component-build.xml',
);
$build_template->write(array('toolsdir' => $options['toolsdir']));
- $phpunit_template = new Components_Helper_Templates(
+ $phpunit_template = new Components_Helper_Templates_Single(
$this->_config_application->getTemplateDirectory(),
$options['ciprebuild'],
'hudson-component-phpunit.xml',
$origin = basename($arguments[0]);
}
- $config_template = new Components_Helper_Templates(
+ $config_template = new Components_Helper_Templates_Single(
$this->_config_application->getTemplateDirectory(),
$options['cisetup'],
'hudson-component-config.xml',
$options = $this->_config->getOptions();
$arguments = $this->_config->getArguments();
- $template = null;
- foreach (
- new RecursiveIteratorIterator(
- new RecursiveDirectoryIterator(
- $this->_config_application->getTemplateDirectory()
- ),
- RecursiveIteratorIterator::CHILD_FIRST
- )
- as $file
- ) {
- if (strpos($file->getBasename(), 'distribute_') === 0) {
- $template = $file;
- break;
- }
- }
- if (empty($template)) {
- throw new Components_Exception(
- sprintf(
- 'No packaging template starting with "distribute_" was found in the template directory %s!',
- $this->_config_application->getTemplateDirectory()
- )
- );
- }
-
if (!isset($options['pearrc'])) {
$package = $this->_factory->createPackageForDefaultLocation(
$arguments[0] . DIRECTORY_SEPARATOR . 'package.xml'
$version = $package->getVersion() . 'dev' . strftime('%Y%m%d%H%M');
$package->generateSnapshot($version, dirname($options['distribute']));
- ob_start();
- include $template->getPathname();
- $packaging = ob_get_clean();
-
- file_put_contents(
- $options['distribute'],
- $packaging
+ $build_template = new Components_Helper_Templates_Prefix(
+ $this->_config_application->getTemplateDirectory(),
+ dirname($options['distribute']),
+ 'distribute_',
+ basename($options['distribute'])
+ );
+ $build_template->write(
+ array('package' => $package, 'version' => $version)
);
+
}
}
<active>yes</active>
</lead>
<date>2011-01-11</date>
- <time>05:29:34</time>
+ <time>11:29:20</time>
<version>
<release>0.0.1</release>
<api>0.0.1</api>
<file name="Pear.php" role="php" />
</dir> <!-- /lib/Components/Exception -->
<dir name="Helper">
+ <dir name="Template">
+ <file name="Php.php" role="php" />
+ <file name="Printf.php" role="php" />
+ </dir> <!-- /lib/Components/Helper/Template -->
+ <dir name="Templates">
+ <file name="Prefix.php" role="php" />
+ <file name="Single.php" role="php" />
+ </dir> <!-- /lib/Components/Helper/Templates -->
<file name="InstallationRun.php" role="php" />
<file name="ListRun.php" role="php" />
<file name="Root.php" role="php" />
+ <file name="Template.php" role="php" />
<file name="Templates.php" role="php" />
<file name="Tree.php" role="php" />
</dir> <!-- /lib/Components/Helper -->
<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" />
+ <file name="input.template" role="test" />
+ <file name="php.template" role="test" />
+ <file name="simple.template" role="test" />
+ <file name="variables.template" role="test" />
</dir> <!-- /test/Components/fixture/templates -->
</dir> <!-- /test/Components/fixture -->
<dir name="Integration">
<install as="Components/Helper/InstallationRun.php" name="lib/Components/Helper/InstallationRun.php" />
<install as="Components/Helper/ListRun.php" name="lib/Components/Helper/ListRun.php" />
<install as="Components/Helper/Root.php" name="lib/Components/Helper/Root.php" />
+ <install as="Components/Helper/Template.php" name="lib/Components/Helper/Template.php" />
<install as="Components/Helper/Templates.php" name="lib/Components/Helper/Templates.php" />
<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/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/Module/CiSetup.php" name="lib/Components/Module/CiSetup.php" />
<install as="Components/Module/Dependencies.php" name="lib/Components/Module/Dependencies.php" />
<install as="Components/fixture/templates/hudson-component-build.xml.template" name="test/Components/fixture/templates/hudson-component-build.xml.template" />
<install as="Components/fixture/templates/hudson-component-config.xml.template" name="test/Components/fixture/templates/hudson-component-config.xml.template" />
<install as="Components/fixture/templates/hudson-component-phpunit.xml.template" name="test/Components/fixture/templates/hudson-component-phpunit.xml.template" />
+ <install as="Components/fixture/templates/input.template" name="test/Components/fixture/templates/input.template" />
+ <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/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" />
public function testWrite()
{
$tdir = $this->getTemporaryDirectory();
- $templates = new Components_Helper_Templates(
+ $templates = new Components_Helper_Templates_Single(
dirname(__FILE__) . '/../../../fixture/templates',
$tdir,
'simple',
public function testSource()
{
$tdir = $this->getTemporaryDirectory();
- $templates = new Components_Helper_Templates(
+ $templates = new Components_Helper_Templates_Single(
dirname(__FILE__) . '/../../../fixture/templates',
$tdir,
'simple',
public function testMissingSource()
{
$source = dirname(__FILE__) . '/NO_SUCH_TEMPLATE';
- $templates = new Components_Helper_Templates($source, '');
+ $templates = new Components_Helper_Templates_Single($source, '', '', '');
}
public function testVariables()
{
$tdir = $this->getTemporaryDirectory();
- $templates = new Components_Helper_Templates(
+ $templates = new Components_Helper_Templates_Single(
dirname(__FILE__) . '/../../../fixture/templates',
$tdir,
'variables',
file_get_contents($tdir . DIRECTORY_SEPARATOR . 'target')
);
}
+
+ public function testPrefix()
+ {
+ $tdir = $this->getTemporaryDirectory();
+ $templates = new Components_Helper_Templates_Prefix(
+ dirname(__FILE__) . '/../../../fixture/templates',
+ $tdir,
+ 'var',
+ 'target'
+ );
+ $templates->write(array('1' => 'One', '2' => 'Two'));
+ $this->assertEquals(
+ "One : Two\n",
+ file_get_contents($tdir . DIRECTORY_SEPARATOR . 'target')
+ );
+ }
+
+ /**
+ * @expectedException Components_Exception
+ */
+ public function testMissingPrefixTemplate()
+ {
+ $tdir = $this->getTemporaryDirectory();
+ $templates = new Components_Helper_Templates_Prefix(
+ dirname(__FILE__) . '/../../../fixture/templates',
+ $tdir,
+ 'NOSUCHPREFIX',
+ 'target'
+ );
+ }
+
+ public function testPhp()
+ {
+ $tdir = $this->getTemporaryDirectory();
+ $templates = new Components_Helper_Templates_Single(
+ dirname(__FILE__) . '/../../../fixture/templates',
+ $tdir,
+ 'php',
+ 'target'
+ );
+ $templates->write();
+ $this->assertEquals(
+ "test",
+ file_get_contents($tdir . DIRECTORY_SEPARATOR . 'target')
+ );
+ }
+
+ public function testInput()
+ {
+ $tdir = $this->getTemporaryDirectory();
+ $templates = new Components_Helper_Templates_Single(
+ dirname(__FILE__) . '/../../../fixture/templates',
+ $tdir,
+ 'input',
+ 'target'
+ );
+ $templates->write(array('input' => 'SOME INPUT'));
+ $this->assertEquals(
+ "SOME INPUT",
+ file_get_contents($tdir . DIRECTORY_SEPARATOR . 'target')
+ );
+ }
}
\ No newline at end of file
--- /dev/null
+<?php
+
+echo $input;
--- /dev/null
+<?php
+
+echo "test";
--- /dev/null
+%1$s : %2$s