The first is the implementation of the plug-in management class:
Copy codeThe Code is as follows:
<?
/**
* STBLOG PluginManager Class
*
* Core plug-in implementation class
*
* @ Package STBLOG
* @ Subpackage Libraries
* @ Category Libraries
* @ Author Saturn
* @ Link http://www.cnsaturn.com/
*/
Class PluginManager
{
/**
* Listen to registered plug-ins
*
* @ Access private
* @ Var array
*/
Private $ _ listeners = array ();
/**
* Constructor
*
* @ Access public
* @ Return void
*/
Public function _ construct ()
{
# Here, the $ plugin array contains the plug-in information that has been activated by the user.
# For ease of demonstration, we assume that $ plugin contains at least
# $ Plugin = array (
# 'Name' => 'plug-In name ',
# 'Directory '=> 'plugin installation directory'
#);
$ Plugins = get_active_plugins (); # implement this function on your own.
If ($ plugins)
{
Foreach ($ plugins as $ plugin)
{// Assume that each plug-in folder contains an actions. php file, which is the specific implementation of the plug-in.
If (@ file_exists (STPATH. 'ins INS/'. $ plugin ['Directory']. '/actions. php '))
{
Include_once (STPATH. 'ins INS/'. $ plugin ['Directory']. '/actions. php ');
$ Class = $ plugin ['name']. '_ actions ';
If (class_exists ($ class ))
{
// Initialize all plug-ins
New $ class ($ this );
}
}
}
}
# Here are some log records
}
/**
* Register the plug-in method (Hook) to be listened)
*
* @ Param string $ hook
* @ Param object $ reference
* @ Param string $ method
*/
Function register ($ hook, & $ reference, $ method)
{
// Obtain the implementation method of the plug-in
$ Key = get_class ($ reference). '->'. $ method;
// Push the reference of the plug-in along with the method into the listening array
$ This-> _ listeners [$ hook] [$ key] = array (& $ reference, $ method );
# Here are some log records
}
/**
* Trigger a hook
*
* @ Param string $ hook name
* @ Param mixed $ input parameter of the data hook
* @ Return mixed
*/
Function trigger ($ hook, $ data = '')
{
$ Result = '';
// Check whether the hook to be implemented is in the listener Array
If (isset ($ this-> _ listeners [$ hook]) & is_array ($ this-> _ listeners [$ hook]) & count ($ this-> _ listeners [$ hook])> 0)
{
// Start of loop call
Foreach ($ this-> _ listeners [$ hook] as $ listener)
{
// Retrieve the reference and method of the plug-in object
$ Class = & $ listener [0];
$ Method = $ listener [1];
If (method_exists ($ class, $ method ))
{
// Dynamically call the plug-in method
$ Result. = $ class-> $ method ($ data );
}
}
}
# Here are some log records
Return $ result;
}
}
?>
Then, the specific implementation method of the plug-in is as follows:
Copy codeThe Code is as follows:
<?
/**
* This is an implementation of a simple Hello World plug-in.
*
* @ Package DEMO
* @ Subpackage DEMO
* @ Category Plugins
* @ Author Saturn
* @ Link http://www.cnsaturn.com/
*/
/**
* Note the following default rules:
* 1. The file name of this plug-in class must be action.
* 2. The plug-in class name must be {plug-in name_actions}
*/
Class DEMO_actions
{
// The parameters of the parsing function are referenced by pluginManager.
Function _ construct (& $ pluginManager)
{
// Register this plug-in
// The first parameter is the name of the hook.
// The second parameter is the reference of pluginManager.
// The third method is the method executed by the plug-in.
$ PluginManager-> register ('Demo', $ this, 'Say _ hello ');
}
Function say_hello ()
{
Echo 'Hello world ';
}
}
?>
For example, if you want to put say_hello to Index. php on the homepage of my blog, write it at a location in index. php: (the author's original words)
Copy codeThe Code is as follows:
$ PluginManager-> trigger ('Demo ','');
The above is the implementation of a plug-in mechanism. over!