PHP implementation of the website plug in mechanism

  • 2020-03-31 16:43:52
  • OfStack

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

<? 
/** 
* 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 
{ 
 
private $_listeners = array(); 
 
public function __construct() 
{ 
# Here, $plugin The array contains information that we get about the plug-ins that have been activated by the user  
# For demonstration purposes, we assume $plugin At least including  
#$plugin = array( 
# 'name' => ' The plug-in name ', 
# 'directory'=>' Plug-in installation directory ' 
#); 
$plugins = get_active_plugins();# Please implement this function yourself  
if($plugins) 
{ 
foreach($plugins as $plugin) 
{//Assume that each plug-in folder contains an action.php file that is the concrete 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 plug-ins
new $class($this); 
} 
} 
} 
} 
# Do some logging here  
} 

 
function register($hook, &$reference, $method) 
{ 
//Gets the method to be implemented by the plug-in
$key = get_class($reference).'->'.$method; 
//Push the plug-in reference along with the method into the listening array
$this->_listeners[$hook][$key] = array(&$reference, $method); 
# Do some logging here  
} 
 
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) 
{ 
//Fetch the reference and method of the plug-in object
$class =& $listener[0]; 
$method = $listener[1]; 
if(method_exists($class,$method)) 
{ 
//Calls the plug-in's methods dynamically
$result .= $class->$method($data); 
} 
} 
} 
# Do some logging here  
return $result; 
} 
} 
?>

Then there is the concrete implementation method of the plug-in:

<? 
/** 
*  This is a Hello World Simple plug-in implementation  
* 
* @package DEMO 
* @subpackage DEMO 
* @category Plugins 
* @author Saturn 
* @link http://www.cnsaturn.com/ 
*/ 
 
class DEMO_actions 
{ 
//The argument to the parse function is a reference to the pluginManager
function __construct(&$pluginManager) 
{ 
//Register the plug-in
//The first parameter is the name of the hook
//The second parameter is a reference to 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, if I want to put say_hello on the front page of my blog, index.php, then somewhere in index.php you write:

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

That's the implementation of a plug-in mechanism, over!

Related articles: