SystemFileInfoServices.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 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\dao\system\log\SystemFileInfoDao;
  13. use app\services\BaseServices;
  14. /**
  15. * @author 吴汐
  16. * @email 442384644@qq.com
  17. * @date 2023/04/07
  18. */
  19. class SystemFileInfoServices extends BaseServices
  20. {
  21. /**
  22. * 构造方法
  23. * SystemLogServices constructor.
  24. * @param SystemFileInfoDao $dao
  25. */
  26. public function __construct(SystemFileInfoDao $dao)
  27. {
  28. $this->dao = $dao;
  29. }
  30. public function syncfile()
  31. {
  32. $list = $this->flattenArray($this->scanDirectory());
  33. $this->dao->saveAll($list);
  34. }
  35. public function scanDirectory($dir = '')
  36. {
  37. if ($dir == '') $dir = root_path();
  38. $result = array();
  39. $files = scandir($dir);
  40. foreach ($files as $file) {
  41. if ($file != '.' && $file != '..') {
  42. $path = $dir . '/' . $file;
  43. $fileInfo = array(
  44. 'name' => $file,
  45. 'update_time' => date('Y-m-d H:i:s', filemtime($path)),
  46. 'create_time' => date('Y-m-d H:i:s', filectime($path)),
  47. 'path' => str_replace(root_path(), '', $dir),
  48. 'full_path' => str_replace(root_path(), '', $path),
  49. );
  50. if (is_dir($path)) {
  51. $fileInfo['type'] = 'dir';
  52. $fileInfo['contents'] = $this->scanDirectory($path);
  53. } else {
  54. $fileInfo['type'] = 'file';
  55. }
  56. $result[] = $fileInfo;
  57. }
  58. }
  59. return $result;
  60. }
  61. public function flattenArray($arr)
  62. {
  63. $result = array();
  64. foreach ($arr as $item) {
  65. $result[] = array(
  66. 'name' => $item['name'],
  67. 'type' => $item['type'],
  68. 'update_time' => $item['update_time'],
  69. 'create_time' => $item['create_time'],
  70. 'path' => $item['path'],
  71. 'full_path' => $item['full_path'],
  72. );
  73. if (isset($item['contents'])) {
  74. $result = array_merge($result, $this->flattenArray($item['contents']));
  75. }
  76. }
  77. return $result;
  78. }
  79. }