cvs: pear /HTML_Template_PHPLIB package.xml /HTML_Template_PHPLIB/HTML/Template/PHPLIB Generator.php Helper.php Tool.php Validator.php
/HTML_Template_PHPLIB/tests AllTests.php HTML_Template_PHPLIB_GeneratorTest.php HTML_Template_PHPLIB_HelperTest.php

From: Date: Wed, 03 Oct 2007 22:05:33 +0000
Subject: cvs: pear /HTML_Template_PHPLIB package.xml /HTML_Template_PHPLIB/HTML/Template/PHPLIB Generator.php Helper.php Tool.php Validator.php
/HTML_Template_PHPLIB/tests AllTests.php HTML_Template_PHPLIB_GeneratorTest.php HTML_Template_PHPLIB_HelperTest.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-48744@lists.php.net to get a copy of this message
cweiske Wed Oct 3 22:05:33 2007 UTC Added files: /pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB Generator.php Helper.php /pear/HTML_Template_PHPLIB/tests HTML_Template_PHPLIB_GeneratorTest.php HTML_Template_PHPLIB_HelperTest.php Modified files: /pear/HTML_Template_PHPLIB package.xml /pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB Tool.php Validator.php /pear/HTML_Template_PHPLIB/tests AllTests.php Log: html_template_phplibtool can generate PHP code from templates now. package.xml fixed so that tool gets installed into correct location

