[php] view plaincopy在CODE上查看代码片派生到我的代码片

  1. <?php      

  2. /** 

  3.  * 摘取天上星 版 插件引擎 第二版 version 2.0   

  4.  * By: 摘取天上星! 

  5.  * Emali: happy.yin@qq.com 

  6.  * Date: 2012升级版 

  7.  **/  

  8.   $plugin_arr=array();  

  9.   $plugin_meta=array();  

  10.   $plugin_remove=array();  

  11.   $action_arr=array();  

  12.   $action_meta=array();  

  13.   $action_remove=array();  

  14.   $idx=0;  

  15.   /* 

  16.    * 执行插件引擎中捆绑的所有函数事件(函数执行顺序参加addPlugin函数添加插件时的第四个参数数字,数字越大优先级越高) 

  17.    * $tag 要执行的函数集插件标签名 

  18.    * $args 要往函数中传入的参数,依次按顺序填写,键名同addPlugin添加插件时第三个参数传入的键名、数量对应一致,键名对应的值即传入的参数值, 

  19.    * 该插件引擎是有返回值的插件引擎 

  20.    */  

  21.   function doPlugin($tag,$args=array()){  

  22.     global $plugin_arr,$plugin_remove;  

  23.     $first=array_search(current($args),$args);  

  24.     if(empty($plugin_arr[$tag])) return $args[$first];  

  25.     if(isset($plugin_remove[$tag])){  

  26.         foreach($plugin_remove[$tagas $func){  

  27.             removePlugin($tag,$func);  

  28.         }  

  29.     }  

  30.     krsort($plugin_arr[$tag]);  

  31.     foreach($plugin_arr[$tagas $plugins){  

  32.         foreach($plugins as $plugins){  

  33.             $plugins['args']=array_merge($plugins['args'],$args);  

  34.             $args[$first]=call_user_func_array($plugins['func'],array_slice($plugins['args'],0,$plugins['args_count']));  

  35.         }  

  36.     }  

  37.     return $args[$first];  

  38.   }  

  39.   /* 第一个参数为自定义标签集名, 

  40.    * 第二个参数是你要向标签集里添加的函数名, 

  41.    * 第三个数组参数为第二个参数strAndStr1函数对应的参数集,有多少个函数参数,就需要添加多少个数组元素, 

  42.            参数按照先后顺序依次填写,键值为空即可,且插件里所有函数的参数个数必须一致,一个以上的参数,可多个, 

  43.            这里的传参数组只需要预写好键名即可,在调用doPlugin插件时给对应的键值传入键名对应的实际参数值即可 

  44.    * 第四个参数为排序参数,从1到10的纯数字,数值越大执行优先级越高,反之越小,默认为值为最大优先级10 

  45.    * addPlugin('cleanText','strAndStr1',array('str'=>'','str2'=>''),1);   

  46.    * addPlugin('cleanText','strAndStr2',array('str'=>'','str2'=>''),2);  

  47.    */  

  48.   function addPlugin($tag,$func,$args=array(),$sort=10){  

  49.     global $plugin_arr,$plugin_meta,$idx;  

  50.     $plugin_arr[$tag][$sort][++$idx]=array('func'=>$func,'args'=>$args,'args_count'=>sizeof($args));  

  51.     $plugin_meta[$tag][$func][$idx]=$sort;  

  52.   }  

  53.   /* 

  54.    * 立即删除函数集标签中 的某个函数 

  55.    * 第一个参数为自定义函数集标签名称 

  56.    * 第二个参数为要从函数集里 删除的单个函数名称 

  57.    */  

  58.   function removePlugin($tag,$func){  

  59.     global $plugin_arr,$plugin_meta;  

  60.     if(isset($plugin_meta[$tag][$func])){  

  61.         foreach($plugin_meta[$tag][$funcas $idx=>$sort){  

  62.             unset($plugin_arr[$tag][$sort][$idx]);  

  63.         }  

  64.         unset($plugin_meta[$tag][$func]);  

  65.     }  

  66.   }  

  67.   /* 

  68.    * 在下次执行doPlugin时删除函数集标签中 的某个函数(在doPlugin中的插件函数执行前删除,并且删除后执行插件引擎!) 

  69.    * 第一个参数为自定义函数集标签名称 

  70.    * 第二个参数为要从函数集里 删除的单个函数名称 

  71.    */  

  72.   function addRemovePlugin($tag,$func){  

  73.     global $plugin_remove;  

  74.     if(in_array($func,(array)$plugin_remove[$tag])) return ;  

  75.     $plugin_remove[$tag][]=$func;  

  76.   }  

  77.   /* 

  78.    * 如下执行插件方法同上述有返回值的执行插件使用方法对应一致, 

  79.    * 唯一的区别是没有返回值 

  80.    */  

  81.   /* 

  82.    * 执行插件引擎 

  83.    */  

  84.   function doAction($tag,$args=array()){  

  85.     global  $action_arr,$action_remove;  

  86.     if(empty($action_arr[$tag])) return ;  

  87.     if(isset($action_remove[$tag])){  

  88.         foreach($action_remove[$tagas $func){  

  89.             removeAction($tag,$func);  

  90.         }  

  91.     }  

  92.     krsort($action_arr[$tag]);  

  93.     foreach($action_arr[$tagas $action_sort){  

  94.         foreach($action_sort as $action_idx){  

  95.             $action_idx['args']=array_merge($action_idx['args'],$args);  

  96.             call_user_func_array($action_idx['func'],array_slice($action_idx['args'],0,$action_idx['args_count']));  

  97.         }  

  98.     }  

  99.   }  

  100.   /* 

  101.    * 向插件引擎里添加函数 

  102.    */  

  103.   function addAction($tag,$func,$args=array(),$sort=10){  

  104.     global $action_arr,$action_meta,$idx;  

  105.     $action_arr[$tag][$sort][++$idx]=array('func'=>$func,'args'=>$args,'args_count'=>sizeof($args));  

  106.     $action_meta[$tag][$func][$idx]=$sort;  

  107.   }  

  108.   /* 

  109.    * 从插件引擎里删除 执行的函数 

  110.    */  

  111.   function removeAction($tag,$func){  

  112.     global $action_arr,$action_meta;  

  113.     if(isset($action_meta[$tag][$func])){  

  114.         foreach($action_meta[$tag][$funcas $idx=>$sort){  

  115.             unset($action_arr[$tag][$sort][$idx]);  

  116.         }  

  117.         unset($action_meta[$tag][$func]);  

  118.     }  

  119.   }  

  120.   /* 

  121.    * 添加预删除函数,该函数会在下次执行插件引擎时,在函数集调用前被删除 

  122.    */  

  123.   function addRemoveAction($tag,$func){  

  124.     global $action_remove;  

  125.     if(in_array($func,(array)$action_remove[$tag])) return ;  

  126.     $action_remove[$tag][]=$func;  

  127.   }  

  128.   /* 摘取天上星 - 期待更深层次的扩展压缩...*/  

  129. ?>  




<?php

//执行例子如下

//为插件引擎准备好要用到的测试函数
function str2str2($str){
  return '<p>P标签开始 '.$str.' P标签结束<p/>';
}
function str3str3($str){
  return '<b style="color:red">b标签开始 '.$str.' b标签结束<b/>';

}

//注意:在测试三个例子时,一定要一个一个的测试,测试时请注释掉其他多余的例子,否则将无法看到插件引擎权限优先级的 实际对比效果产生异常结果!

例子一:
//str2str2函数的执行优先级小于str3str3,这里先执行str3str3($str)函数后执行str2str2($str)函数;
//实际运行流程解刨如下:
$str=str3str3('这是要像插件里所有函数传入的参数这里函数str3str3的执行优先级高于str2str2');
$str=str2str2($str);
echo $str; 
/*输出结果浏览器里查看HTML源代码得到如下内容:
 <p>P标签开始 <b style="color:red">b标签开始 这是要像插件里所有函数传入的参数这里函数str3str3的执行优先级高于str2str2 b标签结束<b/> P标签结束<p/>
 */
addPlugin('cleanText','str2str2',array('str'=>''),1);
addPlugin('cleanText','str3str3',array('str'=>''),10);
echo doPlugin('cleanText',array('str'=>'这是要像插件里所有函数传入的参数这里函数str3str3的执行优先级高于str2str2'));
//例子二:
addPlugin('cleanText','str2str2',array('str'=>''),10);
addPlugin('cleanText','str3str3',array('str'=>''),1);
echo doPlugin('cleanText',array('str'=>'这是要像插件里所有函数传入的参数这里函数str2str2的执行优先级高于str3str3'));
/*运行结果HTML页面源代码如下:
<b style="color:red">b标签开始 <p>P标签开始 这是要像插件里所有函数传入的参数这里函数str2str2的执行优先级高于str3str3 P标签结束<p/> b标签结束<b/>
*/
//例子三:
addPlugin('cleanText','str2str2',array('str'=>''),1);
addPlugin('cleanText','str3str3',array('str'=>''),1);
echo doPlugin('cleanText',array('str'=>'当权限排序值大小一致时,后面的函数权限优先级要小于前面的故先添加的函数先执行,这里函数str3str3的执行优先级小于str2str2'));
/* 执行后的HTML源代码结果如下:
<b style="color:red">b标签开始 <p>P标签开始 当权限排序值大小一致时,后面的函数权限优先级要小于前面的故先添加的函数先执行,这里函数str3str3的执行优先级小于str2str2 P标签结束<p/> b标签结束<b/>

*/


//测试doAction执行插件的例子(该插件没有返回值,只执行!)
/*注,该插件为伍返回值插件,故而只用做输出 或直接执行场合,优先级同doPlugin插件优先级设置,故不详述!
function alertstr($str){
  echo "<script>alert('$str');</script>";
}
function alertstr2($str){
  echo $str.'1+2';
}
addAction('alert','alertstr',array('str'=>''),1);
addAction('alert','alertstr2',array('str'=>''),10);
doAction('alert',array('str'=>'要弹出的参数'));
//运行后的HTML源代码结果如下:

//要弹出的参数1+2<script>alert('要弹出的参数');</script>

?>