mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2026-06-11 13:58:11 +08:00
fix: 优化目录扫描
This commit is contained in:
parent
24b5dbcbeb
commit
29337c45a0
@ -73,7 +73,7 @@ class ToolsExtend
|
|||||||
*/
|
*/
|
||||||
public static function removeEmptyDirectory(string $path): bool
|
public static function removeEmptyDirectory(string $path): bool
|
||||||
{
|
{
|
||||||
foreach (self::findFilesYield($path, null, null, true) as $item) {
|
foreach (self::findFilesYield($path, null, null, null, true) as $item) {
|
||||||
($item->isFile() || $item->isLink()) ? unlink($item->getRealPath()) : rmdir($item->getRealPath());
|
($item->isFile() || $item->isLink()) ? unlink($item->getRealPath()) : rmdir($item->getRealPath());
|
||||||
}
|
}
|
||||||
return is_file($path) ? unlink($path) : (!is_dir($path) || rmdir($path));
|
return is_file($path) ? unlink($path) : (!is_dir($path) || rmdir($path));
|
||||||
@ -107,16 +107,17 @@ class ToolsExtend
|
|||||||
*/
|
*/
|
||||||
public static function findFilesArray(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, bool $shortPath = true, ?int $depth = null): array
|
public static function findFilesArray(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, bool $shortPath = true, ?int $depth = null): array
|
||||||
{
|
{
|
||||||
if (is_dir($path)) {
|
$files = [];
|
||||||
$pathSize = $shortPath ? strlen(realpath($path)) + 1 : 0;
|
if (is_file($path)) {
|
||||||
return file_exists($path) ? array_map(function ($file) use ($pathSize, $shortPath) {
|
if (($file = new SplFileInfo($path)) && ($filterFile === null || $filterFile($file))) {
|
||||||
return $shortPath ? substr($file->getRealPath(), $pathSize) : $file->getRealPath();
|
$files[] = $shortPath ? $file->getBasename() : $file->getPathname();
|
||||||
}, iterator_to_array(static::findFilesYield($path, $filterFile, $filterPath, false, $depth))) : [];
|
}
|
||||||
} elseif (is_file($path)) {
|
} elseif (is_dir($path)) {
|
||||||
return $filterFile === null || $filterFile(new SplFileInfo($path)) ? [basename($path)] : [];
|
foreach (static::findFilesYield($path, $filterFile, $filterPath, $depth) as $file) {
|
||||||
} else {
|
$files[] = $shortPath ? substr($file->getRealPath(), strlen($path) + 1) : $file->getRealPath();
|
||||||
return [];
|
}
|
||||||
}
|
}
|
||||||
|
return $files;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -124,22 +125,22 @@ class ToolsExtend
|
|||||||
* @param string $path 目录路径。
|
* @param string $path 目录路径。
|
||||||
* @param \Closure|null $filterFile 文件过滤器闭包,返回 true 表示文件被接受。
|
* @param \Closure|null $filterFile 文件过滤器闭包,返回 true 表示文件被接受。
|
||||||
* @param \Closure|null $filterPath 目录过滤器闭包,返回 true 表示目录被接受。
|
* @param \Closure|null $filterPath 目录过滤器闭包,返回 true 表示目录被接受。
|
||||||
* @param boolean $appendPath 是否包含目录本身在结果中。
|
|
||||||
* @param ?integer $depth 当前递归深度,null 表示无限制深度
|
* @param ?integer $depth 当前递归深度,null 表示无限制深度
|
||||||
|
* @param boolean $appendPath 是否包含目录本身在结果中。
|
||||||
* @return \Generator 返回 SplFileInfo 对象的生成器。
|
* @return \Generator 返回 SplFileInfo 对象的生成器。
|
||||||
*/
|
*/
|
||||||
private static function findFilesYield(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, bool $appendPath = false, ?int $depth = null): Generator
|
private static function findFilesYield(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, ?int $depth = null, bool $appendPath = false): Generator
|
||||||
{
|
{
|
||||||
if (!file_exists($path)) return;
|
if (file_exists($path)) {
|
||||||
foreach (is_file($path) ? [new SplFileInfo($path)] : new FilesystemIterator($path, FilesystemIterator::SKIP_DOTS) as $item) {
|
foreach (is_file($path) ? [new SplFileInfo($path)] : new FilesystemIterator($path, FilesystemIterator::SKIP_DOTS) as $item) {
|
||||||
$isDir = $item->isDir() && !$item->isLink();
|
if (($isDir = $item->isDir() && !$item->isLink()) && ($filterPath === null || $filterPath($item))) {
|
||||||
if ($isDir && ($filterPath === null || $filterPath($item))) {
|
if ($depth === null || $depth > 0) {
|
||||||
if ($depth === null || $depth > 0) {
|
yield from static::findFilesYield($item->getPathname(), $filterFile, $filterPath, $depth !== null ? $depth - 1 : null, $appendPath);
|
||||||
yield from static::findFilesYield($item->getPathname(), $filterFile, $filterPath, $appendPath, $depth !== null ? $depth - 1 : null);
|
}
|
||||||
|
if ($appendPath) yield $item;
|
||||||
|
} elseif (!$isDir && ($filterFile === null || $filterFile($item))) {
|
||||||
|
yield $item;
|
||||||
}
|
}
|
||||||
if ($appendPath) yield $item;
|
|
||||||
} elseif (!$isDir && ($filterFile === null || $filterFile($item))) {
|
|
||||||
yield $item;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user