SystemFileInfoServices.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app\services\system\log;
  3. use app\dao\system\log\SystemFileInfoDao;
  4. use app\services\BaseServices;
  5. /**
  6. * @author 吴汐
  7. * @email 442384644@qq.com
  8. * @date 2023/04/07
  9. */
  10. class SystemFileInfoServices extends BaseServices
  11. {
  12. // 排除部分目录
  13. protected $excluded_directories = array(
  14. '/runtime/cache',
  15. '/runtime/log',
  16. '/runtime/session',
  17. '/runtime/temp',
  18. '/public/uploads/attach',
  19. '/public/install/images/install',
  20. '/public/admin/system_static/css',
  21. '/public/admin/system_static/js',
  22. '/public/admin/system_static/img',
  23. '/public/admin/system_static/fonts',
  24. '/public/admin/system_static/media',
  25. '/public/static/css',
  26. '/public/static/js',
  27. '/public/static/img',
  28. '/public/static/images',
  29. '/public/statics/images',
  30. '/public/statics/mp_view/static',
  31. '/vendor'
  32. );
  33. /**
  34. * 构造方法
  35. * SystemLogServices constructor.
  36. * @param SystemFileInfoDao $dao
  37. */
  38. public function __construct(SystemFileInfoDao $dao)
  39. {
  40. $this->dao = $dao;
  41. }
  42. // 同步文件信息
  43. public function syncfile()
  44. {
  45. $list = $this->flattenArray($this->scanDirectory());
  46. $this->dao->saveAll($list);
  47. }
  48. //查询文件目录是否在排除目录中,是则返回true,否则返回false
  49. public function isExcludedDirectory($string) {
  50. foreach ($this->excluded_directories as $item) {
  51. if (strpos($string,$item) === 0) {
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. // 递归扫描目录
  58. public function scanDirectory($dir = '')
  59. {
  60. if ($dir == '') $dir = root_path();
  61. $result = array();
  62. // 获取目录下的所有文件和子目录
  63. $files = array_diff(scandir($dir), array('.', '..'));
  64. // 遍历文件和子目录
  65. foreach ($files as $file) {
  66. $path = $dir . '/' . $file;
  67. $fileInfo = array(
  68. 'name' => $file,
  69. 'update_time' => date('Y-m-d H:i:s', filemtime($path)),
  70. 'create_time' => date('Y-m-d H:i:s', filectime($path)),
  71. 'path' => str_replace(root_path(), '', $dir),
  72. 'full_path' => str_replace(root_path(), '', $path),
  73. );
  74. // 判断是否是目录 并不在排除目录中
  75. if (is_dir($path) && !$this->isExcludedDirectory($file, $this->excluded_directories)) {
  76. $fileInfo['type'] = 'dir';
  77. $fileInfo['contents'] = $this->scanDirectory($path);
  78. } else {
  79. $fileInfo['type'] = 'file';
  80. }
  81. $result[] = $fileInfo;
  82. }
  83. return $result;
  84. }
  85. // 数组扁平化
  86. public function flattenArray($arr)
  87. {
  88. $result = array();
  89. foreach ($arr as $item) {
  90. //如果不是排除目录
  91. if(!$this->isExcludedDirectory($item['path'])){
  92. $result[] = array(
  93. 'name' => $item['name'],
  94. 'type' => $item['type'],
  95. 'update_time' => $item['update_time'],
  96. 'create_time' => $item['create_time'],
  97. 'path' => $item['path'],
  98. 'full_path' => $item['full_path'],
  99. );
  100. if (isset($item['contents'])) {
  101. $result = array_merge($result, $this->flattenArray($item['contents']));
  102. }
  103. }
  104. }
  105. return $result;
  106. }
  107. }