PHP under the B/s programming mode to implement the C/S software programming mode plug-in engine function!

Source: Internet
Author: User
Tags tag name

<?php/** * Extract Sky Star version plugin engine version 2.0 * by: Pick up the stars!  * Emali: [email protected] * date:2012 upgrade version **/$plugin _arr=array ();  $plugin _meta=array ();  $plugin _remove=array ();  $action _arr=array ();  $action _meta=array ();  $action _remove=array ();  $idx = 0; /* * Perform all function events bundled in the plug-in engine (the function execution sequence takes the fourth parameter number when the Addplugin function adds the plug-in, the higher the number priority) * $tag the function set plug-in tag name to execute * $args the parameters to be passed in the function, sequentially filled in order, key name same as Addplugin Add plug-in when the third parameter passed the key name, the number corresponds to the same, the value of the key name is passed in the parameter value, * The plug-in engine is a return value of the plug-in engine */function Doplugin ($tag, $args =array ()) {Global $p  Lugin_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]; }/* The first parameter is a custom tag set name, * The second parameter is the function name you want to add to the tag set, * The third array parameter is the parameter set corresponding to the second parameter strAndStr1 function, how many function parameters, how many array elements need to be added, the parameters according to successively Order fill in sequence, the key value is empty, and the plug-in all the functions of the parameters must be consistent, more than one parameter, can be multiple, here the parameter group only need to pre-write a good key name, in the call Doplugin plug-in to the corresponding key value passed the key name corresponding to the actual parameter value can be * Fourth parameters From 1 to 10 of pure numbers, the higher the value of the higher the execution priority, the smaller the lower, the default value is the maximum priority of ten * 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 ' and ' $args, ' Args_count ' =>sizeof ($args));  $plugin _meta[$tag] [$func] [$idx]= $sort; }/* Immediately delete a function in the function set label * The first parameter is a custom function set label name * The second parameter is the name of a single function to remove from the set of functions */function Removeplugin ($tag, $func) {Globa  L $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]); The next time you execute Doplugin, remove a function from the function set label (delete the plug-in function in Doplugin before executing it, and then execute the plug-in engine after you delete it!)  * The first parameter is a custom function set label name * The second parameter is the name of a single function to be removed from the function set */function Addremoveplugin ($tag, $func) {global $plugin _remove;  if (In_array ($func, (array) $plugin _remove[$tag])) return;  $plugin _remove[$tag][]= $func;  }/* * The following execution plug-in method corresponds to the execution plug-in method with the return value above, * The only difference is that there is no return value */* * Execute plug-in engine */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 '])); }}/* * Add function to plugin engine */function Addaction ($tag, $func, $args =array (), $sort =10) {GlobAl $action _arr, $action _meta, $idx;  $action _arr[$tag] [$sort][++ $idx]=array (' func ' = $func, ' args ' and ' $args, ' Args_count ' =>sizeof ($args));  $action _meta[$tag] [$func] [$idx]= $sort;  }/* * Remove the executed function from the plugin engine */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]);  }/* * Add a pre-delete function that will be deleted before the function set is called the next time the plug-in engine executes */function addremoveaction ($tag, $func) {global $action _remove;  if (In_array ($func, (array) $action _remove[$tag])) return;  $action _remove[$tag][]= $func; }/* Extract Sky stars-expect deeper extended compression ...*/?>


<?php

Examples of implementation are as follows

Prepare the test function to be used for the plug-in engine.
function Str2str2 ($STR) {
Return ' <p>p label start '. $str. ' p tag end <p/> ';
}
function Str3str3 ($STR) {
Return ' <b style= ' color:red ' >b label start '. $str. ' B tag end <b/> ';

}

//NOTE: When testing three examples, be sure to have one test, please comment out other redundant examples when testing, otherwise you will not see the actual contrast effect of the plugin engine privilege priority to produce abnormal results!

Example one:
The execution priority of the STR2STR2 function is less than STR3STR3, where the STR3STR3 ($STR) function is executed before the STR2STR2 ($STR) function is performed;
The actual operating process is solved as follows:
$str =STR3STR3 (' This is going to be like all the functions passed in the plug-in parameter here the function STR3STR3 the execution priority is higher than str2str2 ');
$str =str2str2 ($STR);
Echo $str;
/* Output results in the browser to view the HTML source code to get the following content:
<p>p label start <b style= "color:red" >b label start this is to be like the plug-in all functions passed in the parameter here function STR3STR3 execution priority higher than STR2STR2 B label end <b/> p label end <p/>
*/
Addplugin (' Cleantext ', ' str2str2 ', Array (' str ' = = '), 1);
Addplugin (' Cleantext ', ' STR3STR3 ', Array (' str ' = = '), 10);
echo doplugin (' Cleantext ', Array (' str ' = ') which is going to be like all functions passed in the plugin parameter here the function STR3STR3 execution priority higher than str2str2 '));
Example two:
Addplugin (' Cleantext ', ' str2str2 ', Array (' str ' = = '), 10);
Addplugin (' Cleantext ', ' STR3STR3 ', Array (' str ' = = '), 1);
echo doplugin (' Cleantext ', Array (' str ' = ') which is going to be like all functions passed in the plugin parameter here the function STR2STR2 execution priority higher than STR3STR3 '));
/* Run result HTML page source code is as follows:
<b style= "color:red" >b label start <p>p label start this is going to be like the plug in all the functions passed in the parameters here function Str2str2 execution priority higher than STR3STR3 p label end <p/> B label end <b/>
*/
Example three:
Addplugin (' Cleantext ', ' str2str2 ', Array (' str ' = = '), 1);
Addplugin (' Cleantext ', ' STR3STR3 ', Array (' str ' = = '), 1);
echo doplugin (' Cleantext ', Array (' str ' + ') when the permission sort value is the same size, the subsequent function permission priority is smaller than the preceding so the first added function first executes, here the function Str3str3 execution priority is less than STR2STR2 ' ));
/* After executing the HTML source code results are as follows:
<b style= "color:red" >b label start <p>p label start when the permission sort value is the same size, then the function permission priority is smaller than the preceding so the first added function executes first, Here the function Str3str3 execution priority is less than STR2STR2 p label end <p/> B label End <b/>

*/


Test Doaction Execution Plugin example (the plugin does not return a value, only executes!) )
*/* Note, the plugin return value plug-in, so only used as output or direct execution occasions, priority with the Doplugin plug-in priority settings, it is not detailed!
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 ' = ' = ' parameter to eject ') ');
The following is the result of the running HTML source code:

To eject the parameter 1+2<script>alert (' parameters to eject ');</script>

?>

PHP under the B/s programming mode to implement the C/S software programming mode plug-in engine function!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.