Packet.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. use think\Db;
  12. use util\Sql;
  13. /**
  14. * 数据包模型
  15. * @package app\admin\model
  16. */
  17. class Packet extends Model
  18. {
  19. // 设置当前模型对应的完整数据表名称
  20. protected $name = 'admin_packet';
  21. // 自动写入时间戳
  22. protected $autoWriteTimestamp = true;
  23. /**
  24. * 获取所有数据包列表
  25. * @author 蔡伟明 <314013107@qq.com>
  26. * @return array|bool
  27. */
  28. public function getAll()
  29. {
  30. // 获取数据包目录下的所有插件目录
  31. $dirs = array_map('basename', glob(config('packet_path').'*', GLOB_ONLYDIR));
  32. if ($dirs === false || !file_exists(config('packet_path'))) {
  33. $this->error = '插件目录不可读或者不存在';
  34. return false;
  35. }
  36. // 读取数据库数据包表
  37. $packets = $this->column(true, 'name');
  38. // 读取未安装的数据包
  39. foreach ($dirs as $packet) {
  40. if (!isset($packets[$packet])) {
  41. $info = $this->getInfoFromFile($packet);
  42. $info['status'] = 0;
  43. $packets[] = $info;
  44. }
  45. }
  46. return $packets;
  47. }
  48. /**
  49. * 从文件获取数据包信息
  50. * @param string $name 数据包名称
  51. * @author 蔡伟明 <314013107@qq.com>
  52. * @return array|mixed
  53. */
  54. public static function getInfoFromFile($name = '')
  55. {
  56. $info = [];
  57. if ($name != '') {
  58. // 从配置文件获取
  59. if (is_file(config('packet_path'). $name . '/info.php')) {
  60. $info = include config('packet_path'). $name . '/info.php';
  61. }
  62. }
  63. return $info;
  64. }
  65. /**
  66. * 安装数据包
  67. * @param string $name 数据包名
  68. * @author 蔡伟明 <314013107@qq.com>
  69. * @return bool
  70. */
  71. public static function install($name = '')
  72. {
  73. $info = self::getInfoFromFile($name);
  74. foreach ($info['tables'] as $table) {
  75. $sql_file = realpath(config('packet_path').$name."/{$table}.sql");
  76. if (file_exists($sql_file)) {
  77. if (isset($info['database_prefix']) && $info['database_prefix'] != '') {
  78. $sql_statement = Sql::getSqlFromFile($sql_file, false, [$info['database_prefix'] => config('database.prefix')]);
  79. } else {
  80. $sql_statement = Sql::getSqlFromFile($sql_file);
  81. }
  82. if (!empty($sql_statement)) {
  83. foreach ($sql_statement as $value) {
  84. Db::execute($value);
  85. }
  86. }
  87. } else {
  88. return "【{$table}.sql】文件不存在";
  89. }
  90. }
  91. return true;
  92. }
  93. /**
  94. * 卸载数据包
  95. * @param string $name 数据包名
  96. * @author 蔡伟明 <314013107@qq.com>
  97. * @return bool
  98. * @throws \think\Exception
  99. * @throws \think\exception\PDOException
  100. */
  101. public static function uninstall($name = '')
  102. {
  103. $info = self::getInfoFromFile($name);
  104. foreach ($info['tables'] as $table) {
  105. $sql = "DROP TABLE IF EXISTS `". config('database.prefix') ."{$table}`;";
  106. Db::execute($sql);
  107. self::where('name', $name)->delete();
  108. }
  109. return true;
  110. }
  111. }