ClearServices.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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. use crmeb\services\CacheService;
  14. /**
  15. * Class ClearServices
  16. * @package app\services\system\log
  17. */
  18. class ClearServices extends BaseServices
  19. {
  20. /** 递归删除文件
  21. * @param $dirName
  22. * @param bool $subdir
  23. */
  24. protected function delDirAndFile($dirName)
  25. {
  26. $list = glob($dirName . '*');
  27. foreach ($list as $file) {
  28. if (is_dir($file))
  29. $this->delDirAndFile($file . DS);
  30. else
  31. @unlink($file);
  32. }
  33. @rmdir($dirName);
  34. }
  35. /**
  36. * 删除日志
  37. */
  38. public function deleteLog()
  39. {
  40. $root = app()->getRootPath() . 'runtime' . DS;
  41. $this->delDirAndFile($root . 'admin' . DS . 'log' . DS);
  42. $this->delDirAndFile($root . 'api' . DS . 'log' . DS);
  43. $this->delDirAndFile($root . 'log' . DS);
  44. }
  45. /**
  46. * 刷新数据缓存
  47. */
  48. public function refreshCache()
  49. {
  50. $root = app()->getRootPath() . 'runtime' . DS;
  51. $adminRoute = $root . 'admin';
  52. $apiRoute = $root . 'api';
  53. $cacheRoute = $root . 'cache';
  54. $cache = [];
  55. if (is_dir($adminRoute))
  56. $cache[$adminRoute] = scandir($adminRoute);
  57. if (is_dir($apiRoute))
  58. $cache[$apiRoute] = scandir($apiRoute);
  59. if (is_dir($cacheRoute))
  60. $cache[$cacheRoute] = scandir($cacheRoute);
  61. foreach ($cache as $p => $list) {
  62. foreach ($list as $file) {
  63. if (!in_array($file, ['.', '..', 'log', 'schema', 'route.php'])) {
  64. $path = $p . DS . $file;
  65. if (is_file($path)) {
  66. @unlink($path);
  67. } else {
  68. $this->delDirAndFile($path . DS);
  69. }
  70. }
  71. }
  72. }
  73. CacheService::clearAll();
  74. }
  75. }