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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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 Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)