Skip to content

Commit 4163868

Browse files
committed
fix: 优化 ToolsExtend::findFilesArray 操作,简化参数并调整参数顺序
1 parent 52d3cb3 commit 4163868

File tree

4 files changed

+39
-43
lines changed

4 files changed

+39
-43
lines changed

src/Library.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ public function register()
108108
{
109109
// 动态加载全局配置
110110
[$dir, $ext] = [$this->app->getBasePath(), $this->app->getConfigExt()];
111-
ToolsExtend::findFilesArray($dir, function (SplFileInfo $info) use ($ext) {
112-
$info->getBasename() === "sys{$ext}" && include_once $info->getPathname();
113-
}, null, true, 2);
111+
ToolsExtend::findFilesArray($dir, 2, function (SplFileInfo $info) use ($ext) {
112+
$info->isFile() && $info->getBasename() === "sys{$ext}" && include_once $info->getPathname();
113+
});
114114
if (is_file($file = "{$dir}common{$ext}")) include_once $file;
115115
if (is_file($file = "{$dir}provider{$ext}")) $this->app->bind(include $file);
116116
if (is_file($file = "{$dir}event{$ext}")) $this->app->loadEvent(include $file);

src/extend/PhinxExtend.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,15 +338,17 @@ private function _create_{$table}()
338338
private static function nextFile(string $class): string
339339
{
340340
[$snake, $items] = [Str::snake($class), [20010000000000]];
341-
ToolsExtend::findFilesArray(syspath('database/migrations'), function (SplFileInfo $info) use ($snake, &$items) {
342-
$bname = pathinfo($info->getBasename(), PATHINFO_FILENAME);
343-
$items[] = $version = intval(substr($bname, 0, 14));
344-
if ($snake === substr($bname, 15) && unlink($info->getRealPath())) {
345-
if (is_dir($dataPath = $info->getPath() . DIRECTORY_SEPARATOR . $version)) {
346-
ToolsExtend::removeEmptyDirectory($dataPath);
341+
ToolsExtend::findFilesArray(syspath('database/migrations'), 1, function (SplFileInfo $info) use ($snake, &$items) {
342+
if ($info->isFile()) {
343+
$bname = pathinfo($info->getBasename(), PATHINFO_FILENAME);
344+
$items[] = $version = intval(substr($bname, 0, 14));
345+
if ($snake === substr($bname, 15) && unlink($info->getRealPath())) {
346+
if (is_dir($dataPath = $info->getPath() . DIRECTORY_SEPARATOR . $version)) {
347+
ToolsExtend::removeEmptyDirectory($dataPath);
348+
}
347349
}
348350
}
349-
}, null, true, 1);
351+
});
350352

351353
// 计算下一个版本号
352354
return sprintf("%s_{$snake}.php", min($items) - 1);

src/extend/ToolsExtend.php

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ public static function copyfile(string $frdir, string $todir, array $files = [],
4545
$todir = rtrim($todir, '\\/') . DIRECTORY_SEPARATOR;
4646
// 扫描目录文件
4747
if (empty($files) && is_dir($frdir)) {
48-
$filter = function (SplFileInfo $info) {
48+
$files = static::findFilesArray($frdir, null, function (SplFileInfo $info) {
4949
return $info->getBasename()[0] !== '.';
50-
};
51-
$files = static::findFilesArray($frdir, $filter, $filter);
50+
});
5251
}
5352
// 复制文件列表
5453
foreach ($files as $target) {
@@ -72,7 +71,7 @@ public static function copyfile(string $frdir, string $todir, array $files = [],
7271
*/
7372
public static function removeEmptyDirectory(string $path): bool
7473
{
75-
foreach (self::findFilesYield($path, null, null, null, true) as $item) {
74+
foreach (self::findFilesYield($path, null, null, true) as $item) {
7675
($item->isFile() || $item->isLink()) ? unlink($item->getRealPath()) : rmdir($item->getRealPath());
7776
}
7877
return is_file($path) ? unlink($path) : (!is_dir($path) || rmdir($path));
@@ -81,38 +80,34 @@ public static function removeEmptyDirectory(string $path): bool
8180
/**
8281
* 扫描目录列表
8382
* @param string $path 扫描目录
83+
* @param ?integer $depth 扫描深度
8484
* @param string $filterExt 筛选后缀
8585
* @param boolean $shortPath 相对路径
86-
* @param ?integer $depth 当前递归深度,null 表示无限制深度
8786
* @return array
8887
*/
89-
public static function scanDirectory(string $path, string $filterExt = '', bool $shortPath = true, ?int $depth = null): array
88+
public static function scanDirectory(string $path, ?int $depth = null, string $filterExt = '', bool $shortPath = true): array
9089
{
91-
return static::findFilesArray($path, static function (SplFileInfo $info) use ($filterExt) {
90+
return static::findFilesArray($path, $depth, static function (SplFileInfo $info) use ($filterExt) {
9291
return !$filterExt || $info->getExtension() === $filterExt;
93-
}, static function (SplFileInfo $info) {
94-
return $info->getBasename()[0] !== '.';
95-
}, $shortPath, $depth);
92+
}, $shortPath);
9693
}
9794

9895
/**
9996
* 扫描指定目录并返回文件路径数组
100-
* @param string $path 要扫描的目录路径
101-
* @param ?Closure $filterFile 用于过滤文件的闭包
102-
* @param ?Closure $filterPath 用于过滤目录的闭包
97+
* @param string $path 扫描目录
98+
* @param ?integer $depth 扫描深度
99+
* @param ?Closure $filter 文件过滤,返回 false 表示放弃
103100
* @param boolean $short 是否返回相对于给定路径的短路径
104-
* @param ?integer $depth 当前递归深度,null 表示无限制深度
105101
* @return array 包含文件路径的数组
106102
*/
107-
public static function findFilesArray(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, bool $short = true, ?int $depth = null): array
103+
public static function findFilesArray(string $path, ?int $depth = null, ?Closure $filter = null, bool $short = true): array
108104
{
109105
[$info, $files] = [new SplFileInfo($path), []];
110-
if ($info->isFile()) {
111-
if ($filterFile === null || $filterFile($info)) {
106+
if ($info->isDir() || $info->isFile()) {
107+
if ($info->isFile() && ($filter === null || $filter($info) !== false)) {
112108
$files[] = $short ? $info->getBasename() : $info->getPathname();
113109
}
114-
} elseif ($info->isDir()) {
115-
foreach (static::findFilesYield($info->getRealPath(), $filterFile, $filterPath, $depth) as $file) {
110+
if ($info->isDir()) foreach (static::findFilesYield($info->getRealPath(), $depth, $filter) as $file) {
116111
$files[] = $short ? substr($file->getRealPath(), strlen($info->getRealPath()) + 1) : $file->getRealPath();
117112
}
118113
}
@@ -122,24 +117,23 @@ public static function findFilesArray(string $path, ?Closure $filterFile = null,
122117
/**
123118
* 递归扫描指定目录,返回文件或目录的 SplFileInfo 对象。
124119
* @param string $path 目录路径。
125-
* @param \Closure|null $filterFile 文件过滤器闭包,返回 true 表示文件被接受。
126-
* @param \Closure|null $filterPath 目录过滤器闭包,返回 true 表示目录被接受。
127-
* @param ?int $depth 当前递归深度限制,null 表示无限制深度。
120+
* @param ?integer $depth 扫描深度
121+
* @param \Closure|null $filter 文件过滤,返回 false 表示放弃
128122
* @param boolean $appendPath 是否包含目录本身在结果中。
129-
* @param integer $currentDepth 当前递归深度,初始值为 0。
123+
* @param integer $currDepth 当前递归深度,初始值为 0。
130124
* @return \Generator 返回 SplFileInfo 对象的生成器。
131125
*/
132-
private static function findFilesYield(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, ?int $depth = null, bool $appendPath = false, int $currentDepth = 1): Generator
126+
private static function findFilesYield(string $path, ?int $depth = null, ?Closure $filter = null, bool $appendPath = false, int $currDepth = 1): Generator
133127
{
134-
if (file_exists($path) && is_dir($path) && !is_numeric($depth) || $currentDepth <= $depth) {
128+
if (file_exists($path) && is_dir($path) && (!is_numeric($depth) || $currDepth <= $depth)) {
135129
foreach (new FilesystemIterator($path, FilesystemIterator::SKIP_DOTS) as $item) {
136-
if ($item->isDir() && !$item->isLink()) {
137-
if ($filterPath === null || $filterPath($item)) {
130+
if ($filter === null || $filter($item) !== false) {
131+
if ($item->isDir() && !$item->isLink()) {
138132
$appendPath && yield $item;
139-
yield from static::findFilesYield($item->getPathname(), $filterFile, $filterPath, $depth, $appendPath, $currentDepth + 1);
133+
yield from static::findFilesYield($item->getPathname(), $depth, $filter, $appendPath, $currDepth + 1);
134+
} else {
135+
yield $item;
140136
}
141-
} elseif ($filterFile === null || $filterFile($item)) {
142-
yield $item;
143137
}
144138
}
145139
}

src/support/middleware/MultAccess.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ private function loadMultiApp(string $appPath): bool
169169
// 加载应用函数文件
170170
if (is_file($file = "{$appPath}common{$ext}")) include_once $file;
171171
// 加载应用配置文件
172-
ToolsExtend::findFilesArray($appPath . 'config', function (SplFileInfo $info) use ($ext) {
173-
if (strtolower(".{$info->getExtension()}") === $ext) {
172+
ToolsExtend::findFilesArray($appPath . 'config', 1, function (SplFileInfo $info) use ($ext) {
173+
if ($info->isFile() && strtolower(".{$info->getExtension()}") === $ext) {
174174
$this->app->config->load($info->getPathname(), $info->getBasename($ext));
175175
}
176-
}, null, true, 1);
176+
});
177177
// 加载应用路由配置
178178
if (in_array('route', $fmaps) && method_exists($this->app->route, 'reload')) {
179179
$this->app->route->reload();

0 commit comments

Comments
 (0)