diff --git a/lib/DirScanner.php b/lib/DirScanner.php index 702db33..6a629ee 100644 --- a/lib/DirScanner.php +++ b/lib/DirScanner.php @@ -33,6 +33,7 @@ Class DirScanner { private $scanningDirLevel = 0; //当前扫描的目录深度 private $scanStartTime = 0; //扫描开始时间,单位:秒 private $scanResults = []; //目录扫描结果 + private $tree = []; //目录扫描树形结构 protected $supportFileExtensions = [ //支持的文件类型 @@ -432,7 +433,7 @@ Class DirScanner { $realpath = realpath("{$dir}{$file}"); if (is_dir($realpath)) { $files = []; - if ($nextLevels > 0) { + if ($nextLevels >= 0) { $files = $this->scan($realpath, $nextLevels); if (!empty($files)) { foreach($files as $file) { @@ -462,6 +463,8 @@ Class DirScanner { } } + $this->tree = $tree; + $time = microtime(true); $this->scanTimeCost = $this->scanStartTime > 0 ? ceil( ($time - $this->scanStartTime)*1000 ) : 0; @@ -473,4 +476,44 @@ Class DirScanner { return $this->scanResults; } + //获取菜单,扫描结果中的目录结构 + public function getMenus($tree = []) { + $results = empty($tree) ? $this->tree : $tree; + $menus = []; + if (empty($results)) {return $menus;} + + foreach ($results as $id => $item) { + $dir = []; + if (!empty($item['directory'])) { + $dir = [ + 'id' => $item['id'], + 'directory' => $item['directory'], + 'path' => $item['path'], + ]; + if (!empty($item['snapshot'])) { + $dir['snapshot'] = preg_replace('/\s/', '', $item['snapshot']); + } + if (!empty($item['title'])) { + $dir['title'] = preg_replace('/\s/', '', $item['title']); + } + if (!empty($item['description'])) { + $dir['description'] = $item['description']; + } + } + + if (!empty($item['files'])) { + $dirs = $this->getMenus($item['files']); + if (!empty($dirs)) { + $dir['directories'] = $dirs; + } + } + + if (!empty($dir)) { + $menus[] = $dir; + } + } + + return $menus; + } + } diff --git a/test/DirScannerTest.php b/test/DirScannerTest.php index ff02788..545c049 100644 --- a/test/DirScannerTest.php +++ b/test/DirScannerTest.php @@ -66,13 +66,23 @@ $scanner = new DirScannerTest(); //$dirTree = $scanner->scan('./'); //$dirTree = $scanner->scan(__DIR__); -$dirTree = $scanner->scan(__DIR__ . '/', 3); +$dirTree = $scanner->scan(__DIR__ . '/content/', 4); echo "Time cost: {$scanner->scanTimeCost} ms\n"; echo "\n"; -print_r($dirTree); + +$menus = $scanner->getMenus(); +print_r($menus); echo "\n"; echo "\n"; + +//print_r($dirTree); +//echo "\n"; +//echo "\n"; + //$scanResults = $scanner->getScanResults(); //print_r($scanResults); //echo "\n"; +//echo "\n"; + + diff --git a/test/init_test_files.sh b/test/init_test_files.sh new file mode 100755 index 0000000..cf04303 --- /dev/null +++ b/test/init_test_files.sh @@ -0,0 +1,71 @@ +#!/bin/sh +##-d- 小说 +## |_d_ 金庸小说 +## |_f_ 最爱金庸网站图标.ico +## |_f_ 最爱金庸.url +## |_d_ 古龙小说 +## |_f_ 最爱古龙网站图标.ico +## |_f_ 最爱古龙.url +##-d- 图片 +## |_d_ 图片搜索 +## |_f_ 谷歌图片搜索图标.ico +## |_f_ 谷歌图片搜索.url +## |_f_ description.txt +## |_d_ 必应图片搜索 +## |_f_ bing图标.ico +## |_f_ bing.url +## |_f_ title.txt + +rm -rf content/ + + +#---for favs--- +mkdir -p content/favs/ +cd content/favs/ + +rm -rf "小说/" +rm -rf "图片/" + +mkdir -p "小说/金庸小说/" +mkdir -p "小说/古龙小说/" +mkdir -p "图片/图片搜索/" +mkdir -p "图片/必应图片搜索/" + +touch "小说/金庸小说/最爱金庸网站图标.ico" +touch "小说/金庸小说/最爱金庸.url" +tee -a "小说/金庸小说/最爱金庸.url" < "小说/金庸小说/snapshot.txt" + +touch "小说/古龙小说/最爱古龙网站图标.ico" +touch "小说/古龙小说/最爱古龙.url" +tee -a "小说/金庸小说/最爱古龙.url" < "图片/图片搜索/snapshot.txt" + +touch "图片/必应图片搜索/bing图标.ico" +touch "图片/必应图片搜索/bing.url" +tee -a "图片/必应图片搜索/bing.url" <