Addon.class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace Common\Controller;
  3. /**
  4. * 插件类
  5. * 该类参考了OneThink的部分实现
  6. *
  7. */
  8. abstract class Addon
  9. {
  10. /**
  11. * 视图实例对象
  12. * @var view
  13. * @access protected
  14. *
  15. */
  16. protected $view = null;
  17. public $info = array();
  18. public $addon_path = '';
  19. public $config_file = '';
  20. public $custom_config = '';
  21. public $admin_list = array();
  22. public $custom_adminlist = '';
  23. public $access_url = array();
  24. /**
  25. * 构造方法
  26. *
  27. */
  28. public function __construct()
  29. {
  30. $this->view = \Think\Think::instance('Think\View');
  31. $this->addon_path = C('ADDON_PATH') . $this->getName() . '/';
  32. $TMPL_PARSE_STRING = C('TMPL_PARSE_STRING');
  33. $TMPL_PARSE_STRING['__ADDONROOT__'] = __ROOT__
  34. . '/Addons/'
  35. . $this->getName();
  36. C('TMPL_PARSE_STRING', $TMPL_PARSE_STRING);
  37. if (is_file($this->addon_path . 'config.php')) {
  38. $this->config_file = $this->addon_path . 'config.php';
  39. }
  40. }
  41. /**
  42. * 模板主题设置
  43. * @access protected
  44. * @param string $theme 模版主题
  45. * @return Action
  46. *
  47. */
  48. final protected function theme($theme)
  49. {
  50. $this->view->theme($theme);
  51. return $this;
  52. }
  53. /**
  54. * 显示方法
  55. *
  56. */
  57. final protected function display($file = '')
  58. {
  59. if ($file == '') {
  60. $file = CONTROLLER_NAME;
  61. }
  62. if (MODULE_MARK === 'Home') {
  63. if (C('CURRENT_THEME')) {
  64. $template = './Theme/' . C('CURRENT_THEME') . '/Home/Addons/' . $this->getName() . '/' . $file . '.html';
  65. if (is_file($template)) {
  66. $file = $template;
  67. }
  68. if (IS_WAP) {
  69. $wap_template = './Theme/' . C('CURRENT_THEME') . '/Home/Wap/Addons/' . $this->getName() . '/' . $file . '.html';
  70. if (is_file($wap_template)) {
  71. $file = $wap_template;
  72. }
  73. }
  74. } else {
  75. if (IS_WAP) {
  76. $wap_template = './Addons/' . $this->getName() . '/Wap/' . $file . '.html';
  77. if (is_file($wap_template)) {
  78. $file = $wap_template;
  79. }
  80. }
  81. }
  82. }
  83. echo ($this->fetch($file));
  84. }
  85. /**
  86. * 模板变量赋值
  87. * @access protected
  88. * @param mixed $name 要显示的模板变量
  89. * @param mixed $value 变量的值
  90. * @return Action
  91. *
  92. */
  93. final protected function assign($name, $value = '')
  94. {
  95. $this->view->assign($name, $value);
  96. return $this;
  97. }
  98. /**
  99. * 用于显示模板的方法
  100. *
  101. */
  102. final protected function fetch($templateFile = CONTROLLER_NAME)
  103. {
  104. if (!is_file($templateFile)) {
  105. $templateFile = $this->addon_path
  106. . $templateFile
  107. . C('TMPL_TEMPLATE_SUFFIX');
  108. if (!is_file($templateFile)) {
  109. throw new \Exception("模板不存在:$templateFile");
  110. }
  111. }
  112. return $this->view->fetch($templateFile);
  113. }
  114. /**
  115. * 获取名称
  116. *
  117. */
  118. final public function getName()
  119. {
  120. $class = get_class($this);
  121. return substr($class, strrpos($class, '\\') + 1, -5);
  122. }
  123. /**
  124. * 检查信息
  125. *
  126. */
  127. final public function checkInfo()
  128. {
  129. $info_check_keys = array(
  130. 'name', 'title', 'description', 'status', 'author', 'version',
  131. );
  132. foreach ($info_check_keys as $value) {
  133. if (!array_key_exists($value, $this->info)) {
  134. return false;
  135. }
  136. }
  137. return true;
  138. }
  139. /**
  140. * 获取插件的配置数组
  141. *
  142. */
  143. final public function getConfig($name = '')
  144. {
  145. static $_config = array();
  146. if (empty($name)) {
  147. $name = $this->getName();
  148. }
  149. if (isset($_config[$name])) {
  150. return $_config[$name];
  151. }
  152. $config = array();
  153. $map['name'] = $name;
  154. $map['status'] = 1;
  155. $config = D('Admin/Addon')->where($map)->getField('config');
  156. if ($config) {
  157. $config = json_decode($config, true);
  158. } else {
  159. $temp_arr = include $this->config_file;
  160. foreach ($temp_arr as $key => $value) {
  161. if ($value['type'] == 'group') {
  162. foreach ($value['options'] as $gkey => $gvalue) {
  163. foreach ($gvalue['options'] as $ikey => $ivalue) {
  164. $config[$ikey] = $ivalue['value'];
  165. }
  166. }
  167. } else {
  168. $config[$key] = $temp_arr[$key]['value'];
  169. }
  170. }
  171. }
  172. $_config[$name] = $config;
  173. return $config;
  174. }
  175. /**
  176. * 必须实现安装
  177. *
  178. */
  179. abstract public function install();
  180. /**
  181. * 必须卸载插件方法
  182. *
  183. */
  184. abstract public function uninstall();
  185. }