[phplib-dev] cvs commit From: uw (phplib-dev <email protected>)
Date: 12/11/00

From: uw
Date: Mon Dec 11 14:08:15 2000
Modified files:
      php-lib/php/ext/integratedtemplate.inc

Log message:
Kicked PHP 3 - all my sources from now on will be PHP 4 only. Should not
make any major speed difference.

Index: php-lib/php/ext/integratedtemplate.inc
diff -u php-lib/php/ext/integratedtemplate.inc:1.7 php-lib/php/ext/integratedtemplate.inc:1.8
--- php-lib/php/ext/integratedtemplate.inc:1.7 Fri Dec 1 10:31:37 2000
+++ php-lib/php/ext/integratedtemplate.inc Mon Dec 11 14:07:43 2000
@@ -305,6 +305,25 @@
         var $flagGlobalParsed = false;
         
         /**
+ * EXPERIMENTAL! FIXME!
+ * Flag indication that a template gets cached.
+ *
+ * Complex templates require some times to be preparsed
+ * before the replacement can take place. Often I use
+ * one template file over and over again but I don't know
+ * before that I will use the same template file again.
+ * Now IT could notice this and skip the preparse.
+ *
+ *  <email protected> boolean
+ */
+ var $flagCacheTemplatefile = true;
+
+ /**
+ * EXPERIMENTAL! FIXME!
+ */
+ var $lastTemplatefile = "";
+
+ /**
         * Builds some complex regular expressions and optinally sets the file root directory.
         *
         * Make sure that you call this constructor if you derive your template
@@ -315,10 +334,10 @@
         */
         function IntegratedTemplate($root = "") {
         
- $this->variablesRegExp = "@".$this->openingDelimiter."(".$this->variablenameRegExp.")".$this->closingDelimiter." <email protected>";
- $this->removeVariablesRegExp = "@".$this->openingDelimiter."\s*(".$this->variablenameRegExp.")\s*".$this->closingDelimiter." <email protected>";
+ $this->variablesRegExp = "@" . $this->openingDelimiter . "(" . $this->variablenameRegExp . ")" . $this->closingDelimiter . " <email protected>";
+ $this->removeVariablesRegExp = "@" . $this->openingDelimiter . "\s*(" . $this->variablenameRegExp . ")\s*" . $this->closingDelimiter . " <email protected>";
                 
- $this->blockRegExp = '@<!--\s+BEGIN\s+('.$this->blocknameRegExp.')\s+-->(.*)<!--\s+END\s+\1\s+--> <email protected>';
+ $this->blockRegExp = '@<!--\s+BEGIN\s+(' . $this->blocknameRegExp . ')\s+-->(.*)<!--\s+END\s+\1\s+--> <email protected>';
 
                 $this->setRoot($root);
         } // end constructor
@@ -343,7 +362,7 @@
 
                 if ("__global__" == $block && !$this->flagGlobalParsed)
                         $this->parse("__global__");
