Better plug-in mechanism than onethink

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn the self-developed thinkphp plug-in mechanism, which is easier to transplant, upgrade, and secondary development than onethink.
Core
// + ----------------------------------------------------------------------
// | Thinkphp [we can do it just think it]
// + ----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.inuoer.com All rights reserved.
// + ----------------------------------------------------------------------
// | Author: better
// + ----------------------------------------------------------------------

Namespace Common \ Controller;
Use Think \ Controller;

/**
* Plug-ins
* @ Author better
*/
Abstract class Addon extends Controller
{
/**
* View Instance Object
* @ Var view
* @ Access protected
*/
Protected $ view = null;

Public $ addon_path = '';
Public $ config_file = '';
Public $ view_path = '';

Public function _ construct ()
{
$ This-> view = \ Think: instance ('Think \ view ');
$ This-> addon_path = ADDON_PATH. '/'. $ this-> getName ().'/';
// Reset view Configuration
C ('default _ THEME ','');
C ('view _ path ','');
If (is_file ($ this-> addon_path. 'conf/config. php ')){
$ This-> config_file = $ this-> addon_path. 'conf/config. php ';
$ Config = require $ this-> config_file;
C ($ config );
}

$ This-> view_path =. '/'. ADDON_PATH. '/'. $ this-> getName ().'/';
C ("TMPL_PARSE_STRING", array (
'_ IMG _' => $ this-> view_path. 'view'. C ("DEFAULT_THEME"). '/Public/image ',
'_ CSS _' => $ this-> view_path. 'view'. C ("DEFAULT_THEME"). '/Public/css ',
'_ JS _' => $ this-> view_path. 'view'. C ("DEFAULT_THEME"). '/Public/js ',
'_ ADDON_PUBLIC _' => $ this-> view_path. 'view'. C ("DEFAULT_THEME"). '/public ',
));
}

/**
* Template topic settings
* @ Access protected
* @ Param string $ theme template topic
* @ Return Action
*/
Final protected function theme ($ theme)
{
$ This-> view-> theme ($ theme );
Return $ this;
}

// Display Method
Final protected function display ($ template = '')
{
If ($ template = '')
$ Template = CONTROLLER_NAME;
$ Action = ACTION_NAME;

Echo ($ this-> fetch ($ template, $ action ));
}

/**
* Template variable assignment
* @ Access protected
* @ Param mixed $ name: Template variable to be displayed
* @ Param mixed $ value variable value
* @ Return Action
*/
Final protected function assign ($ name, $ value = '')
{
$ This-> view-> assign ($ name, $ value );
Return $ this;
}


// Method for displaying a template
Final protected function fetch ($ templateFile = CONTROLLER_NAME, $ action = ACTION_NAME)
{
If (! Is_file ($ templateFile )){
If (C ('view _ path ')){
$ TemplateFile = C ('view _ path '). C ('default _ THEME '). '/'. $ templateFile. '/'. $ action. C ('tmpl _ TEMPLATE_SUFFIX ');
} Else {
$ TemplateFile = $ this-> addon_path. 'view /'. C ('default _ THEME '). '/'. $ templateFile. '/'. $ action. C ('tmpl _ TEMPLATE_SUFFIX ');
}

If (! Is_file ($ templateFile )){
Throw new \ Exception ("the template does not exist: $ templateFile ");
}
}
Return $ this-> view-> fetch ($ templateFile );
}

Final public function getName ()
{
$ Class = get_class ($ this );

$ Str = explode ('\', $ class );
Return $ str [1];
}


// Installation is required
Abstract public function install ();

// The plug-in method must be uninstalled.
Abstract public function uninstall ();

}

Core Methods

/**
* Execute the SQL File
*/
Function execute_ SQL _file ($ SQL _path)
{
// Read the SQL File
$ SQL = wp_file_get_contents ($ SQL _path );
$ SQL = str_replace ("\ r", "\ n", $ SQL );
$ SQL = explode ("; \ n", $ SQL );

// Replace the table prefix
$ Orginal = 'wp _';
$ Prefix = C ('db _ prefix ');
$ SQL = str_replace ("{$ orginal}", "{$ prefix}", $ SQL );

// Start Installation
Foreach ($ SQL as $ value ){
$ Value = trim ($ value );
If (empty ($ value ))
Continue;

$ Res = M ()-> execute ($ value );
// Dump ($ res );
// Dump (M ()-> getLastSql ());
}
}

// Anti-Timeout file_get_contents Transformation Function
Function wp_file_get_contents ($ url)
{
$ Context = stream_context_create (array (
'Http' => array (
'Timeout' => 30
)
); // Timeout time, in seconds

Return file_get_contents ($ url, 0, $ context );
}

/**
* Generate the url of the Access Plug-in the content displayed by the plug-in.
* @ Param string $ url
* @ Param array $ param Parameter
* @ Author better
* @ Useage u_addons ('apply: // App/Index/addorder', array ('id' => '1 '))
*/
Function u_addons ($ url, $ param = array ()){
$ Url = explode (': //', $ url );
$ Addon = $ url [0];
$ Url = $ url [1];

$ Url = U ($ url, $ param, false );
Return $ url. '/addon/'. $ addon;
}
: Http://git.oschina.net/einsqing/thinphp-addons

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.