您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

分享一个插件机制

2024/3/18 14:51:09发布20次查看
代替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 'mod:'.$name.',not found!';;    
        }     
        include_once $path;
        //new 构造函数
        $mod=new $name($tag);
               //这个status是保存在数据中的,决定是否被启用
        if($mod->param['status']==0)  return 'mod:'.$name.',is unavailable';
        return $mod->run();
        //载入插件
    }根据$path,他会自动加载./mod/插件名称/default.php的内容,并执行run()方法。目录结构:
在此,default.php会继承modaction,代码:'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']=        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找到./mod/kindeditor/default.php 中的kindeditor类。
下面就是modaction类了,你可以把它放到公用的类中.'',
          '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 下载:46 次 )
ad:真正免费,域名+虚机+企业邮箱=0元
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product