How to implement website plug-in mechanism in php_PHP tutorial

WBOY
Release: 2016-07-21 15:42:49
Original
799 people have browsed it

The first is the implementation of the plug-in management class:

Copy code The code is as follows:

/**
* STBLOG PluginManager Class
*
* Implementation core class of plug-in mechanism
*
* @package STBLOG
* @subpackage Libraries
* @category Libraries
* @author Saturn
* @link http://www.cnsaturn.com/
*/
class PluginManager
{
/**
* Listen for 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 we obtain that has been activated by the user
#For the convenience of demonstration, we assume that $plugin contains at least
#$plugin = array (
# 'name' => 'Plug-in name',
# 'directory' => 'Plug-in installation directory'
#);
$plugins = get_active_plugins();#This function Please implement it yourself
if($plugins)
{
foreach($plugins as $plugin)
{//Assume that each plug-in folder contains an actions.php file, which is the plug-in's Specific implementation
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 plug-ins
new $class($this);
}
}
}
}
#Do some logging stuff here
}

/**
* Register the plug-in method (hook) that needs to be monitored
*
* @param string $hook
* @param object $reference
* @param string $method
*/
function register($hook, &$reference, $method)
{
//Get the method to be implemented by the plug-in
$key = get_class($reference).'->'.$method;
//Push the plug-in reference and the method into the listening array
$this->_listeners[$hook][$key] = array(&$reference, $method);
#Do some logging stuff here
}
/**
* Trigger a hook
*
* @param string $hook The name of the hook
* @param mixed $data The input parameters of the hook
* @return mixed
*/
function trigger($hook, $data= '')
{
$result = '';
//Check whether the hook to be implemented is in the listening 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)
{
// Get 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 );
}
}
}
#Do some logging stuff here
return $result;
}
}
?>

Then the specific implementation method of the plug-in:
Copy the code The code is as follows:

/**
* This is an implementation of a Hello World simple plug-in
*
* @package DEMO
* @subpackage DEMO
* @category Plugins
* @author Saturn
* @ link http://www.cnsaturn.com/
*/
/**
* Several default rules to note:
* 1. The file name of this plug-in class must be action
* 2. The name of the plug-in class must be {plugin name_actions}
*/
class DEMO_actions
{
//The parameter of the parsing function is a reference to pluginManager
function __construct( &$pluginManager)
{
//Register this plug-in
//The first parameter is the name of the hook
//The second parameter is the reference to pluginManager
//The third It is the method executed by the plug-in
$pluginManager->register('demo', $this, 'say_hello');
}

function say_hello()
{
echo 'Hello World';
}
}
?>

For example, if I want to put say_hello on the home page of my blog, Index.php, then what you need in index.php Write somewhere: (author’s original words)
Copy code The code is as follows:

$pluginManager->trigger( 'demo','');

The above is the implementation of a plug-in mechanism, over!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320869.htmlTechArticleFirst is the implementation of the plug-in management class: Copy the code as follows: ? /** * STBLOG PluginManager Class * * Implementation core class of plug-in mechanism * * @package STBLOG * @subpackage Libraries...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template