cvs: pear /HTML_Template_PHPLIB/HTML/Template/PHPLIB Tool.php Validator.php /HTML_Template_PHPLIB/bin html_template_phplibtool
/HTML_Template_PHPLIB/examples broken.tpl /HTML_Template_PHPLIB/tests HTML_Template_PHPLIB_ValidatorTest.php
| From: | Christian Weiske | Date: | Mon, 01 Oct 2007 18:43:01 +0000 |
| Subject: | cvs: pear /HTML_Template_PHPLIB/HTML/Template/PHPLIB Tool.php Validator.php /HTML_Template_PHPLIB/bin html_template_phplibtool /HTML_Template_PHPLIB/examples broken.tpl /HTML_Template_PHPLIB/tests HTML_Template_PHPLIB_ValidatorTest.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-48740@lists.php.net to get a copy of this message | ||
cweiske Mon Oct 1 18:43:01 2007 UTC
Added files:
/pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB Tool.php
/pear/HTML_Template_PHPLIB/bin html_template_phplibtool
/pear/HTML_Template_PHPLIB/examples broken.tpl
Modified files:
/pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB Validator.php
/pear/HTML_Template_PHPLIB/tests
HTML_Template_PHPLIB_ValidatorTest.php
Log:
Introducing the html_template_phplibtool to validate template files
on the command line. Helps to look for errors in your html templates.
http://cvs.php.net/viewvc.cgi/pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Validator.php?r1=1.3&r2=1.4&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.3 pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Validator.php:1.4 --- pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Validator.php:1.3 Mon Oct 1 17:10:57 2007 +++ pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Validator.php Mon Oct 1 18:43:00 2007 @@ -12,6 +12,42 @@ class HTML_Template_PHPLIB_Validator { /** + * Validates a template file. + * You can pass either a file name, or the file content. One of the parameters + * needs to be !== null. + * + * @param string $strFile Template file name to check + * @param string $strContent Template content to check + * + * @return mixed Boolean true if no errors have been found, array of + * errors otherwise. An error is an array with keys + * - 'short' (short error code, string) + * - 'message' (readable message) + * - 'line' (line number) + * - 'code' (code that caused the error) + * false if no file and content is given + * + * @public + * @static + */ + function validate($strFile = null, $strContent = null) + { + if ($strFile !== null) { + $strContent = file_get_contents($strFile); + } else if ($strContent === null) { + return false; + } + + $arErrors = HTML_Template_PHPLIB_Validator::checkBlockDefinitions($strContent); + + HTML_Template_PHPLIB_Validator::sortByLine($arErrors); + + return count($arErrors) == 0 ? true : $arErrors; + }//function validate($strFile = null, $strContent = null) + + + + /** * Check if all block definitions have a closing counterpart * and if the block comments have all the required spaces * @@ -20,6 +56,7 @@ * @return array Array of errors/warnings. An error/warning is an array * of several keys: message, line * + * @protected * @static */ function checkBlockDefinitions($strContent) @@ -130,6 +167,38 @@ return $arErrors; }//function checkBlockDefinitions($strContent) + + + + /** + * Sorts the given error array by line numbers + * + * @param array &$arErrors Error array + * + * @return void + */ + function sortByLine(&$arErrors) + { + if (!is_array($arErrors)) { + return; + } + usort($arErrors, array(__CLASS__, 'intcmpLine')); + }//function sortByLine(&$arErrors) + + + + /** + * Compares the two error arrays by line number + * + * @param array $arA Error array one + * @param array $arB Error array two + * + * @return integer -1, 0 or 1 if $arA is smaller, equal or bigger than $arB + */ + function intcmpLine($arA, $arB) + { + return $arA['line'] - $arB['line']; + }//function intcmpLine($arA, $arB) }//class HTML_Template_PHPLIB_Validator http://cvs.php.net/viewvc.cgi/pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_ValidatorTest.php?r1=1.2&r2=1.3&diff_format=u Index: pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_ValidatorTest.php diff -u pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_ValidatorTest.php:1.2 pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_ValidatorTest.php:1.3 --- pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_ValidatorTest.php:1.2 Mon Oct 1 17:02:58 2007 +++ pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_ValidatorTest.php Mon Oct 1 18:43:00 2007 @@ -43,10 +43,32 @@ */ protected function tearDown() { } + + + + public function testValidate() + { + $this->assertFalse(HTML_Template_PHPLIB_Validator::validate()); + $this->assertTrue(HTML_Template_PHPLIB_Validator::validate(null, '')); + + $arErrors = HTML_Template_PHPLIB_Validator::validate(null, '<!-- BEGIN block -->'); + $this->assertType('array', $arErrors); + $this->assertEquals(1, count($arErrors)); + + $name = tempnam('/tmp', 'HTML_Template_PHPLIB-test'); + file_put_contents($name, '<!-- BEGIN blo -->'); + $arErrors = HTML_Template_PHPLIB_Validator::validate($name); + $this->assertType('array', $arErrors); + $this->assertEquals(1, count($arErrors)); + unlink($name); + }//public function testValidate() + + /** */ - public function testCheckBlockDefinitions() { + public function testCheckBlockDefinitions() + { $cont = <<<EOT <!-- BEGIN one --> <!-- END one --> @@ -60,6 +82,7 @@ <!--END five--> <!--BEGINsix--> <!--ENDsix--> +<!-- BEGIN --> EOT; $arErrors = HTML_Template_PHPLIB_Validator::checkBlockDefinitions($cont); $arErrors = self::stripMessages($arErrors); @@ -146,6 +169,16 @@ 'line' => 12, 'code' => '<!--ENDsix-->', ), + array ( + 'short' => 'MISSING_BLOCK_NAME', + 'line' => 13, + 'code' => '<!-- BEGIN -->' + ), + array ( + 'short' => 'MISSING_SPACE', + 'line' => 13, + 'code' => '<!-- BEGIN -->', + ) ), $arErrors ); http://cvs.php.net/viewvc.cgi/pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Tool.php?view=markup&rev=1.1 Index: pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Tool.php +++ pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Tool.php <?php /** * Command line tool to use the HTML_Template_PHPLIB validator and generator. * * @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_Tool { function HTML_Template_PHPLIB_Tool($args) { $strAction = $this->getAction($args); $this->{'do' . ucfirst($strAction)}($args); }//function HTML_Template_PHPLIB_Tool($args) function getAction(&$args) { if (count($args) == 0) { return 'help'; } $arg = array_shift($args); switch ($arg) { case 'v': case 'validate': case '-v': case '--validate': return 'validate'; default: return 'help'; } }//function getAction(&$args) function dieHard($strMessage, $nExitCode) { echo $strMessage; exit($nExitCode); }//function dieHard($strMessage, $nExitCode) function doHelp() { echo <<<EOT Usage: html_template_phplibtool action parameters Tool to validate and work with HTML templates mode: (- and -- are optional) h, help Show this help screen v, validate Validate a template file EOT; }//function doHelp() 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) { if (file_exists($file)) { $arErrors = HTML_Template_PHPLIB_Validator::validate($file); if ($arErrors === true) { echo 'No errors found in ' . $file . "\n"; $nError = 0; } else if ($arErrors === false) { echo 'Some unexpected error in ' . $file . "\n"; $nError = 3; } else { echo count($arErrors) . ' errors in ' . $file . "\n"; foreach ($arErrors as $arError) { echo ' Line #' . $arError['line'] . ': ' . $arError['message'] . "\n"; } $nError = 10; } } else { echo 'File does not exist: ' . $file . "\n"; $nError = 4; } } $this->dieHard('', $nError); }//function doValidate($args) /** * Start the tool * * @static */ function run() { $args = $GLOBALS['argv']; array_shift($args); new HTML_Template_PHPLIB_Tool($args); }//function run() }//class HTML_Template_PHPLIB_Tool ?> http://cvs.php.net/viewvc.cgi/pear/HTML_Template_PHPLIB/examples/broken.tpl?view=markup&rev=1.1 Index: pear/HTML_Template_PHPLIB/examples/broken.tpl +++ pear/HTML_Template_PHPLIB/examples/broken.tpl <html> <head> <!-- BEGIN meta --> <meta /> <!-- END met --> </head> <body> <!--BEGIN something --> <table> <!-- BEGIN row --> <tr><td>{CELL}</td></tr> </table> <!-- END something --> </body> </html>
http://cvs.php.net/viewvc.cgi/pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Validator.php?r1=1.3&r2=1.4&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.3 pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Validator.php:1.4 --- pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Validator.php:1.3 Mon Oct 1 17:10:57 2007 +++ pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Validator.php Mon Oct 1 18:43:00 2007 @@ -12,6 +12,42 @@ class HTML_Template_PHPLIB_Validator { /** + * Validates a template file. + * You can pass either a file name, or the file content. One of the parameters + * needs to be !== null. + * + * @param string $strFile Template file name to check + * @param string $strContent Template content to check + * + * @return mixed Boolean true if no errors have been found, array of + * errors otherwise. An error is an array with keys + * - 'short' (short error code, string) + * - 'message' (readable message) + * - 'line' (line number) + * - 'code' (code that caused the error) + * false if no file and content is given + * + * @public + * @static + */ + function validate($strFile = null, $strContent = null) + { + if ($strFile !== null) { + $strContent = file_get_contents($strFile); + } else if ($strContent === null) { + return false; + } + + $arErrors = HTML_Template_PHPLIB_Validator::checkBlockDefinitions($strContent); + + HTML_Template_PHPLIB_Validator::sortByLine($arErrors); + + return count($arErrors) == 0 ? true : $arErrors; + }//function validate($strFile = null, $strContent = null) + + + + /** * Check if all block definitions have a closing counterpart * and if the block comments have all the required spaces * @@ -20,6 +56,7 @@ * @return array Array of errors/warnings. An error/warning is an array * of several keys: message, line * + * @protected * @static */ function checkBlockDefinitions($strContent) @@ -130,6 +167,38 @@ return $arErrors; }//function checkBlockDefinitions($strContent) + + + + /** + * Sorts the given error array by line numbers + * + * @param array &$arErrors Error array + * + * @return void + */ + function sortByLine(&$arErrors) + { + if (!is_array($arErrors)) { + return; + } + usort($arErrors, array(__CLASS__, 'intcmpLine')); + }//function sortByLine(&$arErrors) + + + + /** + * Compares the two error arrays by line number + * + * @param array $arA Error array one + * @param array $arB Error array two + * + * @return integer -1, 0 or 1 if $arA is smaller, equal or bigger than $arB + */ + function intcmpLine($arA, $arB) + { + return $arA['line'] - $arB['line']; + }//function intcmpLine($arA, $arB) }//class HTML_Template_PHPLIB_Validator http://cvs.php.net/viewvc.cgi/pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_ValidatorTest.php?r1=1.2&r2=1.3&diff_format=u Index: pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_ValidatorTest.php diff -u pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_ValidatorTest.php:1.2 pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_ValidatorTest.php:1.3 --- pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_ValidatorTest.php:1.2 Mon Oct 1 17:02:58 2007 +++ pear/HTML_Template_PHPLIB/tests/HTML_Template_PHPLIB_ValidatorTest.php Mon Oct 1 18:43:00 2007 @@ -43,10 +43,32 @@ */ protected function tearDown() { } + + + + public function testValidate() + { + $this->assertFalse(HTML_Template_PHPLIB_Validator::validate()); + $this->assertTrue(HTML_Template_PHPLIB_Validator::validate(null, '')); + + $arErrors = HTML_Template_PHPLIB_Validator::validate(null, '<!-- BEGIN block -->'); + $this->assertType('array', $arErrors); + $this->assertEquals(1, count($arErrors)); + + $name = tempnam('/tmp', 'HTML_Template_PHPLIB-test'); + file_put_contents($name, '<!-- BEGIN blo -->'); + $arErrors = HTML_Template_PHPLIB_Validator::validate($name); + $this->assertType('array', $arErrors); + $this->assertEquals(1, count($arErrors)); + unlink($name); + }//public function testValidate() + + /** */ - public function testCheckBlockDefinitions() { + public function testCheckBlockDefinitions() + { $cont = <<<EOT <!-- BEGIN one --> <!-- END one --> @@ -60,6 +82,7 @@ <!--END five--> <!--BEGINsix--> <!--ENDsix--> +<!-- BEGIN --> EOT; $arErrors = HTML_Template_PHPLIB_Validator::checkBlockDefinitions($cont); $arErrors = self::stripMessages($arErrors); @@ -146,6 +169,16 @@ 'line' => 12, 'code' => '<!--ENDsix-->', ), + array ( + 'short' => 'MISSING_BLOCK_NAME', + 'line' => 13, + 'code' => '<!-- BEGIN -->' + ), + array ( + 'short' => 'MISSING_SPACE', + 'line' => 13, + 'code' => '<!-- BEGIN -->', + ) ), $arErrors ); http://cvs.php.net/viewvc.cgi/pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Tool.php?view=markup&rev=1.1 Index: pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Tool.php +++ pear/HTML_Template_PHPLIB/HTML/Template/PHPLIB/Tool.php <?php /** * Command line tool to use the HTML_Template_PHPLIB validator and generator. * * @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_Tool { function HTML_Template_PHPLIB_Tool($args) { $strAction = $this->getAction($args); $this->{'do' . ucfirst($strAction)}($args); }//function HTML_Template_PHPLIB_Tool($args) function getAction(&$args) { if (count($args) == 0) { return 'help'; } $arg = array_shift($args); switch ($arg) { case 'v': case 'validate': case '-v': case '--validate': return 'validate'; default: return 'help'; } }//function getAction(&$args) function dieHard($strMessage, $nExitCode) { echo $strMessage; exit($nExitCode); }//function dieHard($strMessage, $nExitCode) function doHelp() { echo <<<EOT Usage: html_template_phplibtool action parameters Tool to validate and work with HTML templates mode: (- and -- are optional) h, help Show this help screen v, validate Validate a template file EOT; }//function doHelp() 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) { if (file_exists($file)) { $arErrors = HTML_Template_PHPLIB_Validator::validate($file); if ($arErrors === true) { echo 'No errors found in ' . $file . "\n"; $nError = 0; } else if ($arErrors === false) { echo 'Some unexpected error in ' . $file . "\n"; $nError = 3; } else { echo count($arErrors) . ' errors in ' . $file . "\n"; foreach ($arErrors as $arError) { echo ' Line #' . $arError['line'] . ': ' . $arError['message'] . "\n"; } $nError = 10; } } else { echo 'File does not exist: ' . $file . "\n"; $nError = 4; } } $this->dieHard('', $nError); }//function doValidate($args) /** * Start the tool * * @static */ function run() { $args = $GLOBALS['argv']; array_shift($args); new HTML_Template_PHPLIB_Tool($args); }//function run() }//class HTML_Template_PHPLIB_Tool ?> http://cvs.php.net/viewvc.cgi/pear/HTML_Template_PHPLIB/examples/broken.tpl?view=markup&rev=1.1 Index: pear/HTML_Template_PHPLIB/examples/broken.tpl +++ pear/HTML_Template_PHPLIB/examples/broken.tpl <html> <head> <!-- BEGIN meta --> <meta /> <!-- END met --> </head> <body> <!--BEGIN something --> <table> <!-- BEGIN row --> <tr><td>{CELL}</td></tr> </table> <!-- END something --> </body> </html>