The starting point of this article is my understanding of the plug-in mechanism and its implementation in PHP. This solution is only one of the Implementation Solutions of the plug-in mechanism in PHP. Write it down and share it with you. You are welcome to discuss it together. Plug-ins, that is, Plug-in, refer to a specific type of functional modules (usually implemented by third-party developers). They are characteristic of activating a Plug-in when you need it.
The starting point of this article is my understanding of the plug-in mechanism and its implementation in PHP. This solution is only one of the Implementation Solutions of the plug-in mechanism in PHP. Write it down and share it with you. You are welcome to discuss it together. Plug-ins, that is, Plug-in, refer to a specific type of functional modules (usually implemented by third-party developers). They are characteristic of activating a Plug-in when you need it.
The starting point of this article is my understanding of the plug-in mechanism and its implementation in PHP. This solution is only one of the Implementation Solutions of the plug-in mechanism in PHP. Write it down and share it with you. You are welcome to discuss it together.
Plug-ins, that is, Plug-in, refer to a specific type of functional modules (usually implemented by third-party developers). They are characteristic of activating a Plug-in when you need it, disable/delete a plug-in when you do not need it. Whether it is activated or disabled, it does not affect the operation of the system's core modules. That is to say, the plug-in is a non-intrusive modular design, loose coupling between the core program and plug-in program is realized. A typical example is many third-party plug-ins in Wordpress. For example, the Akimet plug-in is used to filter comments by Spam.
I think a robust plug-in mechanism must have the following features:
Plug-in dynamic listening and loading (Lookup)
Dynamic triggering of plug-ins
The implementation of the above two points does not affect the running of the core program.
To implement plug-ins in a program, we should first think of defining different Hooks. "Hooks" are a very visual logical concept, you can think of it as a trigger condition for system-reserved plug-ins. Its Logic principle is as follows: when the system executes a hook, it will determine whether the Hook's conditions are met; If yes, it will call the Hook's functions first, then return to continue to execute the remaining program; if not, Skip. This is a bit like the interrupt protection logic in assembly.
Some hooks may have been designed in advance by the system. For example, the hooks I mentioned earlier for commenting on Spam filtering are usually designed by core system developers into the comment processing logic; another type of hooks may be customized by users (developed by third-party developers) and usually exist in the presentation layer, such as a common PHP form display page.
You may feel bored and sleepy. But to understand the code I wrote below, it is essential to understand the above principles.
The following describes the Core Implementation of the plug-in mechanism in PHP. The core of the entire mechanism is divided into three parts:
A plug-in manager class: this is the core. It is a Global object of an application. It has three main responsibilities:
Monitors all registered plug-ins and instantiates these plug-ins.
Registers all plug-ins.
When the hook condition is met, the corresponding object method is triggered.
Plug-in function implementation: This is mostly done by third-party developers, but certain rules need to be followed. This rule is defined by the plug-in mechanism and varies depending on the plug-in mechanism, the following code shows the rule.
Plug-in trigger: the trigger condition of the hook. Specifically, this is a small piece of code, which is placed where you need to implement the plug-in to trigger this hook.
The principle is explained in a lot. Let's look at my implementation scheme below:
Plugin manager PluginManager class:
Reference content is as follows:
'Plug-In name', # 'Directory '=> 'plug-in installation directory' #); $ plugins = get_active_plugins (); # implement this function on your own 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. 'ins INS /'. $ plugin ['directory']. '/actions. php ') {include_once (STPATH. 'ins INS /'. $ plugin ['directory']. '/actions. php '); $ class = $ plugin ['name']. '_ actions'; if (class_exists ($ class) {// Initialize all plug-ins new $ class ($ this) ;}}}# Here are some log record items}/*** register the plug-in method to be monitored (Hook) ** @ param string $ hook * @ param object $ reference * @ param string $ method */function register ($ hook, & $ reference, $ method) {// obtain the method to be implemented by the plug-in $ key = get_class ($ reference ). '-> '. $ method; // push the reference of the plug-in along with the method into the listening array $ this-> _ listeners [$ hook] [$ key] = array (& $ reference, $ method); # Here is something about logging}/*** trigger a hook ** @ param string $ name of the hook * @ param mixed $ data hook Sub-input parameter * @ return mixed */function trigger ($ hook, $ data = '') {$ result =''; // view the hook to be implemented, in the listener array, if (isset ($ this-> _ listeners [$ hook]) & is_array ($ this-> _ listeners [$ hook]) & count ($ this-> _ listeners [$ hook])> 0) {// start the foreach loop call ($ this-> _ listeners [$ hook] as $ listener) {// retrieve the reference and method of the plug-in object $ class = & $ listener [0]; $ method = $ listener [1]; if (method_exists ($ class, $ method )) {// dynamically call the ins method $ result. = $ clas S-> $ method ($ data) ;}}# return $ result ;}?>
The core of the entire plug-in mechanism is completed when no more than 100 lines of comments are added to the Code above. It must be noted that you must set it to a global class and load the plug-in first. # Note the content you need to complete on your own, including obtaining plug-ins and logging.
The following is a simple plug-in implementation.
Reference content is as follows:
Register ('Demo', $ this, 'Say _ hello');} function say_hello () {echo 'Hello world' ;}}?>
This is a simple Hello World plug-in used to output a sentence. In practice, say_hello may include database operations or other specific logic, such as calling Akimet APIs.
The default rules implemented by the plug-in are determined by the core system developer. For example, some of the default rules in this example are clearly described in the comments. Note that the hook name must be unique.
The last step is to define the hook trigger, where you put the hook, and where the above plug-in method will start. For example, if you want to put say_hello to Index. php on the homepage of my blog, write it at a location in index. php:
The reference content is as follows: $ pluginManager-> trigger ('Demo ','');
The first parameter indicates the hook name. In this example, It is demo. The second parameter is the entry parameter of the method corresponding to the plug-in. It is null because no parameter is input in this example.
Summary
This article introduces a method and idea for implementing the plug-in mechanism in PHP and my understanding of the plug-in mechanism. It may be difficult to understand this first time. However, when you combine the real examples and think about the program running process, the idea may be clearer.
Http://www.chinaz.com/program/2009/1103/96834_2.shtml