From: Gunnar Wrobel Date: Tue, 11 Jan 2011 10:31:16 +0000 (+0100) Subject: Complete the template machinery for the existing code. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=8bb9f9011448547c9264a759e4d02938a53aec18;p=horde.git Complete the template machinery for the existing code. --- diff --git a/components/lib/Components/Helper/Template.php b/components/lib/Components/Helper/Template.php new file mode 100644 index 000000000..7c0e1205f --- /dev/null +++ b/components/lib/Components/Helper/Template.php @@ -0,0 +1,82 @@ + + * @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 + * @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 == ' + * @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 + * @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 diff --git a/components/lib/Components/Helper/Template/Printf.php b/components/lib/Components/Helper/Template/Printf.php new file mode 100644 index 000000000..1f26742f3 --- /dev/null +++ b/components/lib/Components/Helper/Template/Printf.php @@ -0,0 +1,45 @@ + + * @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 + * @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 diff --git a/components/lib/Components/Helper/Templates.php b/components/lib/Components/Helper/Templates.php index 018d81798..842d11f4e 100644 --- a/components/lib/Components/Helper/Templates.php +++ b/components/lib/Components/Helper/Templates.php @@ -25,51 +25,20 @@ * @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 diff --git a/components/lib/Components/Helper/Templates/Prefix.php b/components/lib/Components/Helper/Templates/Prefix.php new file mode 100644 index 000000000..b1913d568 --- /dev/null +++ b/components/lib/Components/Helper/Templates/Prefix.php @@ -0,0 +1,94 @@ + + * @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 + * @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 diff --git a/components/lib/Components/Helper/Templates/Single.php b/components/lib/Components/Helper/Templates/Single.php new file mode 100644 index 000000000..68a2531be --- /dev/null +++ b/components/lib/Components/Helper/Templates/Single.php @@ -0,0 +1,77 @@ + + * @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 + * @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 diff --git a/components/lib/Components/Runner/CiPrebuild.php b/components/lib/Components/Runner/CiPrebuild.php index cf0c14f32..1314409f9 100644 --- a/components/lib/Components/Runner/CiPrebuild.php +++ b/components/lib/Components/Runner/CiPrebuild.php @@ -81,7 +81,7 @@ class Components_Runner_CiPrebuild ); } - $build_template = new Components_Helper_Templates( + $build_template = new Components_Helper_Templates_Single( $this->_config_application->getTemplateDirectory(), $options['ciprebuild'], 'hudson-component-build.xml', @@ -89,7 +89,7 @@ class Components_Runner_CiPrebuild ); $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', diff --git a/components/lib/Components/Runner/CiSetup.php b/components/lib/Components/Runner/CiSetup.php index 68009f09b..b947b3f9f 100644 --- a/components/lib/Components/Runner/CiSetup.php +++ b/components/lib/Components/Runner/CiSetup.php @@ -92,7 +92,7 @@ class Components_Runner_CiSetup $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', diff --git a/components/lib/Components/Runner/Distribute.php b/components/lib/Components/Runner/Distribute.php index 77e68d6b8..92af47388 100644 --- a/components/lib/Components/Runner/Distribute.php +++ b/components/lib/Components/Runner/Distribute.php @@ -74,30 +74,6 @@ class Components_Runner_Distribute $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' @@ -112,13 +88,15 @@ class Components_Runner_Distribute $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) ); + } } diff --git a/components/package.xml b/components/package.xml index a936a2c5b..b1fee86bf 100644 --- a/components/package.xml +++ b/components/package.xml @@ -25,7 +25,7 @@ yes 2011-01-11 - + 0.0.1 0.0.1 @@ -64,9 +64,18 @@ + + + + + + + + + @@ -200,6 +209,10 @@ + + + + @@ -307,8 +320,13 @@ + + + + + @@ -368,6 +386,10 @@ + + + + diff --git a/components/test/Components/Unit/Components/Helper/TemplatesTest.php b/components/test/Components/Unit/Components/Helper/TemplatesTest.php index 06b9f5baa..3186d80c2 100644 --- a/components/test/Components/Unit/Components/Helper/TemplatesTest.php +++ b/components/test/Components/Unit/Components/Helper/TemplatesTest.php @@ -38,7 +38,7 @@ extends Components_TestCase public function testWrite() { $tdir = $this->getTemporaryDirectory(); - $templates = new Components_Helper_Templates( + $templates = new Components_Helper_Templates_Single( dirname(__FILE__) . '/../../../fixture/templates', $tdir, 'simple', @@ -51,7 +51,7 @@ extends Components_TestCase public function testSource() { $tdir = $this->getTemporaryDirectory(); - $templates = new Components_Helper_Templates( + $templates = new Components_Helper_Templates_Single( dirname(__FILE__) . '/../../../fixture/templates', $tdir, 'simple', @@ -70,13 +70,13 @@ extends Components_TestCase 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', @@ -88,4 +88,66 @@ extends Components_TestCase 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 diff --git a/components/test/Components/fixture/templates/input.template b/components/test/Components/fixture/templates/input.template new file mode 100644 index 000000000..62df4daa1 --- /dev/null +++ b/components/test/Components/fixture/templates/input.template @@ -0,0 +1,3 @@ +