Clear.php 2.0 KB

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