Parcourir la source

【程序目录】增加排除文件,避免前端和缓存文件写入数据看

sugar1569 il y a 2 ans
Parent
commit
b03f923b95
1 fichiers modifiés avec 64 ajouts et 30 suppressions
  1. 64 30
      crmeb/app/services/system/log/SystemFileInfoServices.php

+ 64 - 30
crmeb/app/services/system/log/SystemFileInfoServices.php

@@ -12,6 +12,27 @@ use app\services\BaseServices;
  */
  */
 class SystemFileInfoServices extends BaseServices
 class SystemFileInfoServices extends BaseServices
 {
 {
+    // 排除部分目录
+    protected $excluded_directories = array(
+        '/runtime/cache',
+        '/runtime/log',
+        '/runtime/session',
+        '/runtime/temp',
+        '/public/uploads/attach',
+        '/public/install/images/install',
+        '/public/admin/system_static/css',
+        '/public/admin/system_static/js',
+        '/public/admin/system_static/img',
+        '/public/admin/system_static/fonts',
+        '/public/admin/system_static/media',
+        '/public/static/css',
+        '/public/static/js',
+        '/public/static/img',
+        '/public/static/images',
+        '/public/statics/images',
+        '/public/statics/mp_view/static',
+        '/vendor'
+        );
     /**
     /**
      * 构造方法
      * 构造方法
      * SystemLogServices constructor.
      * SystemLogServices constructor.
@@ -21,54 +42,67 @@ class SystemFileInfoServices extends BaseServices
     {
     {
         $this->dao = $dao;
         $this->dao = $dao;
     }
     }
-
+    // 同步文件信息
     public function syncfile()
     public function syncfile()
     {
     {
         $list = $this->flattenArray($this->scanDirectory());
         $list = $this->flattenArray($this->scanDirectory());
         $this->dao->saveAll($list);
         $this->dao->saveAll($list);
     }
     }
-
+    //查询文件目录是否在排除目录中,是则返回true,否则返回false
+    public function isExcludedDirectory($string) {
+        foreach ($this->excluded_directories as $item) {
+            if (strpos($string,$item) === 0) {
+                return true;
+            }
+        }
+        return false;
+    }
+    // 递归扫描目录
     public function scanDirectory($dir = '')
     public function scanDirectory($dir = '')
     {
     {
         if ($dir == '') $dir = root_path();
         if ($dir == '') $dir = root_path();
         $result = array();
         $result = array();
-        $files = scandir($dir);
+        // 获取目录下的所有文件和子目录
+        $files = array_diff(scandir($dir), array('.', '..'));
+        // 遍历文件和子目录
         foreach ($files as $file) {
         foreach ($files as $file) {
-            if ($file != '.' && $file != '..') {
-                $path = $dir . '/' . $file;
-                $fileInfo = array(
-                    'name' => $file,
-                    'update_time' => date('Y-m-d H:i:s', filemtime($path)),
-                    'create_time' => date('Y-m-d H:i:s', filectime($path)),
-                    'path' => str_replace(root_path(), '', $dir),
-                    'full_path' => str_replace(root_path(), '', $path),
-                );
-                if (is_dir($path)) {
-                    $fileInfo['type'] = 'dir';
-                    $fileInfo['contents'] = $this->scanDirectory($path);
-                } else {
-                    $fileInfo['type'] = 'file';
-                }
-                $result[] = $fileInfo;
+            $path = $dir . '/' . $file;
+            $fileInfo = array(
+                'name' => $file,
+                'update_time' => date('Y-m-d H:i:s', filemtime($path)),
+                'create_time' => date('Y-m-d H:i:s', filectime($path)),
+                'path' => str_replace(root_path(), '', $dir),
+                'full_path' => str_replace(root_path(), '', $path),
+            );
+            // 判断是否是目录 并不在排除目录中
+            if (is_dir($path) && !$this->isExcludedDirectory($file, $this->excluded_directories)) {
+                $fileInfo['type'] = 'dir';
+                $fileInfo['contents'] = $this->scanDirectory($path);
+            } else {
+                $fileInfo['type'] = 'file';
             }
             }
+            $result[] = $fileInfo;
         }
         }
         return $result;
         return $result;
     }
     }
-
+    // 数组扁平化
     public function flattenArray($arr)
     public function flattenArray($arr)
     {
     {
         $result = array();
         $result = array();
         foreach ($arr as $item) {
         foreach ($arr as $item) {
-            $result[] = array(
-                'name' => $item['name'],
-                'type' => $item['type'],
-                'update_time' => $item['update_time'],
-                'create_time' => $item['create_time'],
-                'path' => $item['path'],
-                'full_path' => $item['full_path'],
-            );
-            if (isset($item['contents'])) {
-                $result = array_merge($result, $this->flattenArray($item['contents']));
+            //如果不是排除目录
+            if(!$this->isExcludedDirectory($item['path'])){
+                $result[] = array(
+                    'name' => $item['name'],
+                    'type' => $item['type'],
+                    'update_time' => $item['update_time'],
+                    'create_time' => $item['create_time'],
+                    'path' => $item['path'],
+                    'full_path' => $item['full_path'],
+                );
+                if (isset($item['contents'])) {
+                    $result = array_merge($result, $this->flattenArray($item['contents']));
+                }
             }
             }
         }
         }
         return $result;
         return $result;