template proc: first optimization

This commit is contained in:
Fabio Comuni 2012-03-02 12:24:35 +01:00
parent f1bf6dcdfb
commit 60e5dda1b5
2 changed files with 29 additions and 10 deletions

View File

@ -1,5 +1,5 @@
<?php <?php
define ("KEY_NOT_EXISTS", '^R_key_not_Exists^');
class Template { class Template {
var $r; var $r;
@ -43,15 +43,16 @@
} }
private function _push_stack(){ private function _push_stack(){
$this->stack[] = array($this->r, $this->search, $this->replace, $this->nodes); $this->stack[] = array($this->r, $this->nodes);
} }
private function _pop_stack(){ 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){ private function _get_var($name, $retNoKey=false){
$keys = array_map('trim',explode(".",$name)); $keys = array_map('trim',explode(".",$name));
if ($retNoKey && !array_key_exists($keys[0], $this->r)) return KEY_NOT_EXISTS;
$val = $this->r; $val = $this->r;
foreach($keys as $k) { foreach($keys as $k) {
$val = (isset($val[$k]) ? $val[$k] : null); $val = (isset($val[$k]) ? $val[$k] : null);
@ -194,13 +195,24 @@
return str_replace($this->search,$this->replace, $str); 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) { public function replace($s, $r) {
$this->r = $r; $this->r = $r;
$this->search = array(); /*$this->search = array();
$this->replace = array(); $this->replace = array();*/
$this->_build_replace($r, ""); //$this->_build_replace($r, "");
#$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s); #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s);
$s = $this->_build_nodes($s); $s = $this->_build_nodes($s);
@ -215,7 +227,8 @@
while($os!=$s && $count<10){ while($os!=$s && $count<10){
$os=$s; $count++; $os=$s; $count++;
//$s = $this->_str_replace($s); //$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); return template_unescape($s);
} }

View File

@ -14,7 +14,13 @@ if(! function_exists('replace_macros')) {
function replace_macros($s,$r) { function replace_macros($s,$r) {
global $t; global $t;
return $t->replace($s,$r); //$ts = microtime();
$r = $t->replace($s,$r);
//$tt = microtime() - $ts;
//$a = get_app();
//$a->page['debug'] .= "$tt <br>\n";
return $r;
}} }}