ClearServices.php 2.4 KB

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