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

<?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;  }  /* 摘取天上星 - 期待更深层次的扩展压缩...*/?>


//执行例子如下

//为插件引擎准备好要用到的测试函数
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
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
How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

Increase PHP Performance: Caching Strategies & TechniquesIncrease PHP Performance: Caching Strategies & TechniquesMay 11, 2025 am 12:08 AM

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc

See all articles

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 Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.