Skip to content

Commit

Permalink
fix: 优化 ToolsExtend::findFilesArray 操作,简化参数并调整参数顺序
Browse files Browse the repository at this point in the history
  • Loading branch information
zoujingli committed Oct 16, 2024
1 parent 52d3cb3 commit 4163868
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 43 deletions.
6 changes: 3 additions & 3 deletions 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::findFilesArray($dir, function (SplFileInfo $info) use ($ext) {
$info->getBasename() === "sys{$ext}" && include_once $info->getPathname();
}, null, true, 2);
ToolsExtend::findFilesArray($dir, 2, function (SplFileInfo $info) use ($ext) {
$info->isFile() && $info->getBasename() === "sys{$ext}" && include_once $info->getPathname();
});
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
16 changes: 9 additions & 7 deletions src/extend/PhinxExtend.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,17 @@ private function _create_{$table}()
private static function nextFile(string $class): string
{
[$snake, $items] = [Str::snake($class), [20010000000000]];
ToolsExtend::findFilesArray(syspath('database/migrations'), function (SplFileInfo $info) use ($snake, &$items) {
$bname = pathinfo($info->getBasename(), PATHINFO_FILENAME);
$items[] = $version = intval(substr($bname, 0, 14));
if ($snake === substr($bname, 15) && unlink($info->getRealPath())) {
if (is_dir($dataPath = $info->getPath() . DIRECTORY_SEPARATOR . $version)) {
ToolsExtend::removeEmptyDirectory($dataPath);
ToolsExtend::findFilesArray(syspath('database/migrations'), 1, function (SplFileInfo $info) use ($snake, &$items) {
if ($info->isFile()) {
$bname = pathinfo($info->getBasename(), PATHINFO_FILENAME);
$items[] = $version = intval(substr($bname, 0, 14));
if ($snake === substr($bname, 15) && unlink($info->getRealPath())) {
if (is_dir($dataPath = $info->getPath() . DIRECTORY_SEPARATOR . $version)) {
ToolsExtend::removeEmptyDirectory($dataPath);
}
}
}
}, null, true, 1);
});

// 计算下一个版本号
return sprintf("%s_{$snake}.php", min($items) - 1);
Expand Down
54 changes: 24 additions & 30 deletions src/extend/ToolsExtend.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ public static function copyfile(string $frdir, string $todir, array $files = [],
$todir = rtrim($todir, '\\/') . DIRECTORY_SEPARATOR;
// 扫描目录文件
if (empty($files) && is_dir($frdir)) {
$filter = function (SplFileInfo $info) {
$files = static::findFilesArray($frdir, null, function (SplFileInfo $info) {
return $info->getBasename()[0] !== '.';
};
$files = static::findFilesArray($frdir, $filter, $filter);
});
}
// 复制文件列表
foreach ($files as $target) {
Expand All @@ -72,7 +71,7 @@ public static function copyfile(string $frdir, string $todir, array $files = [],
*/
public static function removeEmptyDirectory(string $path): bool
{
foreach (self::findFilesYield($path, null, null, null, true) as $item) {
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));
Expand All @@ -81,38 +80,34 @@ public static function removeEmptyDirectory(string $path): bool
/**
* 扫描目录列表
* @param string $path 扫描目录
* @param ?integer $depth 扫描深度
* @param string $filterExt 筛选后缀
* @param boolean $shortPath 相对路径
* @param ?integer $depth 当前递归深度,null 表示无限制深度
* @return array
*/
public static function scanDirectory(string $path, string $filterExt = '', bool $shortPath = true, ?int $depth = null): array
public static function scanDirectory(string $path, ?int $depth = null, string $filterExt = '', bool $shortPath = true): array
{
return static::findFilesArray($path, static function (SplFileInfo $info) use ($filterExt) {
return static::findFilesArray($path, $depth, static function (SplFileInfo $info) use ($filterExt) {
return !$filterExt || $info->getExtension() === $filterExt;
}, static function (SplFileInfo $info) {
return $info->getBasename()[0] !== '.';
}, $shortPath, $depth);
}, $shortPath);
}

/**
* 扫描指定目录并返回文件路径数组
* @param string $path 要扫描的目录路径
* @param ?Closure $filterFile 用于过滤文件的闭包
* @param ?Closure $filterPath 用于过滤目录的闭包
* @param string $path 扫描目录
* @param ?integer $depth 扫描深度
* @param ?Closure $filter 文件过滤,返回 false 表示放弃
* @param boolean $short 是否返回相对于给定路径的短路径
* @param ?integer $depth 当前递归深度,null 表示无限制深度
* @return array 包含文件路径的数组
*/
public static function findFilesArray(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, bool $short = true, ?int $depth = null): array
public static function findFilesArray(string $path, ?int $depth = null, ?Closure $filter = null, bool $short = true): array
{
[$info, $files] = [new SplFileInfo($path), []];
if ($info->isFile()) {
if ($filterFile === null || $filterFile($info)) {
if ($info->isDir() || $info->isFile()) {
if ($info->isFile() && ($filter === null || $filter($info) !== false)) {
$files[] = $short ? $info->getBasename() : $info->getPathname();
}
} elseif ($info->isDir()) {
foreach (static::findFilesYield($info->getRealPath(), $filterFile, $filterPath, $depth) as $file) {
if ($info->isDir()) foreach (static::findFilesYield($info->getRealPath(), $depth, $filter) as $file) {
$files[] = $short ? substr($file->getRealPath(), strlen($info->getRealPath()) + 1) : $file->getRealPath();
}
}
Expand All @@ -122,24 +117,23 @@ public static function findFilesArray(string $path, ?Closure $filterFile = null,
/**
* 递归扫描指定目录,返回文件或目录的 SplFileInfo 对象。
* @param string $path 目录路径。
* @param \Closure|null $filterFile 文件过滤器闭包,返回 true 表示文件被接受。
* @param \Closure|null $filterPath 目录过滤器闭包,返回 true 表示目录被接受。
* @param ?int $depth 当前递归深度限制,null 表示无限制深度。
* @param ?integer $depth 扫描深度
* @param \Closure|null $filter 文件过滤,返回 false 表示放弃
* @param boolean $appendPath 是否包含目录本身在结果中。
* @param integer $currentDepth 当前递归深度,初始值为 0。
* @param integer $currDepth 当前递归深度,初始值为 0。
* @return \Generator 返回 SplFileInfo 对象的生成器。
*/
private static function findFilesYield(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, ?int $depth = null, bool $appendPath = false, int $currentDepth = 1): Generator
private static function findFilesYield(string $path, ?int $depth = null, ?Closure $filter = null, bool $appendPath = false, int $currDepth = 1): Generator
{
if (file_exists($path) && is_dir($path) && !is_numeric($depth) || $currentDepth <= $depth) {
if (file_exists($path) && is_dir($path) && (!is_numeric($depth) || $currDepth <= $depth)) {
foreach (new FilesystemIterator($path, FilesystemIterator::SKIP_DOTS) as $item) {
if ($item->isDir() && !$item->isLink()) {
if ($filterPath === null || $filterPath($item)) {
if ($filter === null || $filter($item) !== false) {
if ($item->isDir() && !$item->isLink()) {
$appendPath && yield $item;
yield from static::findFilesYield($item->getPathname(), $filterFile, $filterPath, $depth, $appendPath, $currentDepth + 1);
yield from static::findFilesYield($item->getPathname(), $depth, $filter, $appendPath, $currDepth + 1);
} else {
yield $item;
}
} elseif ($filterFile === null || $filterFile($item)) {
yield $item;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/support/middleware/MultAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ private function loadMultiApp(string $appPath): bool
// 加载应用函数文件
if (is_file($file = "{$appPath}common{$ext}")) include_once $file;
// 加载应用配置文件
ToolsExtend::findFilesArray($appPath . 'config', function (SplFileInfo $info) use ($ext) {
if (strtolower(".{$info->getExtension()}") === $ext) {
ToolsExtend::findFilesArray($appPath . 'config', 1, function (SplFileInfo $info) use ($ext) {
if ($info->isFile() && 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 4163868

Please sign in to comment.