In this article, we mainly talk about some
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 PHP 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 PHP plug-ins does not affect the running of core programs.
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 principles of the above PHP plug-in mechanism.
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 principles of the PHP plug-in mechanism are described a lot. Let's look at my implementation scheme below:
Plugin manager PluginManager class:
The following content is referenced by the PHP plug-in mechanism:
- <?
- Class PluginManager
- {
- Private $ _ listeners = array ();
- Public function _ construct ()
- {
- # Here, the $ plugin array contains the information that we obtain from the user
Activated plug-in information
- # For ease of demonstration, we assume that $ plugin contains at least
- # $ Plugin = array (
- # 'Name' =>'Plug-In name ',
- # 'Directory' =>'Plugin 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 an 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 records
- }
- Function register ($ hook, & $ reference,
$ Method)
- {
- // Obtain the implementation method of 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 are some log records
- }
- Function trigger ($ hook, $ data = '')
- {
- $ Result = '';
- // Check whether the hook to be implemented is in the listener 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)
- {
- // Retrieve 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 );
- }
- }
- }
- # Here are some log records
- 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. The # annotation is the part you need to complete on your own, including obtaining the PHP plug-in mechanism and logging.