onethink보다 더 좋은 플러그인 메커니즘

   
<?php
// +----------------------------------------------------------------------
// | Thinkphp [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.inuoer.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: better <[email protected]>
// +----------------------------------------------------------------------

namespace Common\Controller;
use Think\Controller;

/**
 *    
 * @author better <[email protected]>
 */
abstract class Addon extends Controller
{
    /**
     *       
     * @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',
        ));
    }

    /**
     *       
     * @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];
    }


    //      
    abstract public function install();

    //        
    abstract public function uninstall();

}

    

/**
 *   SQL  
 */
function execute_sql_file($sql_path)
{
    //   SQL  
    $sql = wp_file_get_contents($sql_path);
    $sql = str_replace("\r", "
", $sql);     $sql = explode(";
", $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());     } } //  file_get_contents function wp_file_get_contents($url) {     $context = stream_context_create(array(         'http' => array(             'timeout' => 30         )     )); //  ,     return file_get_contents($url, 0, $context); } /**  *  url  * @param string $url url  * @param array $param   * @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; }

좋은 웹페이지 즐겨찾기