自主开发的thinkphp插件机制,比onethink方便移植,升级和二次开发。核心类<br>
<?php <br />
// +----------------------------------------------------------------------<br>
// | Thinkphp [ WE CAN DO IT JUST THINK IT ]<br>
// +----------------------------------------------------------------------<br>
// | Copyright (c) 2015 http://www.inuoer.com All rights reserved.<br>
// +----------------------------------------------------------------------<br>
// | Author: better <einsqing><br>
// +----------------------------------------------------------------------<br>
<br>
namespace Common\Controller;<br>
use Think\Controller;<br>
<br>
/**<br>
* 插件类<br>
* @author better <einsqing><br>
*/<br>
abstract class Addon extends Controller<br>
{<br>
/**<br>
* 视图实例对象<br>
* @var view<br>
* @access protected<br>
*/<br>
protected $view = null;<br>
<br>
public $addon_path = '';<br>
public $config_file = '';<br>
public $view_path = '';<br>
<br>
public function __construct()<br>
{<br>
$this->view = \Think\Think::instance('Think\View');<br>
$this->addon_path = ADDON_PATH . '/' . $this->getName() . '/';<br>
//重置视图配置<br>
C('DEFAULT_THEME', '');<br>
C('VIEW_PATH', '');<br>
if (is_file($this->addon_path . 'Conf/config.php')) {<br>
$this->config_file = $this->addon_path . 'Conf/config.php';<br>
$config = require $this->config_file;<br>
C($config);<br>
}<br>
<br>
$this->view_path = __ROOT__ . '/' . ADDON_PATH . '/' . $this->getName() . '/';<br>
C("TMPL_PARSE_STRING", array(<br>
'__IMG__' => $this->view_path . 'View' . C("DEFAULT_THEME") . '/Public/image',<br>
'__CSS__' => $this->view_path . 'View' . C("DEFAULT_THEME") . '/Public/css',<br>
'__JS__' => $this->view_path . 'View' . C("DEFAULT_THEME") . '/Public/js',<br>
'__ADDON_PUBLIC__' => $this->view_path . 'View' . C("DEFAULT_THEME") . '/Public',<br>
));<br>
}<br>
<br>
/**<br>
* 模板主题设置<br>
* @access protected<br>
* @param string $theme 模版主题<br>
* @return Action<br>
*/<br>
final protected function theme($theme)<br>
{<br>
$this->view->theme($theme);<br>
return $this;<br>
}<br>
<br>
//显示方法<br>
final protected function display($template = '')<br>
{<br>
if ($template == '')<br>
$template = CONTROLLER_NAME;<br>
$action = ACTION_NAME;<br>
<br>
echo($this->fetch($template, $action));<br>
}<br>
<br>
/**<br>
* 模板变量赋值<br>
* @access protected<br>
* @param mixed $name 要显示的模板变量<br>
* @param mixed $value 变量的值<br>
* @return Action<br>
*/<br>
final protected function assign($name, $value = '')<br>
{<br>
$this->view->assign($name, $value);<br>
return $this;<br>
}<br>
<br>
<br>
//用于显示模板的方法<br>
final protected function fetch($templateFile = CONTROLLER_NAME, $action = ACTION_NAME)<br>
{<br>
if (!is_file($templateFile)) {<br>
if (C('VIEW_PATH')) {<br>
$templateFile = C('VIEW_PATH') . C('DEFAULT_THEME') . '/' . $templateFile . '/' . $action . C('TMPL_TEMPLATE_SUFFIX');<br>
} else {<br>
$templateFile = $this->addon_path . 'View/' . C('DEFAULT_THEME') . '/' . $templateFile . '/' . $action . C('TMPL_TEMPLATE_SUFFIX');<br>
}<br>
<br>
if (!is_file($templateFile)) {<br>
throw new \Exception("模板不存在:$templateFile");<br>
}<br>
}<br>
return $this->view->fetch($templateFile);<br>
}<br>
<br>
final public function getName()<br>
{<br>
$class = get_class($this);<br>
<br>
$str = explode('\\', $class);<br>
return $str[1];<br>
}<br>
<br>
<br>
//必须实现安装<br>
abstract public function install();<br>
<br>
//必须卸载插件方法<br>
abstract public function uninstall();<br>
<br>
}<br>
<br>
核心方法<br>
<br>
/**<br>
* 执行SQL文件<br>
*/<br>
function execute_sql_file($sql_path)<br>
{<br>
// 读取SQL文件<br>
$sql = wp_file_get_contents($sql_path);<br>
$sql = str_replace("\r", "\n", $sql);<br>
$sql = explode(";\n", $sql);<br>
<br>
// 替换表前缀<br>
$orginal = 'wp_';<br>
$prefix = C('DB_PREFIX');<br>
$sql = str_replace("{$orginal}", "{$prefix}", $sql);<br>
<br>
// 开始安装<br>
foreach ($sql as $value) {<br>
$value = trim($value);<br>
if (empty ($value))<br>
continue;<br>
<br>
$res = M()->execute($value);<br>
// dump($res);<br>
// dump(M()->getLastSql());<br>
}<br>
}<br>
<br>
// 防超时的file_get_contents改造函数<br>
function wp_file_get_contents($url)<br>
{<br>
$context = stream_context_create(array(<br>
'http' => array(<br>
'timeout' => 30<br>
)<br>
)); // 超时时间,单位为秒<br>
<br>
return file_get_contents($url, 0, $context);<br>
}<br>
<br>
/**<br>
* 插件显示内容里生成访问插件的url<br>
* @param string $url url<br>
* @param array $param 参数<br>
* @author better<br>
* @useage u_addons('apply://App/Index/addorder',array('id'=>'1'))<br>
*/<br>
function u_addons($url, $param = array()){<br>
$url = explode('://', $url);<br>
$addon = $url[0];<br>
$url = $url[1];<br>
<br>
$url = U($url, $param, false);<br>
return $url . '/addon/' . $addon;<br>
}</einsqing></einsqing>
下载地址:http://git.oschina.net/einsqing/thinphp-addons
AD:真正免费,域名+虚机+企业邮箱=0元