]> git.mxchange.org Git - friendica.git/commitdiff
template proc: first optimization
authorFabio Comuni <fabrix.xm@gmail.com>
Fri, 2 Mar 2012 11:24:35 +0000 (12:24 +0100)
committerFabio Comuni <fabrix.xm@gmail.com>
Fri, 2 Mar 2012 11:24:35 +0000 (12:24 +0100)
include/template_processor.php
include/text.php

index b4d444e1c622ee7d472d00d990ba8fbb3369531c..a80a3997af601d7ac2b43a86b652fa2522cfeec2 100755 (executable)
@@ -1,5 +1,5 @@
 <?php
-
+       define ("KEY_NOT_EXISTS", '^R_key_not_Exists^');
 
        class Template {
                var $r;
                } 
                
                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(){
-                       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);
                        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);
                        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);
                }
index 4276b7fcb39263bce5b30d3d5622b24462198c98..042ee982cb1d094741b2f0d7c242ddfada77387a 100755 (executable)
@@ -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 <br>\n";
+       return $r;
 
 }}