Home Backend Development PHP Tutorial PHP下用B/S编程模式去实现C/S软件编程模式下的插件引擎功能!

PHP下用B/S编程模式去实现C/S软件编程模式下的插件引擎功能!

Jun 23, 2016 pm 01:39 PM

<?php /** * 摘取天上星 版 插件引擎 第二版 version 2.0   * By: 摘取天上星! * Emali: happy.yin@qq.com * Date: 2012升级版 **/  $plugin_arr=array();  $plugin_meta=array();  $plugin_remove=array();  $action_arr=array();  $action_meta=array();  $action_remove=array();  $idx=0;  /*   * 执行插件引擎中捆绑的所有函数事件(函数执行顺序参加addPlugin函数添加插件时的第四个参数数字,数字越大优先级越高)   * $tag 要执行的函数集插件标签名   * $args 要往函数中传入的参数,依次按顺序填写,键名同addPlugin添加插件时第三个参数传入的键名、数量对应一致,键名对应的值即传入的参数值,   * 该插件引擎是有返回值的插件引擎   */  function doPlugin($tag,$args=array()){  	global $plugin_arr,$plugin_remove;  	$first=array_search(current($args),$args);  	if(empty($plugin_arr[$tag])) return $args[$first];  	if(isset($plugin_remove[$tag])){  		foreach($plugin_remove[$tag] as $func){  			removePlugin($tag,$func);  		}  	}  	krsort($plugin_arr[$tag]);  	foreach($plugin_arr[$tag] as $plugins){  		foreach($plugins as $plugins){  			$plugins['args']=array_merge($plugins['args'],$args);  			$args[$first]=call_user_func_array($plugins['func'],array_slice($plugins['args'],0,$plugins['args_count']));  		}  	}  	return $args[$first];  }  /* 第一个参数为自定义标签集名,   * 第二个参数是你要向标签集里添加的函数名,   * 第三个数组参数为第二个参数strAndStr1函数对应的参数集,有多少个函数参数,就需要添加多少个数组元素,           参数按照先后顺序依次填写,键值为空即可,且插件里所有函数的参数个数必须一致,一个以上的参数,可多个,           这里的传参数组只需要预写好键名即可,在调用doPlugin插件时给对应的键值传入键名对应的实际参数值即可   * 第四个参数为排序参数,从1到10的纯数字,数值越大执行优先级越高,反之越小,默认为值为最大优先级10   * addPlugin('cleanText','strAndStr1',array('str'=>'','str2'=>''),1);     * addPlugin('cleanText','strAndStr2',array('str'=>'','str2'=>''),2);    */  function addPlugin($tag,$func,$args=array(),$sort=10){  	global $plugin_arr,$plugin_meta,$idx;  	$plugin_arr[$tag][$sort][++$idx]=array('func'=>$func,'args'=>$args,'args_count'=>sizeof($args));  	$plugin_meta[$tag][$func][$idx]=$sort;  }  /*   * 立即删除函数集标签中 的某个函数   * 第一个参数为自定义函数集标签名称   * 第二个参数为要从函数集里 删除的单个函数名称   */  function removePlugin($tag,$func){  	global $plugin_arr,$plugin_meta;  	if(isset($plugin_meta[$tag][$func])){  		foreach($plugin_meta[$tag][$func] as $idx=>$sort){  			unset($plugin_arr[$tag][$sort][$idx]);  		}  		unset($plugin_meta[$tag][$func]);  	}  }  /*   * 在下次执行doPlugin时删除函数集标签中 的某个函数(在doPlugin中的插件函数执行前删除,并且删除后执行插件引擎!)   * 第一个参数为自定义函数集标签名称   * 第二个参数为要从函数集里 删除的单个函数名称   */  function addRemovePlugin($tag,$func){  	global $plugin_remove;  	if(in_array($func,(array)$plugin_remove[$tag])) return ;  	$plugin_remove[$tag][]=$func;  }  /*   * 如下执行插件方法同上述有返回值的执行插件使用方法对应一致,   * 唯一的区别是没有返回值   */  /*   * 执行插件引擎   */  function doAction($tag,$args=array()){  	global	$action_arr,$action_remove;  	if(empty($action_arr[$tag])) return ;  	if(isset($action_remove[$tag])){  		foreach($action_remove[$tag] as $func){  			removeAction($tag,$func);  		}  	}  	krsort($action_arr[$tag]);  	foreach($action_arr[$tag] as $action_sort){  		foreach($action_sort as $action_idx){  			$action_idx['args']=array_merge($action_idx['args'],$args);  			call_user_func_array($action_idx['func'],array_slice($action_idx['args'],0,$action_idx['args_count']));  		}  	}  }  /*   * 向插件引擎里添加函数   */  function addAction($tag,$func,$args=array(),$sort=10){  	global $action_arr,$action_meta,$idx;  	$action_arr[$tag][$sort][++$idx]=array('func'=>$func,'args'=>$args,'args_count'=>sizeof($args));  	$action_meta[$tag][$func][$idx]=$sort;  }  /*   * 从插件引擎里删除 执行的函数   */  function removeAction($tag,$func){  	global $action_arr,$action_meta;  	if(isset($action_meta[$tag][$func])){  		foreach($action_meta[$tag][$func] as $idx=>$sort){  			unset($action_arr[$tag][$sort][$idx]);  		}  		unset($action_meta[$tag][$func]);  	}  }  /*   * 添加预删除函数,该函数会在下次执行插件引擎时,在函数集调用前被删除   */  function addRemoveAction($tag,$func){  	global $action_remove;  	if(in_array($func,(array)$action_remove[$tag])) return ;  	$action_remove[$tag][]=$func;  }  /* 摘取天上星 - 期待更深层次的扩展压缩...*/?>
Copy after login


//执行例子如下

//为插件引擎准备好要用到的测试函数
function str2str2($str){
  return '

P标签开始 '.$str.' P标签结束

';
}
function str3str3($str){
  return 'b标签开始 '.$str.' b标签结束';

}

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

例子一:
//str2str2函数的执行优先级小于str3str3,这里先执行str3str3($str)函数后执行str2str2($str)函数;
//实际运行流程解刨如下:
$str=str3str3('这是要像插件里所有函数传入的参数这里函数str3str3的执行优先级高于str2str2');
$str=str2str2($str);
echo $str; 
/*输出结果浏览器里查看HTML源代码得到如下内容:
 

P标签开始 b标签开始 这是要像插件里所有函数传入的参数这里函数str3str3的执行优先级高于str2str2 b标签结束 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标签开始

P标签开始 这是要像插件里所有函数传入的参数这里函数str2str2的执行优先级高于str3str3 P标签结束

b标签结束
*/
//例子三:
addPlugin('cleanText','str2str2',array('str'=>''),1);
addPlugin('cleanText','str3str3',array('str'=>''),1);
echo doPlugin('cleanText',array('str'=>'当权限排序值大小一致时,后面的函数权限优先级要小于前面的故先添加的函数先执行,这里函数str3str3的执行优先级小于str2str2'));
/* 执行后的HTML源代码结果如下:
b标签开始

P标签开始 当权限排序值大小一致时,后面的函数权限优先级要小于前面的故先添加的函数先执行,这里函数str3str3的执行优先级小于str2str2 P标签结束

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>

?>

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles