How PHP implements the plugin mechanism for Web sites

Source: Internet
Author: User

PHP implementation of the website plug-in mechanism of the method these days to make a plug-in in the site to implement the function of Google a bit, found an article feel to me a great help, post out and everyone to share, nonsense not much to say, directly post code for everyone to analyze.

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

Copy CodeThe code is as follows:
<?
/**
* Stblog PlugInManager Class
*
* Plug-in mechanism implementation core class
*
* @package Stblog
* @subpackage Libraries
* @category Libraries
* @author Saturn
* @link http://www.cnsaturn.com/
*/
Class PlugInManager
{
/**
* Listen to Registered plugins
*
* @access Private
* @var Array
*/
Private $_listeners = Array ();
/**
* Constructor function
*
* @access Public
* @return void
*/
Public Function __construct ()
{
#这里 $plugin array contains the plugin information that we have been activated by the user
#为演示方便, we assume that $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 the 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);
}
}
}
}
#此处做些日志记录方面的东西
}

/**
* Register the Plugin method (hooks) that need to be monitored
*
* @param string $hook
* @param object $reference
* @param string $method
*/
function Register ($hook, & $reference, $method)
{
Get the plug-in method to implement
$key = Get_class ($reference). ' -$method;
Push the plugin's reference together with the method into the listener array
$this->_listeners[$hook [$key] = Array (& $reference, $method);
#此处做些日志记录方面的东西
}
/**
* Trigger a hook
*
* @param string $hook The name of the hook
* @param mixed $data The entry of the hook
* @return Mixed
*/
Function trigger ($hook, $data = ")
{
$result = ";
See if the hooks to implement are in the listener 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)
{
Remove references and methods from plug-in objects
$class =& $listener [0];
$method = $listener [1];
if (Method_exists ($class, $method))
{
Methods for dynamically invoking plug-ins
$result. = $class $method ($data);
}
}
}
#此处做些日志记录方面的东西
return $result;
}
}
?>


Then the specific implementation of the plug-in method:

Copy CodeThe code is as follows:
<?
/**
* This is the implementation of a Hello World simple plugin
*
* @package DEMO
* @subpackage DEMO
* @category Plugins
* @author Saturn
* @link http://www.cnsaturn.com/
*/
/**
* There are several default rules to note:
* 1. The file name 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 argument of the analytic function is a reference to the PlugInManager
function __construct (& $pluginManager)
{
Register this plugin
The first parameter is the name of the hook
The second argument is a reference to the PlugInManager
The third is the method that the plugin executes
$pluginManager->register (' demo ', $this, ' Say_hello ');
}

function Say_hello ()
{
Echo ' Hello world ';
}
}
?>


For example, I want to put Say_hello on my blog home index.php, then you in index.php somewhere in a position to write: (the author's exact words)

Copy CodeThe code is as follows:
$pluginManager->trigger (' demo ', ');


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

How PHP implements the plugin mechanism for Web sites

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.