Typecho process principle and plug-in mechanism analysis (second bomb)

Source: Internet
Author: User
Tags throw exception

Typecho process principle and plug-in mechanism analysis (second bomb) Ride 393Published on April 02, 2014
    • Recommended 1 Recommendations
    • Collection Collection,3.7k view

The last time I said Typecho general process, today simply say the plug-in mechanism and the method of writing plug-ins.

Or the first index.php?

if ([email protected]include_once ‘config.inc.php‘) {    file_exists(‘./install.php‘) ? header(‘Location: install.php‘) : print(‘Missing Config File‘); exit;}/** 初始化组件 */Typecho_Widget::widget(‘Widget_Init‘);/** 注册一个初始化插件 */Typecho_Plugin::factory(‘index.php‘)->begin();/** 开始路由分发 */Typecho_Router::dispatch();/** 注册一个结束插件 */Typecho_Plugin::factory(‘index.php‘)->end();

A careful friend may find the last time

/** 注册一个初始化插件 */Typecho_Plugin::factory(‘index.php‘)->begin();/** 注册一个结束插件 */Typecho_Plugin::factory(‘index.php‘)->end();

These two lines of code we did not mention, this is why? This is because the previous issue was primarily an analysis of the system execution process, and we assumed that no plug-ins were installed, and that the two lines of code did not have any effect if the plug-in was not installed.

As we all know, most of the plug-ins are using the hook mechanism, Typecho is no exception, these two lines of code is the system in the index.php pre-set two plug-in interface, respectively, the entire program flow in the first and last interface. How is this plug-in interface executed?

We take HelloWorld the plug-in as an example, the HelloWorld plug-in uses the interface admin/menu.php , the code is as follows:

<?php Typecho_Plugin::factory(‘admin/menu.php‘)->navBar(); ?>

HelloWorldPlug-in code, in usr/plugins/HelloWorld/Plugins.php :

<?php/** * Hello World * *@package HelloWorld *@author qining *@version 1.0.0 *@link http://typecho.org * *ClassHelloworld_pluginImplementstypecho_plugin_interface{/** * Activate plugin method, if activation fails, throw exception directly * *@access Public *@return void *@throws typecho_plugin_exception * *PublicStaticfunctionActivate() {Typecho_plugin::factory (' admin/menu.php ')->navbar =Array' Helloworld_plugin ',' Render '); }/** * Disable Plugin method, if disable fails, throw exception directly * *@static *@access Public *@return void *@throws typecho_plugin_exception * *PublicStaticfunctionDeactivate(){}/** * Get Plugin configuration Panel * *@access Public *@param typecho_widget_helper_form $form Configuration Panel *@return void */PublicStaticfunctionConfig(Typecho_widget_helper_form$form) {/** Category name */$name =New Typecho_widget_helper_form_element_text (' word ',Null' Hello World ', _t (' Say something ');$form->addinput ($NAME); }/** * Individual user's configuration panel * *@access Public *@param typecho_widget_helper_form $form *@return void */Publicstatic function personalconfig(typecho_widget_helper_form $form) {} /** * plug-in implementation method * * @access public * @return void *  /public static function render() { echo ' <span class= ' mes Sage Success ">". Typecho_widget::widget ('widget_options ')->plugin (' HelloWorld ')->word. ' </span> ';}}                 

Typecho in the Database Options table, there is a name plugins field, which records the entire program has activated plug-in interface mount situation, the field is not activated any plug-in when plugins :

Array(    [activated] => Array        (        )    [handles] => Array        (        ))

Next we activate the program comes with the HelloWorld plugin to see what changes, after activating the plugins field becomes:

  array ([activated] = array ([HelloWorld] = array ([handles] = 
       
        array ([Admin/menu.php:navbar] = 
        array ([0] = array ([0" = Helloworld_plugin [1] = render)))) [Handles] = array ([Admin/menu.php:navbar] = array ([0] = = array ([0] = Helloworld_plugin [1] = render)))            
       

As can be seen, after activating the plugin, the program is based on the plugin's activation function

    public static function activate() { Typecho_Plugin::factory(‘admin/menu.php‘)->navBar = array(‘HelloWorld_Plugin‘, ‘render‘); }

The system associates the HelloWorld plug-in with the interface in the database (‘admin/menu.php‘)->navBar() ,
It is the function of the class that is associated with the specific HelloWorld_Plugin render .

In fact, here we can see a general, when we visit the system backstage, when the execution to

<?php Typecho_Plugin::factory(‘admin/menu.php‘)->navBar(); ?>

This code is:

    • Retrieved from the Database plugins field, retrieved to the [admin/menu.php:navBar] next mount a plugin, specifically the HelloWorld_Plugin function of the class, the render system executes and outputs the HelloWorld navigation bar in the background.

    • So if no plug-in is retrieved, the program does not perform any action and executes the code below , which is why the code for the plug-in interface can be skipped directly when parsing the system flow.

A lot of similar plug-in interface is set up in the key place of the whole program, it is convenient to write plug-ins without modifying the program source code to complete our specific functions, there are two ways to set up the plug-in interface:

    • One is Typecho_Plugin::factory , defined in the var/Typecho/Plugin.php

    • The other is $this->pluginHandle() , defined in the var/Typecho/Widget.php

The second is the interface that can pass parameters, and this will be done later.

Finally, someone to ask, the program is set up so many interfaces, even if a plug-in is not installed to be executed, how inefficient?

The answer is yes, it must be executed, but the database is read only once, and then there is memory, each subsequent query is in memory contrast, more than two lines of PHP code basically inefficient impact. Specific speed how to use the WP you understand ~ ~

The next section, according to the specific interface, the demo plugin writing ~

    • Typecho

Typecho process principle and plug-in mechanism analysis (second bomb)

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.