-
+
                 if (!isset($this->blocklist[$block])) {
                         $this->halt("The block '$block' was not found in the template.", __FILE__, __LINE__);
                         return true;
@@ -385,20 +404,18 @@
 
                 if ($this->clearCacheOnParse) {
                         
- reset($this->variableCache);
- while (list($name, $value)=each($this->variableCache)) {
- $regs[] = "@".$this->openingDelimiter.$name.$this->closingDelimiter."@";
+ foreach ($this->variableCache as $name => $value) {
+ $regs[] = "@" . $this->openingDelimiter . $name . $this->closingDelimiter . "@";
                                 $values[] = $value;
                         }
                         $this->variableCache = array();
                 
                 } else {
 
- reset($this->blockvariables[$block]);
- while (list($allowedvar, $k) = each($this->blockvariables[$block])) {
+ foreach ($this->blockvariables[$block] as $allowedvar => $v) {
                 
                                 if (isset($this->variableCache[$allowedvar])) {
- $regs[] = "@".$this->openingDelimiter.$allowedvar.$this->closingDelimiter."@";
+ $regs[] = "@".$this->openingDelimiter . $allowedvar . $this->closingDelimiter . "@";
                        $values[] = $this->variableCache[$allowedvar];
                                         unset($this->variableCache[$allowedvar]);
                                 }
@@ -412,39 +429,38 @@
 
     if (isset($this->blockinner[$block])) {
                 
- reset($this->blockinner[$block]);
- while (list($k, $innerblock) = each($this->blockinner[$block])) {
+ foreach ($this->blockinner[$block] as $k => $innerblock) {
 
         $this->parse($innerblock, true);
- if (""!=$this->blockdata[$innerblock])
+ if ("" != $this->blockdata[$innerblock])
                                         $empty = false;
 
- $placeholder = $this->openingDelimiter."__".$innerblock."__".$this->closingDelimiter;
+ $placeholder = $this->openingDelimiter . "__" . $innerblock . "__" . $this->closingDelimiter;
         $outer = str_replace($placeholder, $this->blockdata[$innerblock], $outer);
                                 $this->blockdata[$innerblock] = "";
       }
+
     }
 
     if ($this->removeUnknownVariables)
                         $outer = preg_replace($this->removeVariablesRegExp, "", $outer);
 
-
                 if ($empty) {
 
                         if (!$this->removeEmptyBlocks) {
                         
- $this->blockdata[$block].= $outer;
+ $this->blockdata[$block ].= $outer;
                                 
                         } else {
 
                                 if (isset($this->touchedBlocks[$block]))
- $this->blockdata[$block].= $outer;
+ $this->blockdata[$block] .= $outer;
                                 
                         }
                                 
                 } else {
                 
- $this->blockdata[$block].= $outer;
+ $this->blockdata[$block] .= $outer;
                 
                 }
 
@@ -472,17 +488,15 @@
         *  <email protected> string prefix for variable names
         *  <email protected> public
         */
- function setVariable($variable, $value="") {
+ function setVariable($variable, $value = "") {
                 
                 if (is_array($variable)) {
                 
- reset($variable);
- while (list($var, $value)=each($variable))
- $this->variableCache[$var] = $value;
+ $this->variableCache = array_merge($this->variableCache, $variable);
                                 
                 } else {
                         
- $this->variableCache[$variable] = $value;
+ $this->variableCache[$variable] = $value;
                         
                 }
         
@@ -586,17 +600,24 @@
         *  <email protected> public
         */
         function setTemplate($template, $removeUnknownVariables = true, $removeEmptyBlocks = true) {
- if (""==$template) {
- $this->halt("The given string is empty.", __FILE__, __LINE__);
- return false;
- }
-
+
                 $this->removeUnknownVariables = $removeUnknownVariables;
                 $this->removeEmptyBlocks = $removeEmptyBlocks;
                 
- $this->template = '<!-- BEGIN __global__ -->'.$template.'<!-- END __global__ -->';
- $this->init();
+ 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)
                         return false;
                 
@@ -614,10 +635,14 @@
         *  <email protected> $template, setTemplate(), $removeUnknownVariables, $removeEmptyBlocks
         */
         function loadTemplatefile($filename, $removeUnknownVariables = true, $removeEmptyBlocks = true) {
-
- $template = $this->getfile($filename);
-
- return $this->setTemplate($this->getFile($filename), $removeUnknownVariables, $removeEmptyBlocks);
+
+ $template = "";
+ if (!$this->flagCacheTemplatefile || $this->lastTemplatefile != $filename)
+ $template = $this->getfile($filename);
+
+ $this->lastTemplatefile = $filename;
+
+ return $this->setTemplate($template, $removeUnknownVariables, $removeEmptyBlocks, true);
         } // end func LoadTemplatefile
         
         /**
@@ -632,8 +657,8 @@
         */
         function setRoot($root) {
                 
- if (""!=$root && "/"!= substr($root, -1))
- $root.="/";
+ if ("" != $root && "/" != substr($root, -1))
+ $root .= "/";
                 
                 $this->fileRoot = $root;
                 
@@ -644,14 +669,12 @@
         */
         function buildBlockvariablelist() {
 
- reset($this->blocklist);
- while (list($name, $content)=each($this->blocklist)) {
+ foreach ($this->blocklist as $name => $content) {
                         preg_match_all( $this->variablesRegExp, $content, $regs );
 
                         if (0 != count($regs[1])) {
-
- reset($regs[1]);
- while (list($k, $var) = each($regs[1]))
+
+ foreach ($regs[1] as $k => $var)
                                         $this->blockvariables[$name][$var] = true;
                                         
                         } else {
@@ -671,12 +694,11 @@
 
     $regs = array();
     $values = array();
-
- reset($this->blockvariables["__global__"]);
- while (list($allowedvar, $v) = each($this->blockvariables["__global__"])) {
+
+ foreach ($this->blockvariables["__global__"] as $allowedvar => $v) {
                         
                         if (isset($this->variableCache[$allowedvar])) {
- $regs[] = "@".$this->openingDelimiter.$allowedvar.$this->closingDelimiter."@";
+ $regs[] = "@" . $this->openingDelimiter . $allowedvar . $this->closingDelimiter."@";
                                 $values[] = $this->variableCache[$allowedvar];
                                 unset($this->variableCache[$allowedvar]);
                         }
@@ -699,8 +721,7 @@
 
                 if (preg_match_all($this->blockRegExp, $string, $regs, PREG_SET_ORDER)) {
                         
- reset($regs);
- while (list($k, $match)=each($regs)) {
+ foreach ($regs as $k => $match) {
                         
                                 $blockname = $match[1];
                                 $blockcontent = $match[2];
@@ -716,8 +737,7 @@
                                 $blocklist[] = $blockname;
                                 
                                 $inner = $this->findBlocks($blockcontent);
- reset($inner);
- while (list($k, $name)=each($inner)) {
+ foreach ($inner as $k => $name) {
 
                                         $pattern = sprintf('@<!--\s+BEGIN\s+%s\s+-->(.*)<!--\s+END\s+%s\s+--> <email protected>',
                                                                                                         $name,
@@ -725,7 +745,7 @@
                                                                                                 );
 
                                         $this->blocklist[$blockname] = preg_replace( $pattern,
- $this->openingDelimiter."__".$name."__".$this->closingDelimiter,
+ $this->openingDelimiter . "__" . $name . "__" . $this->closingDelimiter,
                                                                                                                                                                                                                                 $this->blocklist[$blockname]
                                                                                                                                                                                                                         );
                                         $this->blockinner[$blockname][] = $name;
@@ -747,10 +767,10 @@
         */
         function getFile($filename) {
                 
- if ("/" == substr($filename, 0, 1))
+ if ("/" == $filename{0} && "/" == substr($this->fileRoot, -1))
                         $filename = substr($filename, 1);
                         
- $filename = $this->fileRoot.$filename;
+ $filename = $this->fileRoot . $filename;
                 
                 if ( !($fh =  <email protected>($filename, "r")) ) {
                         $this->halt("Can't read '$filename'.", __FILE__, __LINE__);
@@ -770,7 +790,8 @@
         *  <email protected> int Line where the error occured
         *  <email protected> $err
         */
- function halt($message, $file="", $line=0) {
+ function halt($message, $file = "", $line = 0) {
+
                 
                 $message = sprintf("IntegratedTemplate Error: %s [File: %s, Line: %d]",
                                                                                                                         $message,

---------------------------------------------------------------------
To unsubscribe, e-mail: phplib-dev-unsubscribe <email protected>
For additional commands, e-mail: phplib-dev-help <email protected>