Browse Source

add function for menus

master
filesite 3 years ago
parent
commit
7c7865cf61
  1. 45
      lib/DirScanner.php
  2. 14
      test/DirScannerTest.php
  3. 71
      test/init_test_files.sh

45
lib/DirScanner.php

@ -33,6 +33,7 @@ Class DirScanner {
private $scanningDirLevel = 0; //当前扫描的目录深度 private $scanningDirLevel = 0; //当前扫描的目录深度
private $scanStartTime = 0; //扫描开始时间,单位:秒 private $scanStartTime = 0; //扫描开始时间,单位:秒
private $scanResults = []; //目录扫描结果 private $scanResults = []; //目录扫描结果
private $tree = []; //目录扫描树形结构
protected $supportFileExtensions = [ //支持的文件类型 protected $supportFileExtensions = [ //支持的文件类型
@ -432,7 +433,7 @@ Class DirScanner {
$realpath = realpath("{$dir}{$file}"); $realpath = realpath("{$dir}{$file}");
if (is_dir($realpath)) { if (is_dir($realpath)) {
$files = []; $files = [];
if ($nextLevels > 0) { if ($nextLevels >= 0) {
$files = $this->scan($realpath, $nextLevels); $files = $this->scan($realpath, $nextLevels);
if (!empty($files)) { if (!empty($files)) {
foreach($files as $file) { foreach($files as $file) {
@ -462,6 +463,8 @@ Class DirScanner {
} }
} }
$this->tree = $tree;
$time = microtime(true); $time = microtime(true);
$this->scanTimeCost = $this->scanStartTime > 0 ? ceil( ($time - $this->scanStartTime)*1000 ) : 0; $this->scanTimeCost = $this->scanStartTime > 0 ? ceil( ($time - $this->scanStartTime)*1000 ) : 0;
@ -473,4 +476,44 @@ Class DirScanner {
return $this->scanResults; 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;
}
} }

14
test/DirScannerTest.php

@ -66,13 +66,23 @@ $scanner = new DirScannerTest();
//$dirTree = $scanner->scan('./'); //$dirTree = $scanner->scan('./');
//$dirTree = $scanner->scan(__DIR__); //$dirTree = $scanner->scan(__DIR__);
$dirTree = $scanner->scan(__DIR__ . '/', 3); $dirTree = $scanner->scan(__DIR__ . '/content/', 4);
echo "Time cost: {$scanner->scanTimeCost} ms\n"; echo "Time cost: {$scanner->scanTimeCost} ms\n";
echo "\n"; echo "\n";
print_r($dirTree);
$menus = $scanner->getMenus();
print_r($menus);
echo "\n"; echo "\n";
echo "\n"; echo "\n";
//print_r($dirTree);
//echo "\n";
//echo "\n";
//$scanResults = $scanner->getScanResults(); //$scanResults = $scanner->getScanResults();
//print_r($scanResults); //print_r($scanResults);
//echo "\n"; //echo "\n";
//echo "\n";

71
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" <<EOF
[InternetShortcut]
URL=https://www.google.com
EOF
echo '/favs/小说/金庸小说/最爱金庸网站图标.ico' > "小说/金庸小说/snapshot.txt"
touch "小说/古龙小说/最爱古龙网站图标.ico"
touch "小说/古龙小说/最爱古龙.url"
tee -a "小说/金庸小说/最爱古龙.url" <<EOF
[InternetShortcut]
URL=https://www.google.com
EOF
touch "图片/图片搜索/谷歌图片搜索图标.ico"
touch "图片/图片搜索/谷歌图片搜索.url"
tee -a "图片/图片搜索/谷歌图片搜索.url" <<EOF
[InternetShortcut]
URL=https://www.google.com
EOF
touch "图片/图片搜索/description.txt"
tee -a "图片/图片搜索/description.txt" <<EOF
什么图片都能搜,
输入关键词,然后点“搜索”
EOF
echo '/favs/图片/图片搜索/谷歌图片搜索图标.ico' > "图片/图片搜索/snapshot.txt"
touch "图片/必应图片搜索/bing图标.ico"
touch "图片/必应图片搜索/bing.url"
tee -a "图片/必应图片搜索/bing.url" <<EOF
[InternetShortcut]
URL=https://www.bing.com
EOF
touch "图片/必应图片搜索/title.txt"
tee -a "图片/必应图片搜索/title.txt" <<EOF
Bing必应图片搜索
EOF
Loading…
Cancel
Save