PluginModel.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 老猫 <thinkcmf@126.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model;
  12. use think\Model;
  13. use think\Db;
  14. class PluginModel extends Model
  15. {
  16. /**
  17. * 获取插件列表
  18. */
  19. public function getList()
  20. {
  21. $dirs = array_map('basename', glob(CMF_ROOT . 'plugins/*', GLOB_ONLYDIR));
  22. if ($dirs === false) {
  23. $this->error = '插件目录不可读';
  24. return false;
  25. }
  26. $plugins = [];
  27. if (empty($dirs)) return $plugins;
  28. $list = $this->select();
  29. foreach ($list as $plugin) {
  30. $plugins[$plugin['name']] = $plugin;
  31. }
  32. foreach ($dirs as $pluginDir) {
  33. $pluginDir = cmf_parse_name($pluginDir, 1);
  34. if (!isset($plugins[$pluginDir])) {
  35. $class = cmf_get_plugin_class($pluginDir);
  36. if (!class_exists($class)) { // 实例化插件失败忽略
  37. //TODO 加入到日志中
  38. continue;
  39. }
  40. $obj = new $class;
  41. $plugins[$pluginDir] = $obj->info;
  42. if (!isset($obj->info['type']) || $obj->info['type'] == 1) {//只获取普通插件
  43. if ($plugins[$pluginDir]) {
  44. $plugins[$pluginDir]['status'] = 3;//未安装
  45. }
  46. } else {
  47. unset($plugins[$pluginDir]);
  48. }
  49. }
  50. }
  51. return $plugins;
  52. }
  53. /**
  54. * @TODO
  55. * 获取所有钩子,包括系统,应用,模板
  56. * @param bool $refresh 是否刷新缓存
  57. * @return array
  58. */
  59. public function getHooks($refresh = false)
  60. {
  61. if (!$refresh) {
  62. // TODO 加入缓存
  63. }
  64. $returnHooks = [];
  65. $systemHooks = [
  66. //系统钩子
  67. "app_init", "app_begin", "module_init", "action_begin", "view_filter",
  68. "app_end", "log_write", "log_write_done", "response_end",
  69. "admin_init",
  70. "home_init",
  71. "send_mobile_verification_code",
  72. //系统钩子结束
  73. //前台登录钩子
  74. "user_login_start",
  75. //模板钩子
  76. "body_start", "before_head_end", "before_footer", "footer_start", "before_footer_end", "before_body_end",
  77. "left_sidebar_start",
  78. "before_left_sidebar_end",
  79. "right_sidebar_start",
  80. "before_right_sidebar_end",
  81. "comment",
  82. "guestbook",
  83. ];
  84. $dbHooks = Db::name('hook')->column('hook');
  85. $returnHooks = array_unique(array_merge($systemHooks, $dbHooks));
  86. return $returnHooks;
  87. }
  88. public function uninstall($id)
  89. {
  90. $findPlugin = $this->find($id);
  91. if (empty($findPlugin)) {
  92. return -1; //插件不存在;
  93. }
  94. $class = cmf_get_plugin_class($findPlugin['name']);
  95. Db::startTrans();
  96. try {
  97. $this->where('name', $findPlugin['name'])->delete();
  98. Db::name('hook_plugin')->where('plugin', $findPlugin['name'])->delete();
  99. if (class_exists($class)) {
  100. $plugin = new $class;
  101. $uninstallSuccess = $plugin->uninstall();
  102. if (!$uninstallSuccess) {
  103. Db::rollback();
  104. return -2;
  105. }
  106. }
  107. // 删除后台菜单
  108. Db::name('admin_menu')->where([
  109. 'app' => "plugin/{$findPlugin['name']}",
  110. ])->delete();
  111. // 删除权限规则
  112. Db::name('auth_rule')->where('app', "plugin/{$findPlugin['name']}")->delete();
  113. Db::commit();
  114. } catch (\Exception $e) {
  115. Db::rollback();
  116. return false;
  117. }
  118. return true;
  119. }
  120. }