SystemFileInfoServices.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. * 构造方法
  14. * SystemLogServices constructor.
  15. * @param SystemFileInfoDao $dao
  16. */
  17. public function __construct(SystemFileInfoDao $dao)
  18. {
  19. $this->dao = $dao;
  20. }
  21. public function syncfile()
  22. {
  23. $list = $this->flattenArray($this->scanDirectory());
  24. $this->dao->saveAll($list);
  25. }
  26. public function scanDirectory($dir = '')
  27. {
  28. if ($dir == '') $dir = root_path();
  29. $result = array();
  30. $files = scandir($dir);
  31. foreach ($files as $file) {
  32. if ($file != '.' && $file != '..') {
  33. $path = $dir . '/' . $file;
  34. $fileInfo = array(
  35. 'name' => $file,
  36. 'update_time' => date('Y-m-d H:i:s', filemtime($path)),
  37. 'create_time' => date('Y-m-d H:i:s', filectime($path)),
  38. 'path' => str_replace(root_path(), '', $dir),
  39. 'full_path' => str_replace(root_path(), '', $path),
  40. );
  41. if (is_dir($path)) {
  42. $fileInfo['type'] = 'dir';
  43. $fileInfo['contents'] = $this->scanDirectory($path);
  44. } else {
  45. $fileInfo['type'] = 'file';
  46. }
  47. $result[] = $fileInfo;
  48. }
  49. }
  50. return $result;
  51. }
  52. public function flattenArray($arr)
  53. {
  54. $result = array();
  55. foreach ($arr as $item) {
  56. $result[] = array(
  57. 'name' => $item['name'],
  58. 'type' => $item['type'],
  59. 'update_time' => $item['update_time'],
  60. 'create_time' => $item['create_time'],
  61. 'path' => $item['path'],
  62. 'full_path' => $item['full_path'],
  63. );
  64. if (isset($item['contents'])) {
  65. $result = array_merge($result, $this->flattenArray($item['contents']));
  66. }
  67. }
  68. return $result;
  69. }
  70. }