Plugin.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * 洛阳赤炎鹰网络科技有限公司
  4. * https://www.cyyvip.com
  5. * Copyright (c) 2022 赤店商城 All rights reserved.
  6. */
  7. // +---------------------------------------------------------------------
  8. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  9. // +---------------------------------------------------------------------
  10. // | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
  11. // +---------------------------------------------------------------------
  12. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  13. // +---------------------------------------------------------------------
  14. // | Author: Dean <zxxjjforever@163.com>
  15. // +---------------------------------------------------------------------
  16. namespace Common\Lib;
  17. /**
  18. * 插件类
  19. */
  20. abstract class Plugin{
  21. /**
  22. * 视图实例对象
  23. * @var view
  24. * @access protected
  25. */
  26. protected $view = null;
  27. /**
  28. * $info = array(
  29. * 'name'=>'Helloworld',
  30. * 'title'=>'Helloworld',
  31. * 'description'=>'Helloworld',
  32. * 'status'=>1,
  33. * 'author'=>'thinkcmf',
  34. * 'version'=>'1.0'
  35. * )
  36. */
  37. public $info = array();
  38. public $plugin_path = '';
  39. public $config_file = '';
  40. public $custom_config = '';
  41. public $admin_list = array();
  42. public $custom_adminlist = '';
  43. public $access_url = array();
  44. public $tmpl_root ="";
  45. public function __construct(){
  46. $this->view = \Think\Think::instance('Think\View');
  47. $this->plugin_path = './plugins/'.$this->getName().'/';
  48. //多语言
  49. if (C('LANG_SWITCH_ON',null,false)){
  50. $lang_file= $this->plugin_path."Lang/".LANG_SET.".php";
  51. if(is_file($lang_file)){
  52. $lang=include $lang_file;
  53. L($lang);
  54. }
  55. }
  56. $TMPL_PARSE_STRING = C('TMPL_PARSE_STRING');
  57. $plugin_root= __ROOT__ . '/plugins/'.$this->getName();
  58. $TMPL_PARSE_STRING['__PLUGINROOT__'] =$plugin_root;
  59. if(is_file($this->plugin_path.'config.php')){
  60. $this->config_file = $this->plugin_path.'config.php';
  61. }
  62. $config=$this->getConfig();
  63. $theme=$config['theme'];
  64. $depr = "/";
  65. $theme=empty($theme)?"":$theme.$depr;
  66. $v_layer=C("DEFAULT_V_LAYER");
  67. $this->tmpl_root= "plugins/".$this->getName()."/$v_layer/".$theme;
  68. $TMPL_PARSE_STRING['__PLUGINTMPL__'] = __ROOT__."/".$this->tmpl_root;
  69. C('TMPL_PARSE_STRING', $TMPL_PARSE_STRING);
  70. }
  71. /**
  72. * 模板主题设置
  73. * @access protected
  74. * @param string $theme 模版主题
  75. * @return Action
  76. */
  77. final protected function theme($theme){
  78. $this->view->theme($theme);
  79. return $this;
  80. }
  81. //显示方法
  82. final protected function display($template='widget'){
  83. echo ($this->fetch($template));
  84. }
  85. /**
  86. * 模板变量赋值
  87. * @access protected
  88. * @param mixed $name 要显示的模板变量
  89. * @param mixed $value 变量的值
  90. * @return Action
  91. */
  92. final protected function assign($name,$value='') {
  93. $this->view->assign($name,$value);
  94. return $this;
  95. }
  96. //用于显示模板的方法
  97. final protected function fetch($templateFile = 'widget'){
  98. if(!is_file($templateFile)){
  99. $config=$this->getConfig();
  100. $theme=$config['theme'];
  101. $depr = "/";
  102. $theme=empty($theme)?"":$theme.$depr;
  103. $templateFile = sp_add_template_file_suffix("./".$this->tmpl_root.$templateFile);
  104. if(!file_exists_case($templateFile)){
  105. throw new \Exception("模板不存在:$templateFile");
  106. }
  107. }
  108. return $this->view->fetch($templateFile);
  109. }
  110. final public function getName(){
  111. $class = get_class($this);
  112. return substr($class,strrpos($class, '\\')+1, -6);
  113. }
  114. final public function checkInfo(){
  115. $info_check_keys = array('name','title','description','status','author','version');
  116. foreach ($info_check_keys as $value) {
  117. if(!array_key_exists($value, $this->info))
  118. return false;
  119. }
  120. return true;
  121. }
  122. /**
  123. * 获取插件的配置数组
  124. */
  125. public function getConfig($name=''){
  126. static $_config = array();
  127. if(empty($name)){
  128. $name = $this->getName();
  129. }
  130. if(isset($_config[$name])){
  131. return $_config[$name];
  132. }
  133. $config=M('Plugins')->where(array("name"=>$name))->getField("config");
  134. if($config){
  135. $config = json_decode($config, true);
  136. }else{
  137. $temp_arr = include $this->config_file;
  138. foreach ($temp_arr as $key => $value) {
  139. if($value['type'] == 'group'){
  140. foreach ($value['options'] as $gkey => $gvalue) {
  141. foreach ($gvalue['options'] as $ikey => $ivalue) {
  142. $config[$ikey] = $ivalue['value'];
  143. }
  144. }
  145. }else{
  146. $config[$key] = $temp_arr[$key]['value'];
  147. }
  148. }
  149. }
  150. $_config[$name] = $config;
  151. return $config;
  152. }
  153. //必须实现安装
  154. abstract public function install();
  155. //必须卸载插件方法
  156. abstract public function uninstall();
  157. }