cvs: pear /HTML_Template_IT IT.php ITX.php

From: Date: Fri, 28 Oct 2005 09:40:27 +0000
Subject: cvs: pear /HTML_Template_IT IT.php ITX.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-35918@lists.php.net to get a copy of this message
dufuz Fri Oct 28 05:40:27 2005 EDT Modified files: /pear/HTML_Template_IT IT.php ITX.php Log: fixing a lot of mishaps regarding CS, missing {}, { in wrong places and so on.

http://cvs.php.net/diff.php/pear/HTML_Template_IT/IT.php?r1=1.9&r2=1.10&ty=u Index: pear/HTML_Template_IT/IT.php diff -u pear/HTML_Template_IT/IT.php:1.9 pear/HTML_Template_IT/IT.php:1.10 --- pear/HTML_Template_IT/IT.php:1.9 Sun Aug 14 14:27:03 2005 +++ pear/HTML_Template_IT/IT.php Fri Oct 28 05:40:22 2005 @@ -16,16 +16,16 @@ // | Author: Ulf Wendel <ulf.wendel@phpdoc.de> | // +----------------------------------------------------------------------+ // -// $Id: IT.php,v 1.9 2005/08/14 18:27:03 pajoye Exp $ +// $Id: IT.php,v 1.10 2005/10/28 09:40:22 dufuz Exp $ // -require_once('PEAR.php'); +require_once 'PEAR.php'; -define("IT_OK", 1); -define("IT_ERROR", -1); -define("IT_TPL_NOT_FOUND", -2); -define("IT_BLOCK_NOT_FOUND", -3); -define("IT_BLOCK_DUPLICATE", -4); +define('IT_OK', 1); +define('IT_ERROR', -1); +define('IT_TPL_NOT_FOUND', -2); +define('IT_BLOCK_NOT_FOUND', -3); +define('IT_BLOCK_DUPLICATE', -4); define('IT_UNKNOWN_OPTION', -6); /** * Integrated Template - IT @@ -118,7 +118,7 @@ * </code> * * @author Ulf Wendel <uw@netuse.de> - * @version $Id: IT.php,v 1.9 2005/08/14 18:27:03 pajoye Exp $ + * @version $Id: IT.php,v 1.10 2005/10/28 09:40:22 dufuz Exp $ * @access public * @package HTML_Template_IT */ @@ -180,13 +180,13 @@ * @access public * @see IntegratedTemplate() */ - var $variablesRegExp = ""; + var $variablesRegExp = ''; /** * RegExp used to strip unused variable placeholder. * @brother $variablesRegExp */ - var $removeVariablesRegExp = ""; + var $removeVariablesRegExp = ''; /** * Controls the handling of unknown variables, default is remove. @@ -207,7 +207,7 @@ * @var string * @see IntegratedTemplate() */ - var $blockRegExp = ""; + var $blockRegExp = ''; /** * Name of the current block. @@ -219,7 +219,7 @@ * Content of the template. * @var string */ - var $template = ""; + var $template = ''; /** * Array of all blocks and their content. @@ -246,7 +246,7 @@ * Array of inner blocks of a block. * @var array */ - var $blockinner = array(); + var $blockinner = array(); /** * List of blocks to preverse even if they are "empty". @@ -311,7 +311,7 @@ * @var string * @see HTML_Template_IT(), setRoot() */ - var $fileRoot = ""; + var $fileRoot = ''; /** * Internal flag indicating that a blockname was used multiple times. @@ -342,7 +342,7 @@ /** * EXPERIMENTAL! FIXME! */ - var $lastTemplatefile = ""; + var $lastTemplatefile = ''; /** * $_options['preserve_data'] Whether to substitute variables and remove @@ -369,8 +369,9 @@ * given to the object. * @see setRoot() */ - function HTML_Template_IT($root = "", $options=null) { - if(!is_null($options)){ + function HTML_Template_IT($root = '', $options = null) + { + if (!is_null($options)) { $this->setOptions($options); } $this->variablesRegExp = "@" . $this->openingDelimiter . @@ -401,6 +402,7 @@ $this->_options[$option] = $value; return IT_OK; } + return PEAR::raiseError( $this->errorMessage(IT_UNKNOWN_OPTION) . ": '{$option}'", IT_UNKNOWN_OPTION @@ -421,11 +423,13 @@ */ function setOptions($options) { - foreach($options as $option=>$value){ - if( PEAR::isError($error=$this->setOption($option, $value)) ){ + foreach ($options as $option => $value){ + $error = $this->setOption($option, $value); + if (PEAR::isError($error)) { return $error; } } + return IT_OK; } @@ -433,7 +437,8 @@ * Print a certain block with all replacements done. * @brother get() */ - function show($block = "__global__") { + function show($block = "__global__") + { print $this->get($block); } // end func show @@ -446,10 +451,12 @@ * @access public * @see show() */ - function get($block = "__global__") { + function get($block = "__global__") + { - if ("__global__" == $block && !$this->flagGlobalParsed) + if ($block == "__global__" && !$this->flagGlobalParsed) { $this->parse("__global__"); + } if (!isset($this->blocklist[$block])) { $this->err[] = PEAR::raiseError( @@ -457,13 +464,10 @@ '"' . $block . "'", IT_BLOCK_NOT_FOUND ); - return ""; - } - - if (!isset($this->blockdata[$block])) { return ''; + } - } else { + if (isset($this->blockdata[$block])) { $ret = $this->blockdata[$block]; if ($this->clearCache) { unset($this->blockdata[$block]); @@ -478,6 +482,8 @@ } return $ret; } + + return ''; } // end func get() /** @@ -499,7 +505,7 @@ ); } - if ("__global__" == $block) { + if ($block == "__global__") { $this->flagGlobalParsed = true; } @@ -511,7 +517,6 @@ $empty = true; if ($this->clearCacheOnParse) { - foreach ($this->variableCache as $name => $value) { $regs[] = $this->openingDelimiter . $name . $this->closingDelimiter; @@ -519,9 +524,7 @@ $empty = false; } $this->variableCache = array(); - } else { - foreach ($this->blockvariables[$block] as $allowedvar => $v) { if (isset($this->variableCache[$allowedvar])) { @@ -531,17 +534,14 @@ unset($this->variableCache[$allowedvar]); $empty = false; } - } - } if (isset($this->blockinner[$block])) { - foreach ($this->blockinner[$block] as $k => $innerblock) { $this->parse($innerblock, true); - if ("" != $this->blockdata[$innerblock]) { + if ($this->blockdata[$innerblock] != '') { $empty = false; } @@ -607,7 +607,8 @@ * @see parse(), setCurrentBlock(), $currentBlock * @access public */ - function parseCurrentBlock() { + function parseCurrentBlock() + { return $this->parse($this->currentBlock); } // end func parseCurrentBlock @@ -625,20 +626,15 @@ * @param string prefix for variable names * @access public */ - function setVariable($variable, $value = "") { - + function setVariable($variable, $value = '') + { if (is_array($variable)) { - $this->variableCache = array_merge( $this->variableCache, $variable ); - } else { - $this->variableCache[$variable] = $value; - } - } // end func setVariable /** @@ -650,7 +646,8 @@ * @throws PEAR_Error * @access public */ - function setCurrentBlock($block = "__global__") { + function setCurrentBlock($block = "__global__") + { if (!isset($this->blocklist[$block])) { return PEAR::raiseError( @@ -673,7 +670,8 @@ * @access public * @see $removeEmptyBlocks */ - function touchBlock($block) { + function touchBlock($block) + { if (!isset($this->blocklist[$block])) { return PEAR::raiseError( @@ -696,7 +694,8 @@ * @access public * @see free() */ - function init() { + function init() + { $this->free(); $this->findBlocks($this->template); @@ -714,7 +713,8 @@ * @access public * @see init() */ - function free() { + function free() + { $this->err = array(); @@ -742,29 +742,26 @@ * @access public */ function setTemplate( $template, $removeUnknownVariables = true, - $removeEmptyBlocks = true - ) { + $removeEmptyBlocks = true) + { $this->removeUnknownVariables = $removeUnknownVariables; $this->removeEmptyBlocks = $removeEmptyBlocks; - if ("" == $template && $this->flagCacheTemplatefile) { - + if ($template == '' && $this->flagCacheTemplatefile) { $this->variableCache = array(); $this->blockdata = array(); $this->touchedBlocks = array(); $this->currentBlock = "__global__"; - } else { - $this->template = '<!-- BEGIN __global__ -->' . $template . '<!-- END __global__ -->'; $this->init(); - } - if ($this->flagBlocktrouble) + if ($this->flagBlocktrouble) { return false; + } return true; } // end func setTemplate @@ -782,20 +779,21 @@ */ function loadTemplatefile( $filename, $removeUnknownVariables = true, - $removeEmptyBlocks = true ) { + $removeEmptyBlocks = true ) + { - $template = ""; + $template = ''; if (!$this->flagCacheTemplatefile || $this->lastTemplatefile != $filename - ){ + ) { $template = $this->getFile($filename); } $this->lastTemplatefile = $filename; - return $template!=""? + return $template != '' ? $this->setTemplate( $template,$removeUnknownVariables, $removeEmptyBlocks - ):false; + ) : false; } // end func LoadTemplatefile /** @@ -809,55 +807,50 @@ * @see IntegratedTemplate() * @access public */ - function setRoot($root) { + function setRoot($root) + { - if ("" != $root && "/" != substr($root, -1)) - $root .= "/"; + if ($root != '' && substr($root, -1) != '/') { + $root .= '/'; + } $this->fileRoot = $root; - } // end func setRoot /** * Build a list of all variables within of a block */ - function buildBlockvariablelist() { + function buildBlockvariablelist() + { foreach ($this->blocklist as $name => $content) { - preg_match_all( $this->variablesRegExp, $content, $regs ); - - if (0 != count($regs[1])) { + preg_match_all($this->variablesRegExp, $content, $regs); - foreach ($regs[1] as $k => $var) + if (count($regs[1]) != 0) { + foreach ($regs[1] as $k => $var) { $this->blockvariables[$name][$var] = true; - + } } else { - $this->blockvariables[$name] = array(); - } - } - } // end func buildBlockvariablelist /** * Returns a list of all global variables */ - function getGlobalvariables() { - + function getGlobalvariables() + { $regs = array(); $values = array(); foreach ($this->blockvariables["__global__"] as $allowedvar => $v) { - if (isset($this->variableCache[$allowedvar])) { $regs[] = "@" . $this->openingDelimiter . $allowedvar . $this->closingDelimiter."@"; $values[] = $this->variableCache[$allowedvar]; unset($this->variableCache[$allowedvar]); } - } return array($regs, $values); @@ -869,8 +862,8 @@ * @param string string that gets scanned * @see $blocklist */ - function findBlocks($string) { - + function findBlocks($string) + { $blocklist = array(); if ( @@ -930,14 +923,15 @@ * @param string Filename * @return string Filecontent */ - function getFile($filename) { - - if ("/" == $filename{0} && "/" == substr($this->fileRoot, -1)) + function getFile($filename) + { + if ($filename{0} == '/' && substr($this->fileRoot, -1) == '/') { $filename = substr($filename, 1); + } $filename = $this->fileRoot . $filename; - if (!($fh = @fopen($filename, "r"))) { + if (!($fh = @fopen($filename, 'r'))) { $this->err[] = PEAR::raiseError( $this->errorMessage(IT_TPL_NOT_FOUND) . ': "' .$filename .'"', http://cvs.php.net/diff.php/pear/HTML_Template_IT/ITX.php?r1=1.9&r2=1.10&ty=u Index: pear/HTML_Template_IT/ITX.php diff -u pear/HTML_Template_IT/ITX.php:1.9 pear/HTML_Template_IT/ITX.php:1.10 --- pear/HTML_Template_IT/ITX.php:1.9 Sun Aug 14 13:42:38 2005 +++ pear/HTML_Template_IT/ITX.php Fri Oct 28 05:40:22 2005 @@ -16,10 +16,10 @@ // | Author: Ulf Wendel <ulf.wendel@phpdoc.de> | // +----------------------------------------------------------------------+ // -// $Id: ITX.php,v 1.9 2005/08/14 17:42:38 pajoye Exp $ +// $Id: ITX.php,v 1.10 2005/10/28 09:40:22 dufuz Exp $ // -require_once('HTML/Template/IT.php'); +require_once 'HTML/Template/IT.php'; require_once 'HTML/Template/IT_Error.php'; /** @@ -36,50 +36,50 @@ * * @author Ulf Wendel <uw@netuse.de> * @access public -* @version $Id: ITX.php,v 1.9 2005/08/14 17:42:38 pajoye Exp $ +* @version $Id: ITX.php,v 1.10 2005/10/28 09:40:22 dufuz Exp $ * @package IT[X] */ class HTML_Template_ITX extends HTML_Template_IT { /** * Array with all warnings. - * @var array - * @access public - * @see $printWarning, $haltOnWarning, warning() + * @var array + * @access public + * @see $printWarning, $haltOnWarning, warning() */ var $warn = array(); /** * Print warnings? - * @var array - * @access public - * @see $haltOnWarning, $warn, warning() + * @var array + * @access public + * @see $haltOnWarning, $warn, warning() */ var $printWarning = false; /** * Call die() on warning? - * @var boolean - * @access public - * @see $warn, $printWarning, warning() + * @var boolean + * @access public + * @see $warn, $printWarning, warning() */ var $haltOnWarning = false; /** * RegExp used to test for a valid blockname. - * @var string + * @var string */ var $checkblocknameRegExp = ''; /** * Functionnameprefix used when searching function calls in the template. - * @var string + * @var string */ var $functionPrefix = 'func_'; /** * Functionname RegExp. - * @var string + * @var string */ var $functionnameRegExp = '[_a-zA-Z]+[A-Za-z_0-9]*'; @@ -88,24 +88,24 @@ * * The variable gets set by the constructor. * - * @var string - * @see HTML_Template_IT() + * @var string + * @see HTML_Template_IT() */ var $functionRegExp = ''; /** * List of functions found in the template. * - * @var array + * @var array */ - var $functions = array(); + var $functions = array(); /** * List of callback functions specified by the user. * - * @var array + * @var array */ - var $callback = array(); + var $callback = array(); /** * Builds some complex regexps and calls the constructor @@ -171,9 +171,11 @@ __FILE__, __LINE__ ); } - if ('' == $template) { + + if ($template == '') { return new IT_Error('No block content given.', __FILE__, __LINE__); } + if ($keep_content) { $blockdata = $this->blockdata[$block]; } @@ -247,35 +249,27 @@ { // Don't trust any user even if it's a programmer or yourself... - if ('' == $placeholder) { - + if ($placeholder == '') { return new IT_Error('No variable placeholder given.', __FILE__, __LINE__ ); - - } else if ( '' == $blockname || + } elseif ($blockname == '' || !preg_match($this->checkblocknameRegExp, $blockname) ) { - return new IT_Error("No or invalid blockname '$blockname' given.", __FILE__, __LINE__ ); - - } else if ('' == $template) { - + } elseif ($template == '') { return new IT_Error('No block content given.', __FILE__, __LINE__); - - } else if (isset($this->blocklist[$blockname])) { - + } elseif (isset($this->blocklist[$blockname])) { return new IT_Error('The block already exists.', __FILE__, __LINE__ ); - } // find out where to insert the new block $parents = $this->findPlaceholderBlocks($placeholder); - if (0 == count($parents)) { + if (count($parents) == 0) { return new IT_Error( "The variable placeholder". @@ -283,7 +277,7 @@ __FILE__, __LINE__ ); - } else if ( count($parents) > 1 ) { + } elseif (count($parents) > 1) { reset($parents); while (list($k, $parent) = each($parents)) { @@ -357,12 +351,12 @@ */ function placeholderExists($placeholder, $block = '') { - if ('' == $placeholder) { + if ($placeholder == '') { new IT_Error('No placeholder name given.', __FILE__, __LINE__); return ''; } - if ('' != $block && !isset($this->blocklist[$block])) { + if ($block != '' && !isset($this->blocklist[$block])) { new IT_Error("Unknown block '$block'.", __FILE__, __LINE__); return ''; } @@ -370,10 +364,8 @@ // name of the block where the given placeholder was found $found = ''; - if ('' != $block) { - + if ($block != '') { if (is_array($variables = $this->blockvariables[$block])) { - // search the value in the list of blockvariables reset($variables); while (list($k, $variable) = each($variables)) { @@ -383,20 +375,17 @@ } } } - } else { // search all blocks and return the name of the first block that // contains the placeholder reset($this->blockvariables); while (list($blockname, $variables) = each($this->blockvariables)){ - if (is_array($variables) && isset($variables[$placeholder])) { $found = $blockname; break; } } - } return $found; @@ -413,10 +402,8 @@ reset($this->functions); while (list($func_id, $function) = each($this->functions)) { - if (isset($this->callback[$function['name']])) { - - if ('' != $this->callback[$function['name']]['object']) { + if ($this->callback[$function['name']]['object'] != '') { $this->variableCache['__function' . $func_id . '__'] = call_user_func( array( @@ -431,9 +418,7 @@ $function['args'] ); } - } - } } // end func performCallback @@ -508,7 +493,7 @@ setCallbackFunction($tplfunction, $callbackfunction, $callbackobject = '') { - if ('' == $tplfunction || '' == $callbackfunction) { + if ($tplfunction == '' || $callbackfunction == '') { return new IT_Error( "No template function "."('$tplfunction')". " and/or no callback function ('$callback') given.", @@ -516,8 +501,8 @@ ); } $this->callback[$tplfunction] = array( - "function" => $callbackfunction, - "object" => $callbackobject + 'function' => $callbackfunction, + 'object' => $callbackobject ); return true; @@ -652,7 +637,7 @@ '{__function' . $num . '__}', $template ); - while ('' != $head && $args2 = $this->getValue($head, ',')) { + while ($head != '' && $args2 = $this->getValue($head, ',')) { $arg2 = trim($args2); $args[] = ('"' == $arg2{0} || "'" == $arg2{0}) ? substr($arg2, 1, -1) : $arg2; @@ -672,7 +657,7 @@ function getValue($code, $delimiter) { - if ('' == $code) { + if ($code == '') { return ''; } @@ -685,20 +670,17 @@ $enclosed_by = ''; if (isset($delimiter[$code[0]])) { - $i = 1; - } else { - for ($i = 0; $i < $len; ++$i) { $char = $code[$i]; if ( - ('"' == $char || "'" == $char) && + ($char == '"' || $char = "'") && ($char == $enclosed_by || '' == $enclosed_by) && (0 == $i || ($i > 0 && '\\' != $code[$i - 1])) - ){ + ) { if (!$enclosed) { $enclosed_by = $char; @@ -708,6 +690,7 @@ $enclosed = !$enclosed; } + if (!$enclosed && isset($delimiter[$char])) { break; } @@ -751,7 +734,7 @@ $this->blocklist[$block], $regs ); - if (0 != count($regs[1])) { + if (count($regs[1]) != 0) { foreach ($regs[1] as $k => $var) { $this->blockvariables[$block][$var] = true; } @@ -768,7 +751,7 @@ * loop through inner blocks, registering the variable * placeholders in each */ - foreach($this->blockinner[$block] as $childBlock) { + foreach ($this->blockinner[$block] as $childBlock) { $this->updateBlockvariablelist($childBlock); } }
« previous php.pear.cvs (#35918) next »