Home  >  Article  >  php教程  >  分享一个插件机制

分享一个插件机制

WBOY
WBOYOriginal
2016-06-07 11:44:231037browse

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

总结下:也就是mod会根据name找到./Mod/kindeditor/default.php 中的kindeditor类。
下面就是ModAction类了,你可以把它放到公用的类中.<?php <br /> class ModAction extends BaseAction<br> { <br>     /*<br>      * 外部直接条用方式为<br>      * ?s=mod-name<br>      * */ <br>     public $default=array(<br>           'mod_name'=>'',<br>           'mod_author' =>'',<br>           'mod_email'  =>'',<br>           'mod_site'   =>'',<br>           'mod_description'=>'',<br>     );<br>     public $param=array();    <br>     function __construct($tags=null){<br>        $this->default['mod_alias']=get_class($this);        <br>        //参数优先顺序     调用(tags)  = >  数据库配置=> 插件文件配置 => 默认配置<br>        if($this->param)<br>           $this->param=array_merge($this->default,$this->param);<br>        else <br>           $this->param=$this->default;    <br>        //下面是数据配置,方便在后台启用或禁用,读取数据配置,缓存插件<br>        if(!$plus_arr=S('plus_arr')){<br>            $plus_arr=M('plus')->select();<br>            S('plus_arr',$plus_arr);           <br>        }<br>        $ret=list_search($plus_arr, array('mod_alias'=>$this->default['mod_alias']));  <br>        if($ret[0]){    <br>                $this->param['status']=$ret[0]['status'];<br>                $this->param=array_merge($this->param,json_decode( $ret[0]['param'],true));    <br>        }  <br>        //tags<br>        if($tags)  $this->param=array_merge($this->param,$tags);<br>        //调用初始化<br>        BaseAction::_initialize();       <br>       <br>     }<br>     //插件渲染模板用,默认路径就是插件的路径<br>     function getHtml($tpl='index.html')<br>     {<br>         //mod 的名称<br>         $name=get_class($this);<br>         //C('MOD_DIR') 就是  /Mod<br>         return $this->fetch('.'.C('MOD_DIR').$name.'/'.$tpl);        <br>     }    <br>    //默认run()<br>     public function run()<br>     {<br>         return $this->getHtml();<br>     }    <br>      <br> }ModAction呢只有两个方法,
getHtml(),用于渲染模板,
run(),
构造函数的功能就是配置参数了,
参数优先顺序 调用(tags) = > 数据库配置=> 插件文件配置 => 默认配置
数据库配置呢就是就建立个插件表,用于在后台管理的,其中有个字段param是吧参数序列化保存的(我这里是json格式)。我的表:分享一个插件机制,后台配置自己写个:
分享一个插件机制
BaseAction是我的所有action的基类,如果没有直接继承Action就行。
要实现单独调用mod呢,还需要在你的Action中或BaseAction中添加//mod 单独调用<br>     function mod()<br>     {          <br>         $mod=_get('name');  <br>         $fun=_get('fun');<br>         $path='.'.C('MOD_DIR').$mod.'/default.php' ;<br>         include_once $path; <br>         $myMod=new $mod;<br>         if(empty($fun)) $fun='run';<br>         $ret=$myMod->$fun();<br>         if(!empty($ret))<br>             echo $ret;        <br>     }所以单独调用这个mod为 ?s=mod-kindeditor
这个插件呢其实呢是继承了action,可以像所有action使用,又可以放到任何模板中任意位置。随心所欲了。
===================
发个实例吧,实例使用tp版本3.13,和上面的代码有点区别,上面的代码是在3.12中使用的,代码以例子为准。

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

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn