Skip to content

Commit

Permalink
fix: 优化系统目录扫描
Browse files Browse the repository at this point in the history
  • Loading branch information
zoujingli committed Aug 31, 2024
1 parent 0cf4a12 commit 9c6b1c4
Showing 1 changed file with 34 additions and 41 deletions.
75 changes: 34 additions & 41 deletions plugin/think-library/src/extend/ToolsExtend.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,20 @@ public static function copyfile(string $frdir, string $todir, array $files = [],
$todir = rtrim($todir, '\\/') . DIRECTORY_SEPARATOR;
// 扫描目录文件
if (empty($files) && is_dir($frdir)) {
$files = static::findFilesArray($frdir, static function (SplFileInfo $info) {
return substr($info->getBasename(), 0, 1) !== '.';
}, static function (SplFileInfo $info) {
return substr($info->getBasename(), 0, 1) !== '.';
});
$filter = function (SplFileInfo $info) {
return $info->getBasename()[0] !== '.';
};
$files = static::findFilesArray($frdir, $filter, $filter);
}
// 复制文件列表
foreach ($files as $target) {
if ($force || !is_file($todir . $target)) {
$dir = dirname($todir . $target);
is_dir($dir) or mkdir($dir, 0777, true);
copy($frdir . $target, $todir . $target);
[$fromPath, $destPath] = [$frdir . $target, $todir . $target];
if ($force || !is_file($destPath)) {
is_dir($dir = dirname($destPath)) || mkdir($dir, 0777, true);
copy($fromPath, $destPath);
}
// 删除来源文件
$remove && unlink($frdir . $target);
$remove && unlink($fromPath);
}
// 删除来源目录
$remove && static::removeEmptyDirectory($frdir);
Expand All @@ -76,51 +75,45 @@ public static function copyfile(string $frdir, string $todir, array $files = [],
public static function scanDirectory(string $path, string $filterExt = '', bool $shortPath = true): array
{
return static::findFilesArray($path, static function (SplFileInfo $info) use ($filterExt) {
return empty($filterExt) || $info->getExtension() === $filterExt;
return !$filterExt || $info->getExtension() === $filterExt;
}, static function (SplFileInfo $info) {
return substr($info->getBasename(), 0, 1) !== '.';
return $info->getBasename()[0] !== '.';
}, $shortPath);
}

/**
* 扫描指定目录
* @param string $path
* @param ?Closure $filterFile
* @param ?Closure $filterPath
* @param boolean $shortPath
* @return array
* 扫描指定目录并返回文件路径数组
* @param string $path 要扫描的目录路径
* @param ?Closure $filterFile 用于过滤文件的闭包
* @param ?Closure $filterPath 用于过滤目录的闭包
* @param boolean $shortPath 是否返回相对于给定路径的短路径
* @return array 包含文件路径的数组
*/
public static function findFilesArray(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, bool $shortPath = true): array
{
$items = [];
if (file_exists($path)) {
$files = static::findFilesYield($path, $filterFile, $filterPath);
foreach ($files as $file) $items[] = $file->getRealPath();
if ($shortPath && ($offset = strlen(realpath($path)) + 1)) {
foreach ($items as &$item) $item = substr($item, $offset);
}
}
return $items;
$pathLength = $shortPath ? strlen(realpath($path)) + 1 : 0;
return file_exists($path) ? array_map(function ($file) use ($shortPath, $pathLength) {
return $shortPath ? substr($file->getRealPath(), $pathLength) : $file->getRealPath();
}, iterator_to_array(static::findFilesYield($path, $filterFile, $filterPath))) : [];
}

/**
* 扫描指定目录
* @param string $path
* @param \Closure|null $filterFile
* @param \Closure|null $filterPath
* @param boolean $fullDirectory
* @return \Generator|\SplFileInfo[]
* 递归扫描指定目录,返回文件或目录的 SplFileInfo 对象。
* @param string $path 目录路径。
* @param \Closure|null $filterFile 文件过滤器闭包,返回 true 表示文件被接受。
* @param \Closure|null $filterPath 目录过滤器闭包,返回 true 表示目录被接受。
* @param boolean $fullDirectory 是否包含目录本身在结果中。
* @return \Generator 返回 SplFileInfo 对象的生成器。
*/
public static function findFilesYield(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, bool $fullDirectory = false): Generator
{
if (file_exists($path)) {
$items = is_file($path) ? [new SplFileInfo($path)] : new FilesystemIterator($path);
foreach ($items as $item) if ($item->isDir() && !$item->isLink()) {
if (is_null($filterPath) || $filterPath($item)) {
yield from static::findFilesYield($item->getPathname(), $filterFile, $filterPath, $fullDirectory);
}
$fullDirectory && yield $item;
} elseif (is_null($filterFile) || $filterFile($item)) {
if (!file_exists($path)) return;
foreach (is_file($path) ? [new SplFileInfo($path)] : new FilesystemIterator($path) as $item) {
$isDir = $item->isDir() && !$item->isLink();
if ($isDir && ($filterPath === null || $filterPath($item))) {
yield from static::findFilesYield($item->getPathname(), $filterFile, $filterPath, $fullDirectory);
if ($fullDirectory) yield $item;
} elseif (!$isDir && ($filterFile === null || $filterFile($item))) {
yield $item;
}
}
Expand Down

0 comments on commit 9c6b1c4

Please sign in to comment.