uw Thu Mar 29 13:49:15 2001 EDT Added files: /php4/pear/HTML IT_Error.php Modified files: /php4/pear/HTML IT.php ITX.php Log: - Pearified the error handling
Index: php4/pear/HTML/IT.php diff -u php4/pear/HTML/IT.php:1.4 php4/pear/HTML/IT.php:1.5 --- php4/pear/HTML/IT.php:1.4 Thu Feb 8 14:19:59 2001 +++ php4/pear/HTML/IT.php Thu Mar 29 13:49:15 2001 @@ -16,8 +16,11 @@ // | Authors: Ulf Wendel <[EMAIL PROTECTED]> | // +----------------------------------------------------------------------+ // -// $Id: IT.php,v 1.4 2001/02/08 22:19:59 chagenbu Exp $ +// $Id: IT.php,v 1.5 2001/03/29 21:49:15 uw Exp $ // + +require_once("HTML/IT_Error.php"); + /** * Integrated Template - IT * @@ -108,55 +111,40 @@ * $tpl->get(); * </code> * -* @author Ulf Wendel <[EMAIL PROTECTED]> -* @version $Id: IT.php,v 1.4 2001/02/08 22:19:59 chagenbu Exp $ -* @access public +* @author Ulf Wendel <[EMAIL PROTECTED]> +* @version $Id: IT.php,v 1.5 2001/03/29 21:49:15 uw Exp $ +* @access public +* @package IX[X] */ class IntegratedTemplate { /** * Contains the error objects - * @var array - * @access public - * @see halt(), $printError, $haltOnError + * @var array + * @access public + * @see halt(), $printError, $haltOnError */ var $err = array(); /** - * Print error messages? - * @var boolean - * @access public - * @see halt(), $haltOnError, $err - */ - var $printError = false; - - /** - * Call die() on error? - * @var boolean - * @access public - * @see halt(), $printError, $err - */ - var $haltOnError = false; - - /** * Clear cache on get()? - * @var boolean + * @var boolean */ var $clearCache = false; /** * First character of a variable placeholder ( _{_VARIABLE} ). - * @var string - * @access public - * @see $closingDelimiter, $blocknameRegExp, $variablenameRegExp + * @var string + * @access public + * @see $closingDelimiter, $blocknameRegExp, $variablenameRegExp */ var $openingDelimiter = "{"; /** * Last character of a variable placeholder ( {VARIABLE_}_ ). - * @var string - * @access public - * @see $openingDelimiter, $blocknameRegExp, $variablenameRegExp + * @var string + * @access public + * @see $openingDelimiter, $blocknameRegExp, $variablenameRegExp */ var $closingDelimiter = "}"; @@ -164,9 +152,9 @@ * RegExp matching a block in the template. * Per default "sm" is used as the regexp modifier, "i" is missing. * That means a case sensitive search is done. - * @var string - * @access public - * @see $variablenameRegExp, $openingDelimiter, $closingDelimiter + * @var string + * @access public + * @see $variablenameRegExp, $openingDelimiter, $closingDelimiter */ var $blocknameRegExp = "[0-9A-Za-z_-]+"; @@ -174,83 +162,83 @@ * RegExp matching a variable placeholder in the template. * Per default "sm" is used as the regexp modifier, "i" is missing. * That means a case sensitive search is done. - * @var string - * @access public - * @see $blocknameRegExp, $openingDelimiter, $closingDelimiter + * @var string + * @access public + * @see $blocknameRegExp, $openingDelimiter, $closingDelimiter */ var $variablenameRegExp = "[0-9A-Za-z_-]+"; /** * RegExp used to find variable placeholder, filled by the constructor. * @var string Looks somewhat like @(delimiter varname delimiter)@ - * @access public - * @see IntegratedTemplate() + * @access public + * @see IntegratedTemplate() */ var $variablesRegExp = ""; /** * RegExp used to strip unused variable placeholder. - * @brother $variablesRegExp + * @brother $variablesRegExp */ var $removeVariablesRegExp = ""; /** * Controls the handling of unknown variables, default is remove. - * @var boolean - * @access public + * @var boolean + * @access public */ var $removeUnknownVariables = true; /** * Controls the handling of empty blocks, default is remove. - * @var boolean - * @access public + * @var boolean + * @access public */ var $removeEmptyBlocks = true; /** * RegExp used to find blocks an their content, filled by the constructor. - * @var string - * @see IntegratedTemplate() + * @var string + * @see IntegratedTemplate() */ var $blockRegExp = ""; /** * Name of the current block. - * @var string + * @var string */ var $currentBlock = "__global__"; /** * Content of the template. - * @var string + * @var string */ var $template = ""; /** * Array of all blocks and their content. * - * @var array - * @see findBlocks() + * @var array + * @see findBlocks() */ var $blocklist = array(); /** * Array with the parsed content of a block. * - * @var array + * @var array */ var $blockdata = array(); /** * Array of variables in a block. - * @var array + * @var array */ var $blockvariables = array(); /** * Array of inner blocks of a block. - * @var array + * @var array */ var $blockinner = array(); @@ -276,7 +264,7 @@ * if they are empty. * * @var array $touchedBlocks - * @see touchBlock(), $removeEmptyBlocks + * @see touchBlock(), $removeEmptyBlocks */ var $touchedBlocks = array(); @@ -349,7 +337,7 @@ * class from this one. * * @param string File root directory, prefix for all filenames given to the object. - * @see setRoot() + * @see setRoot() */ function IntegratedTemplate($root = "") { @@ -373,7 +361,8 @@ * Returns a block with all replacements done. * * @param string name of the block - * @return string + * @return string + * @throws IT_Error * @access public * @see show() */ @@ -383,8 +372,8 @@ $this->parse("__global__"); if (!isset($this->blocklist[$block])) { - $this->halt("The block '$block' was not found in the template.", __FILE__, __LINE__); - return true; + new IT_Error("The block '$block' was not found in the template.", +__FILE__, __LINE__); + return ""; } if ($this->clearCache) { @@ -407,11 +396,12 @@ * @param string name of the block to be parsed * @access public * @see parseCurrentBlock() + * @throws IT_Error */ function parse($block = "__global__", $flag_recursion = false) { if (!isset($this->blocklist[$block])) { - $this->halt("The block '$block' was not found in the template.", __FILE__, __LINE__); + return new IT_Error("The block '$block' was not found in the template.", +__FILE__, __LINE__); return false; } @@ -457,11 +447,11 @@ $placeholder = $this->openingDelimiter . "__" . $innerblock . "__" . $this->closingDelimiter; $outer = str_replace($placeholder, $this->blockdata[$innerblock], $outer); $this->blockdata[$innerblock] = ""; - } + } - } + } - if ($this->removeUnknownVariables) + if ($this->removeUnknownVariables) $outer = preg_replace($this->removeVariablesRegExp, "", $outer); if ($empty) { @@ -502,7 +492,7 @@ * or with one array $variables["varname"] = "value" given setVariable($variables) * quite like phplib templates set_var(). * - * @param mixed string with the variable name or an array %variables["varname"] = "value" + * @param mixed string with the variable name or an array +%variables["varname"] = "value" * @param string value of the variable or empty if $variable is an array. * @param string prefix for variable names * @access public @@ -524,16 +514,15 @@ /** * Sets the name of the current block that is the block where variables are added. * - * @param string name of the block - * @return boolean false on failure, otherwise true - * @access public + * @param string name of the block + * @return boolean false on failure, otherwise true + * @throws IT_Error + * @access public */ function setCurrentBlock($block = "__global__") { - if (!isset($this->blocklist[$block])) { - $this->halt("Can't find the block '$block' in the template.", __FILE__, __LINE__); - return false; - } + if (!isset($this->blocklist[$block])) + return new IT_Error("Can't find the block '$block' in the template.", +__FILE__, __LINE__); $this->currentBlock = $block; @@ -543,17 +532,16 @@ /** * Preserves an empty block even if removeEmptyBlocks is true. * - * @param string name of the block - * @return boolean false on false, otherwise true - * @access public - * @see $removeEmptyBlocks + * @param string name of the block + * @return boolean false on false, otherwise true + * @throws IT_Error + * @access public + * @see $removeEmptyBlocks */ function touchBlock($block) { - if (!isset($this->blocklist[$block])) { - $this->halt("Can't find the block '$block' in the template.", __FILE__, __LINE__); - return false; - } + if (!isset($this->blocklist[$block])) + return new IT_Error("Can't find the block '$block' in the template.", +__FILE__, __LINE__); $this->touchedBlocks[$block] = true; @@ -567,8 +555,8 @@ * when a new template is given. Don't use this function * unless you know what you're doing. * - * @access public - * @see free() + * @access public + * @see free() */ function init() { @@ -583,8 +571,8 @@ * * Don't use this function unless you know what you're doing. * - * @access public - * @see init() + * @access public + * @see init() */ function free() { @@ -607,12 +595,11 @@ * You can eighter load a template file from disk with LoadTemplatefile() or set the * template manually using this function. * - * @param string template content - * @param boolean Unbekannte, nicht ersetzte Platzhalter entfernen? - * @param boolean remove unknown/unused variables? - * @param boolean remove empty blocks? - * @see LoadTemplatefile(), $template - * @access public + * @param string template content + * @param boolean remove unknown/unused variables? + * @param boolean remove empty blocks? + * @see LoadTemplatefile(), $template + * @access public */ function setTemplate($template, $removeUnknownVariables = true, $removeEmptyBlocks = true) { @@ -642,9 +629,9 @@ /** * Reads a template file from the disk. * - * @param string name of the template file - * @param bool How to handle unknown variables. - * @param bool How to handle empty blocks. + * @param string name of the template file + * @param bool how to handle unknown variables. + * @param bool how to handle empty blocks. * @access public * @return boolean false on failure, otherwise true * @see $template, setTemplate(), $removeUnknownVariables, $removeEmptyBlocks @@ -727,8 +714,7 @@ * Recusively builds a list of all blocks within the template. * * @param string string that gets scanned - * @access private - * @see $blocklist + * @see $blocklist */ function findBlocks($string) { @@ -742,7 +728,7 @@ $blockcontent = $match[2]; if (isset($this->blocklist[$blockname])) { - $this->halt("The name of a block must be unique within a template. Found '$blockname' twice. Unpredictable results may appear.", __FILE__, __LINE__); + new IT_Error("The name of a block must be unique within a +template. Found '$blockname' twice. Unpredictable results may appear.", __FILE__, +__LINE__); $this->flagBlocktrouble = true; } @@ -778,7 +764,7 @@ /** * Reads a file from disk and returns its content. * @param string Filename - * @return string Filecontent + * @return string Filecontent */ function getFile($filename) { @@ -788,7 +774,7 @@ $filename = $this->fileRoot . $filename; if (!($fh = @fopen($filename, "r"))) { - $this->halt("Can't read '$filename'.", __FILE__, __LINE__); + new IT_Error("Can't read '$filename'.", __FILE__, __LINE__); return ""; } @@ -797,32 +783,6 @@ return $content; } // end func getFile - - /** - * Error Handling function. - * @param string error message - * @param mixed File where the error occured - * @param int Line where the error occured - * @see $err - */ - function halt($message, $file = "", $line = 0) { - - - $message = sprintf("IntegratedTemplate Error: %s [File: %s, Line: %d]", - $message, - $file, - $line - ); - - $this->err[] = $message; - - if ($this->printError) - print $message; - - if ($this->haltOnError) - die($message); - - } // end func halt } // end class IntegratedTemplate ?> Index: php4/pear/HTML/ITX.php diff -u php4/pear/HTML/ITX.php:1.4 php4/pear/HTML/ITX.php:1.5 --- php4/pear/HTML/ITX.php:1.4 Wed Jan 24 03:19:28 2001 +++ php4/pear/HTML/ITX.php Thu Mar 29 13:49:15 2001 @@ -16,7 +16,7 @@ // | Authors: Ulf Wendel <[EMAIL PROTECTED]> | // +----------------------------------------------------------------------+ // -// $Id: ITX.php,v 1.4 2001/01/24 11:19:28 sbergmann Exp $ +// $Id: ITX.php,v 1.5 2001/03/29 21:49:15 uw Exp $ // require_once "HTML/IT.php"; @@ -33,9 +33,10 @@ * Note that you can replace an existing block and add new blocks add runtime. * Adding new blocks means changing a variable placeholder to a block. * -* @author Ulf Wendel <[EMAIL PROTECTED]> -* @access public -* @version $Id: ITX.php,v 1.4 2001/01/24 11:19:28 sbergmann Exp $ +* @author Ulf Wendel <[EMAIL PROTECTED]> +* @access public +* @version $Id: ITX.php,v 1.5 2001/03/29 21:49:15 uw Exp $ +* @package IT[X] */ class IntegratedTemplateExtension extends IntegratedTemplate { @@ -151,19 +152,16 @@ * * @param string Blockname * @param string Blockcontent - * @return boolean + * @return boolean + * @throws IT_Error * @see replaceBlockfile(), addBlock(), addBlockfile() * @access public */ function replaceBlock($block, $template) { - if (!isset($this->blocklist[$block])) { - $this->halt("The block '$block' does not exist in the template and thus it can't be replaced.", __FILE__, __LINE__); - return false; - } - if (""==$template) { - $this->halt("No block content given.", __FILE__, __LINE__); - return false; - } + if (!isset($this->blocklist[$block])) + return new IT_Error("The block '$block' does not exist in the template +and thus it can't be replaced.", __FILE__, __LINE__); + if ("" == $template) + return new IT_Error("No block content given.", __FILE__, __LINE__); print "This function has not been coded yet."; @@ -182,7 +180,7 @@ * @param string Name of the file that contains the blockcontent */ function replaceBlockfile($block, $filename) { - return $this->replaceBlock($block, $this->getFile($filename)); + return $this->replaceBlock($block, $this->getFile($filename)); } // end func replaceBlockfile /** @@ -207,33 +205,30 @@ * @param string Name of the variable placeholder, the name must be unique within the template. * @param string Name of the block to be added * @param string Content of the block - * @return boolean - * @see addBlockfile() - * @access public + * @return boolean + * @throws IT_Error + * @see addBlockfile() + * @access public */ function addBlock($placeholder, $blockname, $template) { // Don't trust any user even if it's a programmer or yourself... if ("" == $placeholder) { - $this->halt("No variable placeholder given.", __FILE__, __LINE__); - return false; + return new IT_Error("No variable placeholder given.", __FILE__, __LINE__); } else if ("" == $blockname || !preg_match($this->checkblocknameRegExp, $blockname) ) { print $this->checkblocknameRegExp; - $this->halt("No or invalid blockname '$blockname' given.", __FILE__, __LINE__); - return false; + return new IT_Error("No or invalid blockname '$blockname' given.", +__FILE__, __LINE__); } else if ("" == $template) { - $this->halt("No block content given.", __FILE__, __LINE__); - return false; + return new IT_Error("No block content given.", __FILE__, __LINE__); } else if (isset($this->blocklist[$blockname])) { - $this->halt("The block already exists.", __FILE__, __LINE__); - return false; + return new IT_Error("The block already exists.", __FILE__, __LINE__); } @@ -241,8 +236,7 @@ $parents = $this->findPlaceholderBlocks($placeholder); if (0 == count($parents)) { - $this->halt("The variable placeholder '$placeholder' was not found in the template.", __FILE__, __LINE__); - return false; + return new IT_Error("The variable placeholder '$placeholder' was not +found in the template.", __FILE__, __LINE__); } else if ( count($parents) > 1 ) { @@ -251,8 +245,7 @@ $msg .= "$parent, "; $msg = substr($parent, -2); - $this->halt("The variable placeholder '$placeholder' must be unique, found in multiple blocks '$msg'.", __FILE__, __LINE__); - return false; + return new IT_Error("The variable placeholder '$placeholder' must be +unique, found in multiple blocks '$msg'.", __FILE__, __LINE__); } @@ -292,17 +285,18 @@ * @param string Name of the block to scan. If left out (default) all blocks are scanned. * @return string Name of the (first) block that contains the specified placeholder. * If the placeholder was not found or an error occured an empty string is returned. + * @throws IT_Error * @access public */ function placeholderExists($placeholder, $block = "") { if ("" == $placeholder) { - $this->halt("No placeholder name given.", __FILE__, __LINE__); + new IT_Error("No placeholder name given.", __FILE__, __LINE__); return ""; } if ("" != $block && !isset($this->blocklist[$block])) { - $this->halt("Unknown block '$block'.", __FILE__, __LINE__); + new IT_Error("Unknown block '$block'.", __FILE__, __LINE__); return ""; } @@ -348,7 +342,7 @@ /** * Checks the list of function calls in the template and calls their callback function. * - * @access public + * @access public */ function performCallback() { @@ -382,8 +376,8 @@ /** * Returns a list of all function calls in the current template. * - * @return array - * @access public + * @return array + * @access public */ function getFunctioncalls() { @@ -394,9 +388,9 @@ /** * Replaces a function call with the given replacement. * - * @param int Function ID + * @param int Function ID * @param string Replacement - * @access public + * @access public */ function setFunctioncontent($functionID, $replacement) { @@ -410,15 +404,14 @@ * @param string Function name in the template * @param string Name of the callback function * @param string Name of the callback object - * @return boolean False on failure. - * @access public + * @return boolean False on failure. + * @throws IT_Error + * @access public */ function setCallbackFunction($tplfunction, $callbackfunction, $callbackobject = "") { - if ("" == $tplfunction || "" == $callbackfunction) { - $this->halt("No template function ('$tplfunction') and/or no callback function ('$callback') given.", __FILE__, __LINE__); - return false; - } + if ("" == $tplfunction || "" == $callbackfunction) + return new IT_Error("No template function ('$tplfunction') and/or no +callback function ('$callback') given.", __FILE__, __LINE__); $this->callback[$tplfunction] = array( "function" => $callbackfunction, Index: php4/pear/HTML/IT_Error.php +++ php4/pear/HTML/IT_Error.php <?php // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | [EMAIL PROTECTED] so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Ulf Wendel <[EMAIL PROTECTED]> | // +----------------------------------------------------------------------+ // // $Id: IT_Error.php,v 1.1 2001/03/29 21:49:15 uw Exp $ require_once "PEAR.php"; /** * IT[X] Error class * * @package IT[X] */ class IT_Error extends PEAR_Error { /** * Prefix of all error messages. * * @var string */ var $error_message_prefix = "IntegratedTemplate Error: "; /** * Creates an cache error object. * * @param string error message * @param string file where the error occured * @param string linenumber where the error occured */ function Cache_Error($msg, $file = __FILE__, $line = __LINE__) { $this->PEAR_Error(sprintf("%s [%s on line %d].", $msg, $file, $line)); } // end func Cache_Error } // end class Cache_Error ?>
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]