You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.9 KiB

  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\command;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\input\Argument;
  7. use think\console\input\Option;
  8. use think\console\Output;
  9. use think\facade\Db;
  10. use think\facade\Config;
  11. use app\lib\Plugins;
  12. class Clean extends Command
  13. {
  14. protected function configure()
  15. {
  16. $this->setName('clean')
  17. ->setDescription('the clean command');
  18. }
  19. protected function execute(Input $input, Output $output)
  20. {
  21. $res = Db::name('config')->cache('configs',0)->column('value','key');
  22. Config::set($res, 'sys');
  23. if(config_get('bt_url')){
  24. $this->clean_plugins($input, $output, 'Linux');
  25. }
  26. if(config_get('wbt_url')){
  27. $this->clean_plugins($input, $output, 'Windows');
  28. }
  29. config_set('cleantime', date('Y-m-d H:i:s'));
  30. }
  31. private function clean_plugins(Input $input, Output $output, $os){
  32. $data_dir = get_data_dir($os) . 'plugins/';
  33. $file_list = [];
  34. $json_arr = Plugins::get_plugin_list($os);
  35. if(count($json_arr['list']) == 0) return;
  36. foreach($json_arr['list'] as $plugin){
  37. foreach($plugin['versions'] as $version){
  38. $ver = $version['m_version'].'.'.$version['version'];
  39. if(!isset($version['download'])){
  40. $file_list[] = $plugin['name'].'-'.$ver;
  41. }
  42. }
  43. }
  44. $count = 0;
  45. $dir = opendir($data_dir.'package');
  46. while(false !== ( $file = readdir($dir)) ) {
  47. if($file == '.' || $file == '..') continue;
  48. $name = str_replace('.zip', '', $file);
  49. if(!in_array($name, $file_list)){
  50. $filepath = $data_dir . 'package/' . $file;
  51. unlink($filepath);
  52. $count++;
  53. }
  54. }
  55. $output->writeln($os.'成功清理'.$count.'个历史版本插件包');
  56. $count = 0;
  57. $dir = opendir($data_dir.'folder');
  58. while(false !== ( $file = readdir($dir)) ) {
  59. if($file == '.' || $file == '..') continue;
  60. if(!in_array($file, $file_list)){
  61. $filepath = $data_dir . 'folder/' . $file;
  62. $this->delete_dir($filepath);
  63. $count++;
  64. }
  65. }
  66. $output->writeln($os.'成功清理'.$count.'个历史版本插件目录');
  67. }
  68. // 删除文件夹
  69. private function delete_dir($dir){
  70. $rd = opendir($dir);
  71. if (!$rd) {
  72. return false;
  73. }
  74. while (($file = readdir($rd)) !== false) {
  75. if ($file == '.' || $file == '..') {
  76. continue;
  77. }
  78. $file = $dir . '/' . $file;
  79. if (is_dir($file)) {
  80. $this->delete_dir($file);
  81. }
  82. else {
  83. unlink($file);
  84. }
  85. }
  86. closedir($rd);
  87. rmdir($dir);
  88. return true;
  89. }
  90. }