Clear.php 2.1 KB

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