1616use RecursiveIteratorIterator ;
1717use Toolkit \FsUtil \Exception \FileNotFoundException ;
1818use Toolkit \FsUtil \Exception \FileSystemException ;
19+ use Toolkit \FsUtil \Traits \DirOperateTrait ;
1920use function basename ;
2021use function glob ;
2122use function implode ;
3334 */
3435class Directory extends FileSystem
3536{
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;
12238
12339 /**
12440 * 只获得目录结构
@@ -161,13 +77,12 @@ public static function getList(string $path, int $pid = 0, bool $son = false, ar
16177 }
16278
16379 /**
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
16884 *
16985 * @return array
170- * @throws FileNotFoundException
17186 */
17287 public static function getDirs (string $ path , bool $ loop = false , $ parent = null , array $ list = []): array
17388 {
@@ -216,7 +131,6 @@ public static function simpleInfo(string $dir, $ext = null, bool $recursive = fa
216131
217132 // glob()寻找与模式匹配的文件路径 $file is pull path
218133 foreach (glob ($ dir . '* ' ) as $ file ) {
219-
220134 // 匹配文件 如果没有传入$ext 则全部遍历,传入了则按传入的类型来查找
221135 if (is_file ($ file ) && (!$ ext || preg_match ("/\.( $ ext)$/i " , $ file ))) {
222136 //basename — 返回路径中的 文件名部分
@@ -242,7 +156,7 @@ public static function simpleInfo(string $dir, $ext = null, bool $recursive = fa
242156 * @param string $path string 目标目录
243157 * @param array|string $ext array('css','html','php') css|html|php
244158 * @param bool $recursive 是否包含子目录
245- * @param null| string $parent
159+ * @param string $parent
246160 * @param array $list
247161 *
248162 * @return array
@@ -252,7 +166,7 @@ public static function getFiles(
252166 string $ path ,
253167 $ ext = null ,
254168 bool $ recursive = false ,
255- $ parent = null ,
169+ string $ parent = '' ,
256170 array $ list = []
257171 ): array {
258172 $ path = self ::pathFormat ($ path );
@@ -293,7 +207,6 @@ public static function getFiles(
293207 public static function getFilesInfo (string $ path , $ ext = null , bool $ recursive = false , array &$ list = []): array
294208 {
295209 $ path = self ::pathFormat ($ path );
296-
297210 if (!is_dir ($ path )) {
298211 throw new FileNotFoundException ("directory not exists! DIR: $ path " );
299212 }
@@ -302,7 +215,7 @@ public static function getFilesInfo(string $path, $ext = null, bool $recursive =
302215
303216 static $ id = 0 ;
304217
305- //glob()寻找与模式匹配的文件路径
218+ // glob()寻找与模式匹配的文件路径
306219 foreach (glob ($ path . '* ' ) as $ file ) {
307220 $ id ++;
308221
@@ -336,11 +249,10 @@ public static function create(string $path, int $mode = 0775, bool $recursive =
336249 /**
337250 * 复制目录内容
338251 *
339- * @param $oldDir
340- * @param $newDir
252+ * @param string $oldDir
253+ * @param string $newDir
341254 *
342255 * @return bool
343- * @throws FileNotFoundException
344256 */
345257 public static function copy (string $ oldDir , string $ newDir ): bool
346258 {
@@ -393,7 +305,6 @@ public static function delete(string $path, bool $delSelf = true): bool
393305 }
394306
395307 $ delSelf && rmdir ($ dirPath );//默认最后删掉自己
396-
397308 return true ;
398309 }
399310}
0 commit comments