@@ -45,10 +45,9 @@ public static function copyfile(string $frdir, string $todir, array $files = [],
45
45
$ todir = rtrim ($ todir , '\\/ ' ) . DIRECTORY_SEPARATOR ;
46
46
// 扫描目录文件
47
47
if (empty ($ files ) && is_dir ($ frdir )) {
48
- $ filter = function (SplFileInfo $ info ) {
48
+ $ files = static :: findFilesArray ( $ frdir , null , function (SplFileInfo $ info ) {
49
49
return $ info ->getBasename ()[0 ] !== '. ' ;
50
- };
51
- $ files = static ::findFilesArray ($ frdir , $ filter , $ filter );
50
+ });
52
51
}
53
52
// 复制文件列表
54
53
foreach ($ files as $ target ) {
@@ -72,7 +71,7 @@ public static function copyfile(string $frdir, string $todir, array $files = [],
72
71
*/
73
72
public static function removeEmptyDirectory (string $ path ): bool
74
73
{
75
- foreach (self ::findFilesYield ($ path , null , null , null , true ) as $ item ) {
74
+ foreach (self ::findFilesYield ($ path , null , null , true ) as $ item ) {
76
75
($ item ->isFile () || $ item ->isLink ()) ? unlink ($ item ->getRealPath ()) : rmdir ($ item ->getRealPath ());
77
76
}
78
77
return is_file ($ path ) ? unlink ($ path ) : (!is_dir ($ path ) || rmdir ($ path ));
@@ -81,38 +80,34 @@ public static function removeEmptyDirectory(string $path): bool
81
80
/**
82
81
* 扫描目录列表
83
82
* @param string $path 扫描目录
83
+ * @param ?integer $depth 扫描深度
84
84
* @param string $filterExt 筛选后缀
85
85
* @param boolean $shortPath 相对路径
86
- * @param ?integer $depth 当前递归深度,null 表示无限制深度
87
86
* @return array
88
87
*/
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
90
89
{
91
- return static ::findFilesArray ($ path , static function (SplFileInfo $ info ) use ($ filterExt ) {
90
+ return static ::findFilesArray ($ path , $ depth , static function (SplFileInfo $ info ) use ($ filterExt ) {
92
91
return !$ filterExt || $ info ->getExtension () === $ filterExt ;
93
- }, static function (SplFileInfo $ info ) {
94
- return $ info ->getBasename ()[0 ] !== '. ' ;
95
- }, $ shortPath , $ depth );
92
+ }, $ shortPath );
96
93
}
97
94
98
95
/**
99
96
* 扫描指定目录并返回文件路径数组
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 表示放弃
103
100
* @param boolean $short 是否返回相对于给定路径的短路径
104
- * @param ?integer $depth 当前递归深度,null 表示无限制深度
105
101
* @return array 包含文件路径的数组
106
102
*/
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
108
104
{
109
105
[$ 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 )) {
112
108
$ files [] = $ short ? $ info ->getBasename () : $ info ->getPathname ();
113
109
}
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 ) {
116
111
$ files [] = $ short ? substr ($ file ->getRealPath (), strlen ($ info ->getRealPath ()) + 1 ) : $ file ->getRealPath ();
117
112
}
118
113
}
@@ -122,24 +117,23 @@ public static function findFilesArray(string $path, ?Closure $filterFile = null,
122
117
/**
123
118
* 递归扫描指定目录,返回文件或目录的 SplFileInfo 对象。
124
119
* @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 表示放弃
128
122
* @param boolean $appendPath 是否包含目录本身在结果中。
129
- * @param integer $currentDepth 当前递归深度,初始值为 0。
123
+ * @param integer $currDepth 当前递归深度,初始值为 0。
130
124
* @return \Generator 返回 SplFileInfo 对象的生成器。
131
125
*/
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
133
127
{
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) ) {
135
129
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 ( )) {
138
132
$ 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 ;
140
136
}
141
- } elseif ($ filterFile === null || $ filterFile ($ item )) {
142
- yield $ item ;
143
137
}
144
138
}
145
139
}
0 commit comments