The first is the implementation of the plug-in's management class:
Copy Code code as follows:
?
/**
* Stblog PlugInManager Class
*
* Implementation of the plug-in mechanism core class
*
* @package Stblog
* @subpackage Libraries
* @category Libraries
* @author Saturn
* @link http://www.cnsaturn.com/
*/
Class PlugInManager
{
/**
* Listen for Registered plugins
*
* @access Private
* @var Array
*/
Private $_listeners = Array ();
/**
* Constructor
*
* @access Public
* @return void
*/
Public Function __construct ()
{
#这里 $plugin array contains information about plug-ins that we have activated by the user
#为演示方便, we assume that the $plugin contains at least
# $plugin = Array (
# ' name ' => ' plugin name ',
# ' directory ' => ' plugin installation directory '
#);
$plugins = Get_active_plugins (); #这个函数请自行实现
if ($plugins)
{
foreach ($plugins as $plugin)
{//Assume that each plug-in folder contains a actions.php file, which is a specific implementation of the plug-in
if (@file_exists Stpath. plugins/'. $plugin [' directory ']. ' /actions.php '))
{
Include_once (Stpath. ') plugins/'. $plugin [' directory ']. ' /actions.php ');
$class = $plugin [' name ']. ' _actions ';
if (class_exists ($class))
{
Initialize all plugins
New $class ($this);
}
}
}
}
#此处做些日志记录方面的东西
}
/**
* Registration required to monitor the plug-in method (Hook)
*
* @param string $hook
* @param object $reference
* @param string $method
*/
function Register ($hook, & $reference, $method)
{
Get the method that the plug-in will implement
$key = Get_class ($reference). ' -> '. $method;
Push the plug-in's reference along with the method into the listening array
$this->_listeners[$hook] [$key] = Array (& $reference, $method);
#此处做些日志记录方面的东西
}
/**
* Trigger a hook
*
* @param string $hook The name of the hook
* @param mixed $data Hook entry
* @return Mixed
*/
Function trigger ($hook, $data = ')
{
$result = ';
See if the hook to implement is in the listening array
if (Isset ($this->_listeners[$hook]) && Is_array ($this->_listeners[$hook]) && count ($this-> _listeners[$hook]) > 0)
{
Loop call Start
foreach ($this->_listeners[$hook] as $listener)
{
To remove a reference and method for a plug-in object
$class =& $listener [0];
$method = $listener [1];
if (Method_exists ($class, $method))
{
Ways to invoke Plug-ins dynamically
$result. = $class-> $method ($data);
}
}
}
#此处做些日志记录方面的东西
return $result;
}
}
?>
Then the plug-in's specific implementation methods:
Copy Code code as follows:
?
/**
* This is a Hello World simple plug-in implementation
*
* @package DEMO
* @subpackage DEMO
* @category Plugins
* @author Saturn
* @link http://www.cnsaturn.com/
*/
/**
* Several default rules to note:
* 1. The filename of this plug-in class must be an action
* 2. The name of the plug-in class must be {plug-in name _actions}
*/
Class Demo_actions
{
The parameter of the parse function is a reference to PlugInManager
function __construct (& $pluginManager)
{
Register this plugin
The first argument is the name of the hook
The second argument is a reference to the PlugInManager
The third is the method that the plug-in executes
$pluginManager->register (' demo ', $this, ' Say_hello ');
}
function Say_hello ()
{
Echo ' Hello world ';
}
}
?>
For example, I would like to put Say_hello on my blog home index.php, then you in the index.php in a position to write down: (the author's exact words)
Copy Code code as follows:
$pluginManager->trigger (' demo ', ');
The above is a plug-in mechanism implementation, over!