One implementation [PHP] PHP plug-in mechanism

Plug-in, that is, Plug-in, it refers to a particular kind of functional modules (usually implemented by third-party developers), which is characterized by: when you need it to activate it, do not need it to disable / remove it; and whether it is active or disabled will not affect the operation of the system core modules, that plug-in is a non-invasive modular design to achieve a loosely coupled core program and plug-ins. Wordpress is a typical example of the many third-party plug-ins, such as Akimet plug-in for the user comments were Spam filter.

A robust plug-in mechanism, I think it must have the following characteristics:

  • Dynamically monitor and loading plug-ins (Lookup) 
  • Dynamic plug-in trigger 
  • Achieve more than two points do not affect the core of the program running

 

To implement the program, plug-in, we should first think of different definitions hook (Hooks); "hook" is a very logical concept image, you may think it is reserved for the system plug-trigger condition. Its logic works as follows: When the system to execute a hook, the hook will determine the condition is met; if met, would instead go to call the hook function developed by, and then return the rest of the program continues; if not satisfied , you can skip. It's a bit like a compilation of "interruption protection" logic.

Some hook system may be designed in advance, such as hooks before I give comments on Spam filtering, it usually has a core system developers to design into the review process logic; it may be another type of hook by the user self-customized (developed by third-party developers), normally present in the presentation layer, such as a common form of PHP to display the page.

You may feel the above words more boring, people drowsy; but I wrote the following code to read and understand the above principle is essential.

Carried out in the core PHP plug-in mechanism to achieve the following, the core of the mechanism is divided into three large pieces:

  • A plug-in manager class: This is the core of the core. It is a program global Global target application. It has three main responsibilities: 
    • Is responsible for monitoring has registered all plug-ins, plug-ins and instantiate these objects. 
    • Responsible for registering all plug-ins. 
    • When hook condition is satisfied, trigger the corresponding object methods.

         

  • Function plug-realization: This is mostly done by third-party developers, but need to follow certain rules, this rule is provided for plug-in mechanism, due to the different mechanisms and different plug-ins, the following shows the code you will see this rule. 
  • Plug-trigger: the trigger condition is the hook. Specifically, this is a piece of code, placed where you need to plug-in implementation for triggering the hook.

 

Implementation: PluginManager categories:

<?php
/** 
* PluginManager Class 
* 
* Plug-in mechanism to achieve core classes 
* 
* Author: Fan children still Betsy
*/
class PluginManager 
{ 
  /** 
   * Monitor registered plug-ins 
   * 
   * @access private 
   * @Var array 
   */
  private $_listeners = array(); 
   /** 
   * Constructor 
   * 
   * @access public 
   * @return void 
   */
  public function __construct() 
  { 
    # Here $ plugin array contains we get has been activated by the user plug-in information 
   # For the convenience of presentation, we assume that $ plugin contains at least 
   # $ plugin = Array ( 
    #   'name' => 'plug-in name', 
    #   'Directory' => 'plug-in installation directory' 
    # );
    
    // $ plugins = get_active_plugins (); # this function at your own realization 
    The final effect of the data structure achieve the following functions // 
    $ plugins = Array ( 
Array ( "Directory" => "Demo", "name" => "the DEMO" )
);
IF ( $ plugins ) { the foreach ( $ plugins AS $ plugin ) {
// is assumed that each file contains a widget actions.php folder, which is embodied widget IF (@ the 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 // $ this is a reference to this class of new new $ class ( $ the this ); } } } } # Here to do something on the records that log } /** * Registration required listening plug-in methods (hook) * * @param string $hook * @param object $reference * @param string $method */ function register($hook, &$reference, $method) { // Get method to be implemented plug $ Key = the get_class ( $ Reference ) .'-> '. $ Method ; // references together with the push method into listening array widget $ the this -> _ the Listeners [ $ Hook ] [ $ Key ] = Array (& $ Reference , $ Method ); # here do something on the records log } / * * * Trigger a hook * * @Param string $ hook hook name * @Param mixed $ data hook to the Senate * @return mixed */ function trigger($hook, $data='') { $ the Result = '' ; // Check hook to be implemented, whether the listener into an array of IF ( isset ( $ the this -> _ in the Listeners [ $ Hook ]) && is_array ( $ the this -> _ in the Listeners [ $ Hook ]) && COUNT ( the this $ -> _ the Listeners [ $ Hook ])> 0 ) { // loop start calling foreach ( $ the this -> _ in the Listeners [ $ Hook ] AS $ listener ) { // remove the plug-in object references and methods $ class = & $ listener [0 ]; $ Method = $ listener [. 1 ]; IF ( method_exists ( $ class , $ Method )) { // dynamic method invocation widget $ Result =. $ Class -> $ Method ( $ Data ); } } } # Here to do something on the records that log return $ the Result ; } } define(STPATH, "./"); $pluginManager=new PluginManager(); $pluginManager->trigger("demo");

 

Note that you have to set it up to global class, in all places need to use plug-in, priority loading.

With the # comment is that you need to complete on their own part, including the acquisition and logging plug-ins, and so on.


Here is the realization of a simple plug-in.

 

<?
/**
* This is a simple plug-realization Hello World
*/
/**
* Note that several default rules:
* 1. The plug-in class file name must be action
* 2. The name of the plugin class must be _actions} {plugin name
*/
class DEMO_actions
{
    // parameter parsing function is pluginManager reference 
    function __construct (& $ pluginManager)
    {
        // register the plugin
         // The first parameter is the name of the hook
         // The second parameter is a reference to the pluginManager
         // The third method is performed by the plug- 
        $ pluginManager-> the Register ( ' Demo ' , $ the this , ' say_hello ' );
    }
     
    function say_hello()
    {
        echo 'Hello World';
    }
}
?>

 

This is a simple Hello World plug for output word. In practice, say_hello may include operation of the database, or some other specific logic such as call API.

The default rule is determined by its own plug-in implementation of the core system developers. For example, some of the default rule in this case I have written very clearly in the Note, this is not repeated here. Special attention is required not to repeat the name of the hook.

The final step is to define the hook trigger, where you will hook on, the above method where the plug-in will be triggered. For example, I want to put my blog say_hello Home Index.php, then you write down in a certain position in index.php:

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

 

The first argument represents the name of the hook, in the present embodiment it is Demo; second parameter is parameter plug the inlet of the corresponding method, since in this example no input parameters, it is empty.

Summary 

This article describes a method and ideas plug-in mechanism implemented in PHP, and my own understanding of the plug-in mechanism. Practical applications. To be adjusted in accordance with the framework and requirements!

 

Guess you like

Origin www.cnblogs.com/richerdyoung/p/11966465.html