Plugin.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. // +---------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +---------------------------------------------------------------------
  5. // | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
  6. // +---------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +---------------------------------------------------------------------
  9. // | Author: Dean <zxxjjforever@163.com>
  10. // +---------------------------------------------------------------------
  11. namespace cmf\lib;
  12. use think\exception\TemplateNotFoundException;
  13. use think\Lang;
  14. use think\Loader;
  15. use think\Request;
  16. use think\View;
  17. use think\Config;
  18. use think\Db;
  19. /**
  20. * 插件类
  21. */
  22. abstract class Plugin
  23. {
  24. /**
  25. * 视图实例对象
  26. * @var view
  27. * @access protected
  28. */
  29. private $view = null;
  30. /**
  31. * $info = array(
  32. * 'name'=>'Helloworld',
  33. * 'title'=>'Helloworld',
  34. * 'description'=>'Helloworld',
  35. * 'status'=>1,
  36. * 'author'=>'thinkcmf',
  37. * 'version'=>'1.0'
  38. * )
  39. */
  40. public $info = [];
  41. private $pluginPath = '';
  42. private $name = '';
  43. private $configFilePath = '';
  44. private $themeRoot = "";
  45. public function __construct()
  46. {
  47. $request = Request::instance();
  48. $engineConfig = Config::get('template');
  49. $this->name = $this->getName();
  50. $nameCStyle = Loader::parseName($this->name);
  51. $this->pluginPath = PLUGINS_PATH . $nameCStyle . '/';
  52. $this->configFilePath = $this->pluginPath . 'config.php';
  53. $config = $this->getConfig();
  54. $theme = isset($config['theme']) ? $config['theme'] : '';
  55. $depr = "/";
  56. $root = cmf_get_root();
  57. $themeDir = empty($theme) ? "" : '/' . $theme;
  58. $themePath = 'view' . $themeDir;
  59. $this->themeRoot = $this->pluginPath . $themePath . '/';
  60. $engineConfig['view_base'] = $this->themeRoot;
  61. $pluginRoot = $root . "plugins/{$nameCStyle}";
  62. $cmfAdminThemePath = config('cmf_admin_theme_path');
  63. $cmfAdminDefaultTheme = config('cmf_admin_default_theme');
  64. $adminThemePath = "{$cmfAdminThemePath}{$cmfAdminDefaultTheme}";
  65. //使cdn设置生效
  66. $cdnSettings = cmf_get_option('cdn_settings');
  67. if (empty($cdnSettings['cdn_static_root'])) {
  68. $replaceConfig = [
  69. '__PLUGIN_TMPL__' => $pluginRoot . '/' . $themePath,
  70. '__PLUGIN_ROOT__' => $pluginRoot,
  71. '__ADMIN_TMPL__' => "{$root}/{$adminThemePath}",
  72. '__STATIC__' => "{$root}/static",
  73. '__WEB_ROOT__' => $root
  74. ];
  75. } else {
  76. $cdnStaticRoot = rtrim($cdnSettings['cdn_static_root'], '/');
  77. $replaceConfig = [
  78. '__PLUGIN_TMPL__' => $cdnStaticRoot . '/' . $pluginRoot . '/' . $themePath,
  79. '__PLUGIN_ROOT__' => $cdnStaticRoot . '/' . $pluginRoot,
  80. '__ADMIN_TMPL__' => "{$cdnStaticRoot}/{$adminThemePath}",
  81. '__STATIC__' => "{$cdnStaticRoot}/static",
  82. '__WEB_ROOT__' => $cdnStaticRoot
  83. ];
  84. }
  85. $this->view = new View($engineConfig, $replaceConfig);
  86. //加载多语言
  87. $langSet = $request->langset();
  88. $lang_file = $this->pluginPath . "lang/" . $langSet . ".php";
  89. Lang::load($lang_file);
  90. }
  91. /**
  92. * 加载模板输出
  93. * @access protected
  94. * @param string $template 模板文件名
  95. * @return mixed
  96. */
  97. final protected function fetch($template)
  98. {
  99. if (!is_file($template)) {
  100. $engineConfig = Config::get('template');
  101. $template = $this->themeRoot . $template . '.' . $engineConfig['view_suffix'];
  102. }
  103. // 模板不存在 抛出异常
  104. if (!is_file($template)) {
  105. throw new TemplateNotFoundException('template not exists:' . $template, $template);
  106. }
  107. return $this->view->fetch($template);
  108. }
  109. /**
  110. * 渲染内容输出
  111. * @access protected
  112. * @param string $content 模板内容
  113. * @return mixed
  114. */
  115. final protected function display($content = '')
  116. {
  117. return $this->view->display($content);
  118. }
  119. /**
  120. * 模板变量赋值
  121. * @access protected
  122. * @param mixed $name 要显示的模板变量
  123. * @param mixed $value 变量的值
  124. * @return void
  125. */
  126. final protected function assign($name, $value = '')
  127. {
  128. $this->view->assign($name, $value);
  129. }
  130. /**
  131. * 获取插件名
  132. * @return string
  133. */
  134. final public function getName()
  135. {
  136. if (empty($this->name)) {
  137. $class = get_class($this);
  138. $this->name = substr($class, strrpos($class, '\\') + 1, -6);
  139. }
  140. return $this->name;
  141. }
  142. /**
  143. * 检查插件信息完整性
  144. * @return bool
  145. */
  146. final public function checkInfo()
  147. {
  148. $infoCheckKeys = ['name', 'title', 'description', 'status', 'author', 'version'];
  149. foreach ($infoCheckKeys as $value) {
  150. if (!array_key_exists($value, $this->info))
  151. return false;
  152. }
  153. return true;
  154. }
  155. /**
  156. * 获取插件根目录绝对路径
  157. * @return string
  158. */
  159. final public function getPluginPath()
  160. {
  161. return $this->pluginPath;
  162. }
  163. /**
  164. * 获取插件配置文件绝对路径
  165. * @return string
  166. */
  167. final public function getConfigFilePath()
  168. {
  169. return $this->configFilePath;
  170. }
  171. /**
  172. *
  173. * @return string
  174. */
  175. final public function getThemeRoot()
  176. {
  177. return $this->themeRoot;
  178. }
  179. /**
  180. * @return View
  181. */
  182. public function getView()
  183. {
  184. return $this->view;
  185. }
  186. /**
  187. * 获取插件的配置数组
  188. * @return array
  189. */
  190. final public function getConfig()
  191. {
  192. static $_config = [];
  193. $name = $this->getName();
  194. if (isset($_config[$name])) {
  195. return $_config[$name];
  196. }
  197. $config = Db::name('plugin')->where('name', $name)->value('config');
  198. if (!empty($config) && $config != "null") {
  199. $config = json_decode($config, true);
  200. } else {
  201. $config = $this->getDefaultConfig();
  202. }
  203. $_config[$name] = $config;
  204. return $config;
  205. }
  206. /**
  207. * 获取插件的配置数组
  208. * @return array
  209. */
  210. final public function getDefaultConfig()
  211. {
  212. $config = [];
  213. if (file_exists($this->configFilePath)) {
  214. $tempArr = include $this->configFilePath;
  215. if (!empty($tempArr)) {
  216. foreach ($tempArr as $key => $value) {
  217. if ($value['type'] == 'group') {
  218. foreach ($value['options'] as $gkey => $gvalue) {
  219. foreach ($gvalue['options'] as $ikey => $ivalue) {
  220. $config[$ikey] = $ivalue['value'];
  221. }
  222. }
  223. } else {
  224. $config[$key] = $tempArr[$key]['value'];
  225. }
  226. }
  227. }
  228. }
  229. return $config;
  230. }
  231. //必须实现安装
  232. abstract public function install();
  233. //必须卸载插件方法
  234. abstract public function uninstall();
  235. }