From 60e5dda1b5e1be36a15c45ded2635340087a0b0f Mon Sep 17 00:00:00 2001 From: Fabio Comuni Date: Fri, 2 Mar 2012 12:24:35 +0100 Subject: [PATCH] template proc: first optimization --- include/template_processor.php | 31 ++++++++++++++++++++++--------- include/text.php | 8 +++++++- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/include/template_processor.php b/include/template_processor.php index b4d444e1c6..a80a3997af 100755 --- a/include/template_processor.php +++ b/include/template_processor.php @@ -1,5 +1,5 @@ stack[] = array($this->r, $this->search, $this->replace, $this->nodes); + $this->stack[] = array($this->r, $this->nodes); } private function _pop_stack(){ - list($this->r, $this->search, $this->replace, $this->nodes) = array_pop($this->stack); + list($this->r, $this->nodes) = array_pop($this->stack); } - private function _get_var($name){ - $keys = array_map('trim',explode(".",$name)); + private function _get_var($name, $retNoKey=false){ + $keys = array_map('trim',explode(".",$name)); + if ($retNoKey && !array_key_exists($keys[0], $this->r)) return KEY_NOT_EXISTS; $val = $this->r; foreach($keys as $k) { $val = (isset($val[$k]) ? $val[$k] : null); @@ -194,13 +195,24 @@ return str_replace($this->search,$this->replace, $str); }*/ + private function var_replace($s){ + $m = array(); + if (preg_match_all('/\$([a-zA-Z0-9-_]+\.*)+/', $s,$m)){ + foreach($m[0] as $var){ + $val = $this->_get_var($var, true); + if ($val!=KEY_NOT_EXISTS) + $s = str_replace($var, $val, $s); + } + } + return $s; + } public function replace($s, $r) { $this->r = $r; - $this->search = array(); - $this->replace = array(); + /*$this->search = array(); + $this->replace = array();*/ - $this->_build_replace($r, ""); + //$this->_build_replace($r, ""); #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s); $s = $this->_build_nodes($s); @@ -215,7 +227,8 @@ while($os!=$s && $count<10){ $os=$s; $count++; //$s = $this->_str_replace($s); - $s = str_replace($this->search, $this->replace, $s); + $s = $this->var_replace($s); + //$s = str_replace($this->search, $this->replace, $s); } return template_unescape($s); } diff --git a/include/text.php b/include/text.php index 4276b7fcb3..042ee982cb 100755 --- a/include/text.php +++ b/include/text.php @@ -14,7 +14,13 @@ if(! function_exists('replace_macros')) { function replace_macros($s,$r) { global $t; - return $t->replace($s,$r); + //$ts = microtime(); + $r = $t->replace($s,$r); + //$tt = microtime() - $ts; + + //$a = get_app(); + //$a->page['debug'] .= "$tt
\n"; + return $r; }}