Packet.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model;
  12. use think\Model;
  13. use think\Db;
  14. use util\Sql;
  15. /**
  16. * 数据包模型
  17. * @package app\admin\model
  18. */
  19. class Packet extends Model
  20. {
  21. // 设置当前模型对应的完整数据表名称
  22. protected $table = '__ADMIN_PACKET__';
  23. // 自动写入时间戳
  24. protected $autoWriteTimestamp = true;
  25. /**
  26. * 获取所有数据包列表
  27. * @author 蔡伟明 <314013107@qq.com>
  28. * @return array|bool
  29. */
  30. public function getAll()
  31. {
  32. // 获取数据包目录下的所有插件目录
  33. $dirs = array_map('basename', glob(config('packet_path').'*', GLOB_ONLYDIR));
  34. if ($dirs === false || !file_exists(config('packet_path'))) {
  35. $this->error = '插件目录不可读或者不存在';
  36. return false;
  37. }
  38. // 读取数据库数据包表
  39. $packets = $this->column(true, 'name');
  40. // 读取未安装的数据包
  41. foreach ($dirs as $packet) {
  42. if (!isset($packets[$packet])) {
  43. $info = $this->getInfoFromFile($packet);
  44. $info['status'] = 0;
  45. $packets[] = $info;
  46. }
  47. }
  48. return $packets;
  49. }
  50. /**
  51. * 从文件获取数据包信息
  52. * @param string $name 数据包名称
  53. * @author 蔡伟明 <314013107@qq.com>
  54. * @return array|mixed
  55. */
  56. public static function getInfoFromFile($name = '')
  57. {
  58. $info = [];
  59. if ($name != '') {
  60. // 从配置文件获取
  61. if (is_file(config('packet_path'). $name . '/info.php')) {
  62. $info = include config('packet_path'). $name . '/info.php';
  63. }
  64. }
  65. return $info;
  66. }
  67. /**
  68. * 安装数据包
  69. * @param string $name 数据包名
  70. * @author 蔡伟明 <314013107@qq.com>
  71. * @return bool
  72. */
  73. public static function install($name = '')
  74. {
  75. $info = self::getInfoFromFile($name);
  76. foreach ($info['tables'] as $table) {
  77. $sql_file = realpath(config('packet_path').$name."/{$table}.sql");
  78. if (file_exists($sql_file)) {
  79. if (isset($info['database_prefix']) && $info['database_prefix'] != '') {
  80. $sql_statement = Sql::getSqlFromFile($sql_file, false, [$info['database_prefix'] => config('database.prefix')]);
  81. } else {
  82. $sql_statement = Sql::getSqlFromFile($sql_file);
  83. }
  84. if (!empty($sql_statement)) {
  85. foreach ($sql_statement as $value) {
  86. Db::execute($value);
  87. }
  88. }
  89. } else {
  90. return "【{$table}.sql】文件不存在";
  91. }
  92. }
  93. return true;
  94. }
  95. /**
  96. * 卸载数据包
  97. * @param string $name 数据包名
  98. * @author 蔡伟明 <314013107@qq.com>
  99. * @return bool
  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. }