Skip to content

Commit

Permalink
fix: 修改文件扫描,增加深度参数
Browse files Browse the repository at this point in the history
  • Loading branch information
zoujingli committed Sep 9, 2024
1 parent 328bdc1 commit ee67838
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
4 changes: 2 additions & 2 deletions plugin/think-library/src/Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ public function register()
{
// 动态加载全局配置
[$dir, $ext] = [$this->app->getBasePath(), $this->app->getConfigExt()];
ToolsExtend::findFilesYield($dir, function (SplFileInfo $info) use ($ext) {
ToolsExtend::findFilesArray($dir, function (SplFileInfo $info) use ($ext) {
$info->getBasename() === "sys{$ext}" && include_once $info->getPathname();
});
}, null, true, 1);
if (is_file($file = "{$dir}common{$ext}")) include_once $file;
if (is_file($file = "{$dir}provider{$ext}")) $this->app->bind(include $file);
if (is_file($file = "{$dir}event{$ext}")) $this->app->loadEvent(include $file);
Expand Down
2 changes: 1 addition & 1 deletion plugin/think-library/src/extend/PhinxExtend.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ private static function nextFile(string $class): string
ToolsExtend::removeEmptyDirectory($dataPath);
}
}
});
}, null, true, 1);

// 计算下一个版本号
$version = !empty($versions) ? min($versions) - 1 : $startVersion;
Expand Down
48 changes: 28 additions & 20 deletions plugin/think-library/src/extend/ToolsExtend.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,34 @@ public static function copyfile(string $frdir, string $todir, array $files = [],
return true;
}

/**
* 移除清空目录
* @param string $path
* @return boolean
*/
public static function removeEmptyDirectory(string $path): bool
{
foreach (self::findFilesYield($path, null, null, true) as $item) {
($item->isFile() || $item->isLink()) ? unlink($item->getRealPath()) : rmdir($item->getRealPath());
}
return is_file($path) ? unlink($path) : (!is_dir($path) || rmdir($path));
}

/**
* 扫描目录列表
* @param string $path 扫描目录
* @param string $filterExt 筛选后缀
* @param boolean $shortPath 相对路径
* @param ?integer $depth 当前递归深度,null 表示无限制深度
* @return array
*/
public static function scanDirectory(string $path, string $filterExt = '', bool $shortPath = true): array
public static function scanDirectory(string $path, string $filterExt = '', bool $shortPath = true, ?int $depth = null): array
{
return static::findFilesArray($path, static function (SplFileInfo $info) use ($filterExt) {
return !$filterExt || $info->getExtension() === $filterExt;
}, static function (SplFileInfo $info) {
return $info->getBasename()[0] !== '.';
}, $shortPath);
}, $shortPath, $depth);
}

/**
Expand All @@ -87,48 +101,42 @@ public static function scanDirectory(string $path, string $filterExt = '', bool
* @param ?Closure $filterFile 用于过滤文件的闭包
* @param ?Closure $filterPath 用于过滤目录的闭包
* @param boolean $shortPath 是否返回相对于给定路径的短路径
* @param ?integer $depth 当前递归深度,null 表示无限制深度
* @return array 包含文件路径的数组
*/
public static function findFilesArray(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, bool $shortPath = true): array
public static function findFilesArray(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, bool $shortPath = true, ?int $depth = null): array
{
if (!file_exists($path)) return [];
$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))) : [];
}, iterator_to_array(static::findFilesYield($path, $filterFile, $filterPath, false, $depth))) : [];
}

/**
* 递归扫描指定目录,返回文件或目录的 SplFileInfo 对象。
* @param string $path 目录路径。
* @param \Closure|null $filterFile 文件过滤器闭包,返回 true 表示文件被接受。
* @param \Closure|null $filterPath 目录过滤器闭包,返回 true 表示目录被接受。
* @param boolean $fullDirectory 是否包含目录本身在结果中。
* @param boolean $appendPath 是否包含目录本身在结果中。
* @param ?integer $depth 当前递归深度,null 表示无限制深度
* @return \Generator 返回 SplFileInfo 对象的生成器。
*/
public static function findFilesYield(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, bool $fullDirectory = false): Generator
private static function findFilesYield(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, bool $appendPath = false, ?int $depth = null): Generator
{
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;
if ($depth === null || $depth > 0) {
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;
}
}
}

/**
* 移除清空目录
* @param string $path
* @return boolean
*/
public static function removeEmptyDirectory(string $path): bool
{
foreach (self::findFilesYield($path, null, null, true) as $item) {
($item->isFile() || $item->isLink()) ? unlink($item->getRealPath()) : rmdir($item->getRealPath());
}
return is_file($path) ? unlink($path) : (!is_dir($path) || rmdir($path));
}

}
2 changes: 1 addition & 1 deletion plugin/think-library/src/support/middleware/MultAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private function loadMultiApp(string $appPath): bool
if (strtolower(".{$info->getExtension()}") === $ext) {
$this->app->config->load($info->getPathname(), $info->getBasename($ext));
}
});
}, null, true, 1);
// 加载应用路由配置
if (in_array('route', $fmaps) && method_exists($this->app->route, 'reload')) {
$this->app->route->reload();
Expand Down

0 comments on commit ee67838

Please sign in to comment.