http://cvs.php.net/viewvc.cgi/pear/HTML_Template_PHPLIB/package.xml?r1=1.14&r2=1.15&diff_format=u Index: pear/HTML_Template_PHPLIB/package.xml diff -u pear/HTML_Template_PHPLIB/package.xml:1.14 pear/HTML_Template_PHPLIB/package.xml:1.15 --- pear/HTML_Template_PHPLIB/package.xml:1.14 Mon Oct 1 18:56:23 2007 +++ pear/HTML_Template_PHPLIB/package.xml Wed Oct 3 22:05:33 2007 @@ -36,9 +36,7 @@ </notes> <contents> <dir name="/"> - <dir name="bin"> - <file name="html_template_phplibtool" role="script" /> - </dir> + <file name="bin/html_template_phplibtool" role="script" install-as="html_template_phplibtool" /> <dir name="examples"> <file name="authors.php" role="doc" /> http://cvs.php.net/viewvc.cgi/pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Tool.php?r1=1.1&r2=1.2&diff_format=u Index: pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Tool.php diff -u pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Tool.php:1.1 pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Tool.php:1.2 --- pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Tool.php:1.1 Mon Oct 1 18:43:00 2007 +++ pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Tool.php Wed Oct 3 22:05:33 2007 @@ -4,7 +4,7 @@ * Command line tool to use the HTML_Template_PHPLIB validator and generator. * * @category HTML -* @package HTML_Template_PHPLIB +* @package HTML_Template_PHPLIB * @author Christian Weiske <cweiske@php.net> * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @link http://pear.php.net/package/HTML_Template_PHPLIB @@ -14,11 +14,32 @@ function HTML_Template_PHPLIB_Tool($args) { $strAction = $this->getAction($args); - $this->{'do' . ucfirst($strAction)}($args); + $this->{'do' . ucfirst($strAction)}($args); }//function HTML_Template_PHPLIB_Tool($args) - - - + + + + /** + * Start the tool + * + * @static + */ + function run() + { + $args = $GLOBALS['argv']; + array_shift($args); + new HTML_Template_PHPLIB_Tool($args); + }//function run() + + + + /** + * Returns the action to execute + * + * @param array &$args Array of command line arguments + * + * @return string Action to execute + */ function getAction(&$args) { if (count($args) == 0) { @@ -31,21 +52,40 @@ case '-v': case '--validate': return 'validate'; + case 'g': + case 'generate': + case '-g': + case '--generate': + return 'generate'; default: return 'help'; } }//function getAction(&$args) - - - + + + + /** + * Echoes the message and exist php with the given + * exit code + * + * @param string $strMessage Message to display + * @param int $nExitCode Exit code + * + * @return void + */ function dieHard($strMessage, $nExitCode) { echo $strMessage; exit($nExitCode); }//function dieHard($strMessage, $nExitCode) - - - + + + + /** + * Prints the help message to stdout + * + * @return void + */ function doHelp() { echo <<<EOT @@ -55,19 +95,27 @@ mode: (- and -- are optional) h, help Show this help screen + g, generate Generate PHP code for the template v, validate Validate a template file EOT; }//function doHelp() - - - + + + + /** + * Validates the files given on the cmdline + * + * @param array $args Command line arguments (files) + * + * @return void + */ function doValidate($args) { if (count($args) == 0) { $this->dieHard("No template files to validate\n", 1); } - + require_once 'HTML/Template/PHPLIB/Validator.php'; $nError = 0; foreach ($args as $file) { @@ -97,16 +145,26 @@ /** - * Start the tool + * Generates PHP code for the given template file * - * @static + * @param array $args Command line arguments + * + * @return void */ - function run() + function doGenerate($args) { - $args = $GLOBALS['argv']; - array_shift($args); - new HTML_Template_PHPLIB_Tool($args); - }//function run() + if (count($args) == 0) { + $this->dieHard("No template file given\n", 1); + } + + $strFile = $args[0]; + require_once 'HTML/Template/PHPLIB/Generator.php'; + $strCode = HTML_Template_PHPLIB_Generator::getCodeBlockDefinition( + $strFile + ); + + $this->dieHard($strCode, 0); + }//function doGenerate($args) }//class HTML_Template_PHPLIB_Tool http://cvs.php.net/viewvc.cgi/pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Validator.php?r1=1.4&r2=1.5&diff_format=u Index: pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Validator.php diff -u pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Validator.php:1.4 pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Validator.php:1.5 --- pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Validator.php:1.4 Mon Oct 1 18:43:00 2007 +++ pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Validator.php Wed Oct 3 22:05:33 2007 @@ -1,4 +1,5 @@ <?php +require_once 'HTML/Template/PHPLIB/Helper.php'; /** * Class to validate templates (syntax checks) @@ -32,13 +33,11 @@ */ function validate($strFile = null, $strContent = null) { - if ($strFile !== null) { - $strContent = file_get_contents($strFile); - } else if ($strContent === null) { + $arLines = HTML_Template_PHPLIB_Helper::getLines($strFile, $strContent); + if ($arLines === false) { return false; } - - $arErrors = HTML_Template_PHPLIB_Validator::checkBlockDefinitions($strContent); + $arErrors = HTML_Template_PHPLIB_Validator::checkBlockDefinitions($arLines); HTML_Template_PHPLIB_Validator::sortByLine($arErrors); @@ -51,7 +50,7 @@ * Check if all block definitions have a closing counterpart * and if the block comments have all the required spaces * - * @param string $strContent Template content + * @param array $arLines Array of template code lines * * @return array Array of errors/warnings. An error/warning is an array * of several keys: message, line @@ -59,17 +58,14 @@ * @protected * @static */ - function checkBlockDefinitions($strContent) + function checkBlockDefinitions($arLines) { - $arLines = explode( - "\n", str_replace(array("\r\n", "\r"), "\n", $strContent) - ); //Array of block definitions found. // key is the block name, value is an array of line numbers $arBlockOpen = array(); $arBlockClose = array(); $arErrors = array(); - + $strRegex = '/<!--(\s*)(BEGIN|END)(\s*)([a-zA-Z0-9_]*)(\s*)-->/'; foreach ($arLines as $nLine => $strLine) { if (preg_match($strRegex, $strLine, $arMatches)) { @@ -95,7 +91,7 @@ 'message' => 'Space missing between ' . $strType . ' and block name', 'line' => $nLine, 'code' => $strLine - ); + ); } if ($arMatches[4] == '') { //block name missing @@ -119,8 +115,8 @@ } } } - - + + /** * Check if all open blocks have a close counterpart */ @@ -162,14 +158,14 @@ ); } } - + //TODO: Check proper nesting - + return $arErrors; - }//function checkBlockDefinitions($strContent) - - - + }//function checkBlockDefinitions($arLines) + + + /** * Sorts the given error array by line numbers * @@ -184,9 +180,9 @@ } usort($arErrors, array(__CLASS__, 'intcmpLine')); }//function sortByLine(&$arErrors) - - - + + + /** * Compares the two error arrays by line number * http://cvs.php.net/viewvc.cgi/pear/HTML_Template_PHPLIB/tests/AllTests.php?r1=1.2&r2=1.3&diff_format=u Index: pear/HTML_Template_PHPLIB/tests/AllTests.php diff -u pear/HTML_Template_PHPLIB/tests/AllTests.php:1.2 pear/HTML_Template_PHPLIB/tests/AllTests.php:1.3 --- pear/HTML_Template_PHPLIB/tests/AllTests.php:1.2 Sun Jul 8 10:54:11 2007 +++ pear/HTML_Template_PHPLIB/tests/AllTests.php Wed Oct 3 22:05:33 2007 @@ -9,6 +9,8 @@ chdir(dirname(__FILE__)); require_once 'HTML_Template_PHPLIBTest.php'; +require_once 'HTML_Template_PHPLIB_GeneratorTest.php'; +require_once 'HTML_Template_PHPLIB_ValidatorTest.php'; class HTML_Template_PHPLIB_AllTests @@ -24,6 +26,8 @@ $suite = new PHPUnit_Framework_TestSuite('HTML_Template_PHPLIB Tests'); /** Add testsuites, if there is. */ $suite->addTestSuite('HTML_Template_PHPLIBTest'); + $suite->addTestSuite('HTML_Template_PHPLIB_ValidatorTest'); + $suite->addTestSuite('HTML_Template_PHPLIB_GeneratorTest'); return $suite; } http://cvs.php.net/viewvc.cgi/pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Generator.php?view=markup&rev=1.1 Index: pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Generator.php +++ pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Generator.php <?php require_once 'HTML/Template/PHPLIB/Helper.php'; /** * Generates code to be used with templates * * @category HTML * @package HTML_Template_PHPLIB * @author Christian Weiske <cweiske@php.net> * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @link http://pear.php.net/package/HTML_Template_PHPLIB */ class HTML_Template_PHPLIB_Generator { /** * Creates the code to use a given template file * * @return string PHP code */ function getCodeBlockDefinition($strFile, $strTplName = null, $strPrefix = '$tpl') { $arBlocks = HTML_Template_PHPLIB_Generator::getBlocks( HTML_Template_PHPLIB_Helper::getLines($strFile) ); if ($strTplName === null) { $strTplName = HTML_Template_PHPLIB_Generator::getTemplateNameFromFilename($strFile); } $nl = "\r\n"; $code = ''; $code .= $strPrefix . ' = new HTML_Template_PHPLIB();' . $nl; $code .= HTML_Template_PHPLIB_Generator::getCodeBlock($arBlocks, $strTplName, $strPrefix); $code .= $nl; $code .= '//TODO: do something with the code' . $nl; $code .= $nl; $code .= $strPrefix . '->finish(' . trim($strPrefix) . "->parse('TMP', '" . $strTplName . "'));" . $nl; return $code; }//function getCodeBlockDefinition($strFile, $strTplName = null, $strPrefix = '$tpl') /** * Creates the PHP code for the given array of blocks. * * @param array $arBlocks Array of blocks, see getBlocks() * @param string $strTplName Template reference name * @param string $strPrefix Prefix to prepend before the code * * @return string PHP code */ function getCodeBlock($arBlocks, $strTplName, $strPrefix = '$tpl') { $nl = "\r\n"; $code = ''; foreach ($arBlocks as $arBlock) { if (count($arBlock['sub']) > 0) { $code .= HTML_Template_PHPLIB_Generator::getCodeBlock( $arBlock['sub'], $strTplName, $strPrefix ); } $code .= $strPrefix . "->set_block('" . $strTplName . "','" . $arBlock['name'] . "', '" . $arBlock['name'] . "_ref');" . $nl; } return $code; }//function getCodeBlock($arBlocks, $strTplName, $strPrefix = '$tpl') /** * Returns an array of blocks in the given template code. * The array values are array with a key "name" and * "sub", an array of nested blocks. * * @param string $strContent Template code * * @return array Array of blocks * * @static * @protected */ function getBlocks($arLines) { $arBlocks = array(); $arRefs = array(); $strRegex = '/<!--\s+(BEGIN|END)\s+([a-zA-Z0-9_]*)\s+-->/'; foreach ($arLines as $nLine => $strLine) { if (!preg_match($strRegex, $strLine, $arMatches)) { continue; } $strType = $arMatches[1]; $strBlockName = $arMatches[2]; if ($strType == 'BEGIN') { $arBlock = array( 'name' => $strBlockName, 'sub' => array() ); if (count($arRefs) == 0) { $arBlocks[$arBlock['name']] = $arBlock; $arRefs[$strBlockName] = &$arBlocks[$arBlock['name']]; } else { end($arRefs); $strOldBlock = key($arRefs); $arRefs[$strOldBlock]['sub'][$strBlockName] = $arBlock; $arRefs[$strBlockName] =& $arRefs[$strOldBlock]['sub'][$strBlockName]; } } else { unset($arRefs[$strBlockName]); } } return $arBlocks; }//function getBlocks($arLines) /** * Creates a name that can be used as handle for a template, * from the given file name. * * @param string $strFile File name * * @return string Template name */ function getTemplateNameFromFilename($strFile) { $strTplName = basename($strFile); //remove extension $nDotPos = strpos($strTplName, '.'); if ($nDotPos !== false) { $strTplName = substr($strTplName, 0, $nDotPos); } return $strTplName; }//function getTemplateNameFromFilename($strFile) }//class HTML_Template_PHPLIB_Generator ?> http://cvs.php.net/viewvc.cgi/pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Helper.php?view=markup&rev=1.1 Index: pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Helper.php +++ pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Helper.php <?php /** * Helper methods for the HTML_Template_PHPLIB tool * * @category HTML * @package HTML_Template_PHPLIB * @author Christian Weiske <cweiske@php.net> * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @link http://pear.php.net/package/HTML_Template_PHPLIB */ class HTML_Template_PHPLIB_Helper { /** * Returns an array with all lines of the text. * Extracts it from the file or the text * * @param string $strFile File name * @param string $strContent Template code * * @return array Array with text lines, without trailing newlines * * @static */ function getLines($strFile = null, $strContent = null) { if ($strContent !== null) { $arLines = HTML_Template_PHPLIB_Helper::splitLines($strContent); } else if ($strFile !== null) { $arLines = file($strFile, FILE_IGNORE_NEW_LINES); } return $arLines; }//function getLines($strFile = null, $strContent = null) /** * Splits the content into single lines and returns * the array. * Similar to file(), but works directly on the content * instead of the file name. * * @param string $strContent File content to be split into lines * * @return array Array of line strings without trailing newlines * * @static */ function splitLines($strContent) { return explode( "\n", str_replace(array("\r\n", "\r"), "\n", $strContent) ); }//function splitLines($strContent) }//class HTML_Template_PHPLIB_Helper ?> http://cvs.php.net/viewvc.cgi/pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_GeneratorTest.php?view=markup&rev=1.1 Index: pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_GeneratorTest.php +++ pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_GeneratorTest.php <?php // Call HTML_Template_PHPLIB_GeneratorTest::main() if this source file is executed directly. if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'HTML_Template_PHPLIB_GeneratorTest::main'); } require_once 'PHPUnit/Framework.php'; require_once 'HTML/Template/PHPLIB/Generator.php'; /** * Test class for HTML_Template_PHPLIB_Generator. * Generated by PHPUnit on 2007-10-03 at 21:44:43. */ class HTML_Template_PHPLIB_GeneratorTest extends PHPUnit_Framework_TestCase { /** * Runs the test methods of this class. * * @access public * @static */ public static function main() { require_once 'PHPUnit/TextUI/TestRunner.php'; $suite = new PHPUnit_Framework_TestSuite('HTML_Template_PHPLIB_GeneratorTest'); $result = PHPUnit_TextUI_TestRunner::run($suite); } /** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. * * @access protected */ protected function setUp() { } /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. * * @access protected */ protected function tearDown() { } /** */ public function testGetBlocks() { $strTemplate = <<<EOT <!-- BEGIN one --> <!-- BEGIN three --> <!-- BEGIN four --> <!-- BEGIN six --> <!-- END six --> <!-- END four --> <!-- END three --> <!-- BEGIN five --> <!-- END five --> <!-- END one --> <!-- BEGIN two --> <!-- END two --> EOT; $arBlocks = HTML_Template_PHPLIB_Generator::getBlocks( HTML_Template_PHPLIB_Helper::splitLines($strTemplate) ); $this->assertEquals(2, count($arBlocks)); $this->assertArrayHasKey('one', $arBlocks); $this->assertArrayHasKey('two', $arBlocks); $this->assertEquals(2, count($arBlocks['one']['sub'])); $this->assertArrayHasKey('three', $arBlocks['one']['sub']); $this->assertArrayHasKey('five', $arBlocks['one']['sub']); $this->assertArrayHasKey('four', $arBlocks['one']['sub']['three']['sub']); }//public function testGetBlocks() public function testGetTemplateNameFromFilename() { $this->assertEquals( 'some', HTML_Template_PHPLIB_Generator::getTemplateNameFromFilename( 'some.tpl' ) ); $this->assertEquals( 'order', HTML_Template_PHPLIB_Generator::getTemplateNameFromFilename( '/path/to/templates/order.mail' ) ); /* //does not work on unix $this->assertEquals( 'mytemplate', HTML_Template_PHPLIB_Generator::getTemplateNameFromFilename( 'C:\\mytemplate.ihtml' ) ); */ }//public function testGetTemplateNameFromFilename() } // Call HTML_Template_PHPLIB_GeneratorTest::main() if this source file is executed directly. if (PHPUnit_MAIN_METHOD == 'HTML_Template_PHPLIB_GeneratorTest::main') { HTML_Template_PHPLIB_GeneratorTest::main(); } ?> http://cvs.php.net/viewvc.cgi/pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_HelperTest.php?view=markup&rev=1.1 Index: pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_HelperTest.php +++ pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_HelperTest.php <?php // Call HTML_Template_PHPLIB_HelperTest::main() if this source file is executed directly. if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'HTML_Template_PHPLIB_HelperTest::main'); } require_once 'PHPUnit/Framework.php'; require_once 'HTML/Template/PHPLIB/Helper.php'; /** * Test class for HTML_Template_PHPLIB_Helper. * Generated by PHPUnit on 2007-10-03 at 23:46:21. */ class HTML_Template_PHPLIB_HelperTest extends PHPUnit_Framework_TestCase { /** * Runs the test methods of this class. * * @access public * @static */ public static function main() { require_once 'PHPUnit/TextUI/TestRunner.php'; $suite = new PHPUnit_Framework_TestSuite('HTML_Template_PHPLIB_HelperTest'); $result = PHPUnit_TextUI_TestRunner::run($suite); } /** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. * * @access protected */ protected function setUp() { } /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. * * @access protected */ protected function tearDown() { } /** */ public function testGetLines() { //test content $strContent = <<<EOT 1 22 333 44 5 EOT; $this->assertEquals( array('1', '22', '333', '44', '5'), HTML_Template_PHPLIB_Helper::getLines(null, $strContent) ); //TODO: test file loading } /** */ public function testSplitLines() { $strContent = <<<EOT 1 22 333 44 5 EOT; $this->assertEquals( array('1', '22', '333', '44', '5'), HTML_Template_PHPLIB_Helper::splitLines($strContent) ); } } // Call HTML_Template_PHPLIB_HelperTest::main() if this source file is executed directly. if (PHPUnit_MAIN_METHOD == 'HTML_Template_PHPLIB_HelperTest::main') { HTML_Template_PHPLIB_HelperTest::main(); } ?>
« previous php.pear.cvs (#48744) next »