dd

分享一个插件机制

jerry thinkphp 2015年11月18日 收藏
代替widget
代码比较乱没有打包,需要修改框架。
我们知道官网的widget是继承的widget基础类,总感觉不够爽。
这里使用的插件的思想就是为系统添加一个CX标签,让插件继承的是action类。。。
首先修改框架
/lib/Driver/TagLib/Taglibx.class.php
标签定义处 Mod标签:
'mod'       =>  array('attr'=>'name','close'=>0,level=>'1'),  //插件
下面是标签代码
//插件
    public function _mod($attr,$content)
    {     
        $tag      = $this->parseXmlAttr($attr,'mod');
        $name     =!empty($tag['name'])?$tag['name']:''; 
               //在此呢mod只有一个必须的tag,那就是name:调用插件的名称 
        $name     =strtolower($name);
        $path='./Mod/'.$name.'/default.php'; //载入每个插件的文件
        if(!file_exists($path))
        {            
            return '<b style=color:red >Mod:'.$name.',not found!</b>';;    
        }     
        include_once $path;
        //new 构造函数
        $mod=new $name($tag);
               //这个status是保存在数据中的,决定是否被启用
        if($mod->param['status']==0)  return '<font color=red >Mod:'.$name.',is unavailable</font>';
        return $mod->run();
        //载入插件
    }
根据$path,他会自动加载./Mod/插件名称/default.php的内容,并执行run()方法。目录结构:
在此,default.php会继承ModAction,代码:
<?php
class kindeditor extends ModAction
{    
    //参数
    public $param=array(
           'mod_name'=>'html在线编辑器',
           'mod_description'=>'KindEditor 是一套开源的在线HTML编辑器',
           'mod_author'=>'无语西风', 
           'id'=>'editor_id',
           'width'=>'99%',
           'height'=>'300px',
           'class'=>'',
           'items'=>'',
           'mode'=>1,  //模式1 全部模式,2精简模式
    );
   //这里写入你插件的具体代码
    public function run()
    {          
        //设置默认的
        $this->param=array_merge($this->default,$this->param);
        //默认
        if($this->param['mode']==1) $this->param['items']='';
        //精简模式
        if($this->param['mode']==2) $this->param['items']=<<<EOT
        items:[
                'source','|','code','fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
                'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
                'insertunorderedlist', '|', 'emoticons','link'
            ],
EOT;
                
        $this->p=$this->param;      
        return $this->getHtml();  //run方法只要返回被渲染过的模板内容就行
    }
    

 
}
在模板中调用,
<Mod name='kindeditor' mode='1' width='99%' height='50px' ...任何你想加入的参数也就是标签的tag.... />
总结下:也就是mod会根据name找到./Mod/kindeditor/default.php 中的kindeditor类。
下面就是ModAction类了,你可以把它放到公用的类中.
<?php 
class ModAction extends BaseAction

    /*
     * 外部直接条用方式为
     * ?s=mod-name
     * */ 
    public $default=array(
          'mod_name'=>'',
          'mod_author' =>'',
          'mod_email'  =>'',
          'mod_site'   =>'',
          'mod_description'=>'',
    );
    public $param=array();    
    function __construct($tags=null){
       $this->default['mod_alias']=get_class($this);        
       //参数优先顺序     调用(tags)  = >  数据库配置=> 插件文件配置 => 默认配置
       if($this->param)
          $this->param=array_merge($this->default,$this->param);
       else 
          $this->param=$this->default;    
       //下面是数据配置,方便在后台启用或禁用,读取数据配置,缓存插件
       if(!$plus_arr=S('plus_arr')){
           $plus_arr=M('plus')->select();
           S('plus_arr',$plus_arr);           
       }
       $ret=list_search($plus_arr, array('mod_alias'=>$this->default['mod_alias']));  
       if($ret[0]){    
               $this->param['status']=$ret[0]['status'];
               $this->param=array_merge($this->param,json_decode( $ret[0]['param'],true));    
       }  
       //tags
       if($tags)  $this->param=array_merge($this->param,$tags);
       //调用初始化
       BaseAction::_initialize();       
      
    }
    //插件渲染模板用,默认路径就是插件的路径
    function getHtml($tpl='index.html')
    {
        //mod 的名称
        $name=get_class($this);
        //C('MOD_DIR') 就是  /Mod
        return $this->fetch('.'.C('MOD_DIR').$name.'/'.$tpl);        
    }    
   //默认run()
    public function run()
    {
        return $this->getHtml();
    }    
     
}
ModAction呢只有两个方法,
getHtml(),用于渲染模板,
run(),
构造函数的功能就是配置参数了,
参数优先顺序 调用(tags) = > 数据库配置=> 插件文件配置 => 默认配置
数据库配置呢就是就建立个插件表,用于在后台管理的,其中有个字段param是吧参数序列化保存的(我这里是json格式)。我的表:,后台配置自己写个:

BaseAction是我的所有action的基类,如果没有直接继承Action就行。
要实现单独调用mod呢,还需要在你的Action中或BaseAction中添加
//mod 单独调用
    function mod()
    {          
        $mod=_get('name');  
        $fun=_get('fun');
        $path='.'.C('MOD_DIR').$mod.'/default.php' ;
        include_once $path; 
        $myMod=new $mod;
        if(empty($fun)) $fun='run';
        $ret=$myMod->$fun();
        if(!empty($ret))
            echo $ret;        
    }
所以单独调用这个mod为 ?s=mod-kindeditor
这个插件呢其实呢是继承了action,可以像所有action使用,又可以放到任何模板中任意位置。随心所欲了。
===================
发个实例吧,实例使用tp版本3.13,和上面的代码有点区别,上面的代码是在3.12中使用的,代码以例子为准。

附件mod_plus.rar ( 8.91 KB 下载:36 次 )

dd