Clear.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\admin\controller\AuthController;
  4. use crmeb\services\CacheService;
  5. use crmeb\services\JsonService as Json;
  6. /**
  7. * 首页控制器
  8. * Class Clear
  9. * @package app\admin\controller
  10. *
  11. */
  12. class Clear extends AuthController
  13. {
  14. public function index()
  15. {
  16. return $this->fetch();
  17. }
  18. /**
  19. * 刷新数据缓存
  20. */
  21. public function refresh_cache()
  22. {
  23. $root = app()->getRootPath() . 'runtime' . DS;
  24. $adminRoute = $root . 'admin';
  25. $apiRoute = $root . 'api';
  26. $cacheRoute = $root . 'cache';
  27. $cache = [];
  28. if(is_dir($adminRoute))
  29. $cache[$adminRoute] = scandir($adminRoute);
  30. if(is_dir($apiRoute))
  31. $cache[$apiRoute] = scandir($apiRoute);
  32. if(is_dir($cacheRoute))
  33. $cache[$cacheRoute] = scandir($cacheRoute);
  34. foreach ($cache as $p => $list) {
  35. foreach ($list as $file) {
  36. if (!in_array($file, ['.', '..', 'log', 'schema', 'route.php'])) {
  37. $path = $p . DS . $file;
  38. if (is_file($path)) {
  39. @unlink($path);
  40. } else {
  41. $this->delDirAndFile($path . DS);
  42. }
  43. }
  44. }
  45. }
  46. CacheService::clear();
  47. return app('json')->successful('数据缓存刷新成功!');
  48. }
  49. /**
  50. * 删除日志
  51. */
  52. public function delete_log()
  53. {
  54. $root = app()->getRootPath() . 'runtime' . DS;
  55. $this->delDirAndFile($root . 'admin' . DS . 'log' . DS);
  56. $this->delDirAndFile($root . 'api' . DS . 'log' . DS);
  57. $this->delDirAndFile($root . 'log' . DS);
  58. return app('json')->successful('数据缓存刷新成功!');
  59. }
  60. /** 递归删除文件
  61. * @param $dirName
  62. * @param bool $subdir
  63. */
  64. protected function delDirAndFile($dirName)
  65. {
  66. $list = glob($dirName . '*');
  67. foreach ($list as $file) {
  68. if (is_dir($file))
  69. $this->delDirAndFile($file . DS);
  70. else
  71. @unlink($file);
  72. }
  73. @rmdir($dirName);
  74. }
  75. }