- 論壇徽章:
- 0
|
自主開發(fā)的thinkphp插件機(jī)制,比onethink方便移植,升級(jí)和二次開發(fā)。
代碼- 核心類
- <?php
- // +----------------------------------------------------------------------
- // | Thinkphp [ WE CAN DO IT JUST THINK IT ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2015 http://www.inuoer.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Author: better <einsqing@gmail.com>
- // +----------------------------------------------------------------------
-
- namespace Common\Controller;
- use Think\Controller;
-
- /**
- * 插件類
- * @author better <einsqing@gmail.com>
- */
- abstract class Addon extends Controller
- {
- /**
- * 視圖實(shí)例對象
- * @var view
- * @access protected
- */
- protected $view = null;
-
- public $addon_path = '';
- public $config_file = '';
- public $view_path = '';
-
- public function __construct()
- {
- $this->view = \Think\Think::instance('Think\View');
- $this->addon_path = ADDON_PATH . '/' . $this->getName() . '/';
- //重置視圖配置
- 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 = __ROOT__ . '/' . 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',
- ));
- }
-
- /**
- * 模板主題設(shè)置
- * @access protected
- * @param string $theme 模版主題
- * @return Action
- */
- final protected function theme($theme)
- {
- $this->view->theme($theme);
- return $this;
- }
-
- //顯示方法
- final protected function display($template = '')
- {
- if ($template == '')
- $template = CONTROLLER_NAME;
- $action = ACTION_NAME;
-
- echo($this->fetch($template, $action));
- }
-
- /**
- * 模板變量賦值
- * @access protected
- * @param mixed $name 要顯示的模板變量
- * @param mixed $value 變量的值
- * @return Action
- */
- final protected function assign($name, $value = '')
- {
- $this->view->assign($name, $value);
- return $this;
- }
-
-
- //用于顯示模板的方法
- 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("模板不存在:$templateFile");
- }
- }
- return $this->view->fetch($templateFile);
- }
-
- final public function getName()
- {
- $class = get_class($this);
-
- $str = explode('\\', $class);
- return $str[1];
- }
-
-
- //必須實(shí)現(xiàn)安裝
- abstract public function install();
-
- //必須卸載插件方法
- abstract public function uninstall();
-
- }
-
- 核心方法
-
- /**
- * 執(zhí)行SQL文件
- */
- function execute_sql_file($sql_path)
- {
- // 讀取SQL文件
- $sql = wp_file_get_contents($sql_path);
- $sql = str_replace("\r", "\n", $sql);
- $sql = explode(";\n", $sql);
-
- // 替換表前綴
- $orginal = 'wp_';
- $prefix = C('DB_PREFIX');
- $sql = str_replace("{$orginal}", "{$prefix}", $sql);
-
- // 開始安裝
- foreach ($sql as $value) {
- $value = trim($value);
- if (empty ($value))
- continue;
-
- $res = M()->execute($value);
- // dump($res);
- // dump(M()->getLastSql());
- }
- }
-
- // 防超時(shí)的file_get_contents改造函數(shù)
- function wp_file_get_contents($url)
- {
- $context = stream_context_create(array(
- 'http' => array(
- 'timeout' => 30
- )
- )); // 超時(shí)時(shí)間,單位為秒
-
- return file_get_contents($url, 0, $context);
- }
-
- /**
- * 插件顯示內(nèi)容里生成訪問插件的url
- * @param string $url url
- * @param array $param 參數(shù)
- * @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;
- }
復(fù)制代碼 |
|