ClearServices.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system\log;
  12. use app\services\BaseServices;
  13. class ClearServices extends BaseServices
  14. {
  15. /** 递归删除文件
  16. * @param $dirName
  17. * @param bool $subdir
  18. */
  19. protected function delDirAndFile($dirName)
  20. {
  21. $list = glob($dirName . '*');
  22. foreach ($list as $file) {
  23. if (is_dir($file))
  24. $this->delDirAndFile($file . DS);
  25. else
  26. @unlink($file);
  27. }
  28. @rmdir($dirName);
  29. }
  30. /**
  31. * 删除日志
  32. */
  33. public function deleteLog()
  34. {
  35. $root = app()->getRootPath() . 'runtime' . DS;
  36. $this->delDirAndFile($root . 'admin' . DS . 'log' . DS);
  37. $this->delDirAndFile($root . 'api' . DS . 'log' . DS);
  38. $this->delDirAndFile($root . 'log' . DS);
  39. }
  40. /**
  41. * 刷新数据缓存
  42. */
  43. public function refresCache()
  44. {
  45. $root = app()->getRootPath() . 'runtime' . DS;
  46. $adminRoute = $root . 'admin';
  47. $apiRoute = $root . 'api';
  48. $cacheRoute = $root . 'cache';
  49. $cache = [];
  50. if (is_dir($adminRoute))
  51. $cache[$adminRoute] = scandir($adminRoute);
  52. if (is_dir($apiRoute))
  53. $cache[$apiRoute] = scandir($apiRoute);
  54. if (is_dir($cacheRoute))
  55. $cache[$cacheRoute] = scandir($cacheRoute);
  56. foreach ($cache as $p => $list) {
  57. foreach ($list as $file) {
  58. if (!in_array($file, ['.', '..', 'log', 'schema', 'route.php'])) {
  59. $path = $p . DS . $file;
  60. if (is_file($path)) {
  61. @unlink($path);
  62. } else {
  63. $this->delDirAndFile($path . DS);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }