16
16
use RecursiveIteratorIterator ;
17
17
use Toolkit \FsUtil \Exception \FileNotFoundException ;
18
18
use Toolkit \FsUtil \Exception \FileSystemException ;
19
+ use Toolkit \FsUtil \Traits \DirOperateTrait ;
19
20
use function basename ;
20
21
use function glob ;
21
22
use function implode ;
33
34
*/
34
35
class Directory extends FileSystem
35
36
{
36
- /**
37
- * ```php
38
- * $filter = function ($current, $key, $iterator) {
39
- * // \SplFileInfo $current
40
- * // Skip hidden files and directories.
41
- * if ($current->getFilename()[0] === '.') {
42
- * return false;
43
- * }
44
- * if ($current->isDir()) {
45
- * // Only recurse into intended subdirectories.
46
- * return $current->getFilename() !== '.git';
47
- * }
48
- * // Only consume files of interest.
49
- * return strpos($current->getFilename(), '.php') !== false;
50
- * };
51
- *
52
- * // $info is instance of \SplFileInfo
53
- * foreach(Directory::getRecursiveIterator($srcDir, $filter) as $info) {
54
- * // $info->getFilename(); ...
55
- * }
56
- * ```
57
- *
58
- * @param string $srcDir
59
- * @param callable $filter
60
- *
61
- * @return RecursiveIteratorIterator
62
- * @throws LogicException
63
- */
64
- public static function getRecursiveIterator (string $ srcDir , callable $ filter ): RecursiveIteratorIterator
65
- {
66
- return self ::getIterator ($ srcDir , $ filter );
67
- }
68
-
69
- /**
70
- * 判断文件夹是否为空
71
- *
72
- * @param string $dir
73
- *
74
- * @return bool
75
- * @throws FileSystemException
76
- */
77
- public static function isEmpty (string $ dir ): bool
78
- {
79
- $ handler = opendir ($ dir );
80
-
81
- if (false === $ handler ) {
82
- throw new FileSystemException ("Open the dir failure! DIR: $ dir " );
83
- }
84
-
85
- while (($ file = readdir ($ handler )) !== false ) {
86
- if ($ file !== '. ' && $ file !== '.. ' ) {
87
- closedir ($ handler );
88
-
89
- return false ;
90
- }
91
- }
92
-
93
- closedir ($ handler );
94
-
95
- return true ;
96
- }
97
-
98
- /**
99
- * 查看一个目录中的所有文件和子目录
100
- *
101
- * @param string $path
102
- *
103
- * @return array
104
- * @throws FileNotFoundException
105
- */
106
- public static function ls (string $ path ): array
107
- {
108
- $ list = [];
109
-
110
- try {
111
- /*** class create new DirectoryIterator Object ***/
112
- foreach (new DirectoryIterator ($ path ) as $ item ) {
113
- $ list [] = $ item ;
114
- }
115
- /*** if an exception is thrown, catch it here ***/
116
- } catch (Exception $ e ) {
117
- throw new FileNotFoundException ($ path . ' 没有任何内容 ' );
118
- }
119
-
120
- return $ list ;
121
- }
37
+ use DirOperateTrait;
122
38
123
39
/**
124
40
* 只获得目录结构
@@ -161,13 +77,12 @@ public static function getList(string $path, int $pid = 0, bool $son = false, ar
161
77
}
162
78
163
79
/**
164
- * @param $path
165
- * @param bool $loop
166
- * @param null $parent
167
- * @param array $list
80
+ * @param string $path
81
+ * @param bool $loop
82
+ * @param null $parent
83
+ * @param array $list
168
84
*
169
85
* @return array
170
- * @throws FileNotFoundException
171
86
*/
172
87
public static function getDirs (string $ path , bool $ loop = false , $ parent = null , array $ list = []): array
173
88
{
@@ -216,7 +131,6 @@ public static function simpleInfo(string $dir, $ext = null, bool $recursive = fa
216
131
217
132
// glob()寻找与模式匹配的文件路径 $file is pull path
218
133
foreach (glob ($ dir . '* ' ) as $ file ) {
219
-
220
134
// 匹配文件 如果没有传入$ext 则全部遍历,传入了则按传入的类型来查找
221
135
if (is_file ($ file ) && (!$ ext || preg_match ("/\.( $ ext)$/i " , $ file ))) {
222
136
//basename — 返回路径中的 文件名部分
@@ -242,7 +156,7 @@ public static function simpleInfo(string $dir, $ext = null, bool $recursive = fa
242
156
* @param string $path string 目标目录
243
157
* @param array|string $ext array('css','html','php') css|html|php
244
158
* @param bool $recursive 是否包含子目录
245
- * @param null| string $parent
159
+ * @param string $parent
246
160
* @param array $list
247
161
*
248
162
* @return array
@@ -252,7 +166,7 @@ public static function getFiles(
252
166
string $ path ,
253
167
$ ext = null ,
254
168
bool $ recursive = false ,
255
- $ parent = null ,
169
+ string $ parent = '' ,
256
170
array $ list = []
257
171
): array {
258
172
$ path = self ::pathFormat ($ path );
@@ -293,7 +207,6 @@ public static function getFiles(
293
207
public static function getFilesInfo (string $ path , $ ext = null , bool $ recursive = false , array &$ list = []): array
294
208
{
295
209
$ path = self ::pathFormat ($ path );
296
-
297
210
if (!is_dir ($ path )) {
298
211
throw new FileNotFoundException ("directory not exists! DIR: $ path " );
299
212
}
@@ -302,7 +215,7 @@ public static function getFilesInfo(string $path, $ext = null, bool $recursive =
302
215
303
216
static $ id = 0 ;
304
217
305
- //glob()寻找与模式匹配的文件路径
218
+ // glob()寻找与模式匹配的文件路径
306
219
foreach (glob ($ path . '* ' ) as $ file ) {
307
220
$ id ++;
308
221
@@ -336,11 +249,10 @@ public static function create(string $path, int $mode = 0775, bool $recursive =
336
249
/**
337
250
* 复制目录内容
338
251
*
339
- * @param $oldDir
340
- * @param $newDir
252
+ * @param string $oldDir
253
+ * @param string $newDir
341
254
*
342
255
* @return bool
343
- * @throws FileNotFoundException
344
256
*/
345
257
public static function copy (string $ oldDir , string $ newDir ): bool
346
258
{
@@ -393,7 +305,6 @@ public static function delete(string $path, bool $delSelf = true): bool
393
305
}
394
306
395
307
$ delSelf && rmdir ($ dirPath );//默认最后删掉自己
396
-
397
308
return true ;
398
309
}
399
310
}
0 commit comments