Hook.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\model;
  10. use think\Model;
  11. /**
  12. * 钩子模型
  13. * @package app\admin\model
  14. */
  15. class Hook extends Model
  16. {
  17. // 设置当前模型对应的完整数据表名称
  18. protected $name = 'admin_hook';
  19. // 自动写入时间戳
  20. protected $autoWriteTimestamp = true;
  21. /**
  22. * 添加钩子
  23. * @param array $hooks 钩子
  24. * @param string $plugin_name
  25. * @author 蔡伟明 <314013107@qq.com>
  26. * @return bool
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. * @throws \think\exception\DbException
  30. */
  31. public static function addHooks($hooks = [], $plugin_name = '')
  32. {
  33. if (!empty($hooks) && is_array($hooks)) {
  34. $data = [];
  35. foreach ($hooks as $name => $description) {
  36. if (is_numeric($name)) {
  37. $name = $description;
  38. $description = '';
  39. }
  40. if (self::where('name', $name)->find()) {
  41. continue;
  42. }
  43. $data[] = [
  44. 'name' => $name,
  45. 'plugin' => $plugin_name,
  46. 'description' => $description,
  47. 'create_time' => request()->time(),
  48. 'update_time' => request()->time(),
  49. ];
  50. }
  51. if (!empty($data) && false === self::insertAll($data)) {
  52. return false;
  53. }
  54. }
  55. return true;
  56. }
  57. /**
  58. * 删除钩子
  59. * @param string $plugin_name 钩子名称
  60. * @author 蔡伟明 <314013107@qq.com>
  61. * @return bool
  62. * @throws \think\Exception
  63. * @throws \think\exception\PDOException
  64. */
  65. public static function deleteHooks($plugin_name = '')
  66. {
  67. if (!empty($plugin_name)) {
  68. if (false === self::where('plugin', $plugin_name)->delete()) {
  69. return false;
  70. }
  71. }
  72. return true;
  73. }
  74. }