1010namespace Toolkit \File ;
1111
1212use DirectoryIterator ;
13+ use Exception ;
14+ use InvalidArgumentException ;
15+ use LogicException ;
16+ use RecursiveIteratorIterator ;
1317use Toolkit \File \Exception \FileNotFoundException ;
1418use Toolkit \File \Exception \FileSystemException ;
19+ use function basename ;
20+ use function glob ;
21+ use function implode ;
22+ use function is_array ;
23+ use function is_dir ;
24+ use function is_file ;
25+ use function preg_match ;
26+ use function strlen ;
27+ use function trim ;
1528
1629/**
1730 * Class Directory
31+ *
1832 * @package Toolkit\File
1933 */
2034class Directory extends FileSystem
@@ -40,19 +54,23 @@ class Directory extends FileSystem
4054 * // $info->getFilename(); ...
4155 * }
4256 * ```
57+ *
4358 * @param string $srcDir
4459 * @param callable $filter
45- * @return \RecursiveIteratorIterator
46- * @throws \LogicException
60+ *
61+ * @return RecursiveIteratorIterator
62+ * @throws LogicException
4763 */
48- public static function getRecursiveIterator ($ srcDir , callable $ filter ): \ RecursiveIteratorIterator
64+ public static function getRecursiveIterator ($ srcDir , callable $ filter ): RecursiveIteratorIterator
4965 {
5066 return self ::getIterator ($ srcDir , $ filter );
5167 }
5268
5369 /**
5470 * 判断文件夹是否为空
71+ *
5572 * @param $dir
73+ *
5674 * @return bool
5775 * @throws FileSystemException
5876 */
@@ -80,9 +98,11 @@ public static function isEmpty($dir): bool
8098
8199 /**
82100 * 查看一个目录中的所有文件和子目录
101+ *
83102 * @param $path
84- * @throws FileNotFoundException
103+ *
85104 * @return array
105+ * @throws FileNotFoundException
86106 */
87107 public static function ls ($ path ): array
88108 {
@@ -94,7 +114,7 @@ public static function ls($path): array
94114 $ list [] = $ item ;
95115 }
96116 /*** if an exception is thrown, catch it here ***/
97- } catch (\ Exception $ e ) {
117+ } catch (Exception $ e ) {
98118 throw new FileNotFoundException ($ path . ' 没有任何内容 ' );
99119 }
100120
@@ -103,10 +123,12 @@ public static function ls($path): array
103123
104124 /**
105125 * 只获得目录结构
126+ *
106127 * @param $path
107128 * @param int $pid
108129 * @param int $son
109130 * @param array $list
131+ *
110132 * @return array
111133 * @throws FileNotFoundException
112134 */
@@ -124,8 +146,8 @@ public static function getList($path, $pid = 0, $son = 0, array $list = []): arr
124146 if (is_dir ($ v )) {
125147 $ id ++;
126148
127- $ list [$ id ]['id ' ] = $ id ;
128- $ list [$ id ]['pid ' ] = $ pid ;
149+ $ list [$ id ]['id ' ] = $ id ;
150+ $ list [$ id ]['pid ' ] = $ pid ;
129151 $ list [$ id ]['name ' ] = basename ($ v );
130152 $ list [$ id ]['path ' ] = realpath ($ v );
131153
@@ -144,6 +166,7 @@ public static function getList($path, $pid = 0, $son = 0, array $list = []): arr
144166 * @param bool $loop
145167 * @param null $parent
146168 * @param array $list
169+ *
147170 * @return array
148171 * @throws FileNotFoundException
149172 */
@@ -155,12 +178,12 @@ public static function getDirs($path, $loop = false, $parent = null, array $list
155178 throw new FileNotFoundException ("directory not exists! DIR: $ path " );
156179 }
157180
158- $ len = \ strlen ($ path );
181+ $ len = strlen ($ path );
159182
160183 foreach (glob ($ path . '* ' ) as $ v ) {
161184 if (is_dir ($ v )) {
162185 $ relatePath = substr ($ v , $ len );
163- $ list [] = $ parent . $ relatePath ;
186+ $ list [] = $ parent . $ relatePath ;
164187
165188 //是否遍历子目录
166189 if ($ loop ) {
@@ -174,33 +197,35 @@ public static function getDirs($path, $loop = false, $parent = null, array $list
174197
175198 /**
176199 * 获得目录下的文件,可选择类型、是否遍历子文件夹
177- * @param string $dir string 目标目录
178- * @param string|array $ext array('css','html','php') css|html|php
200+ *
201+ * @param string $dir string 目标目录
202+ * @param string|array $ext array('css','html','php') css|html|php
179203 * @param bool $recursive int|bool 是否包含子目录
204+ *
180205 * @return array
181206 * @throws FileNotFoundException
182207 */
183208 public static function simpleInfo (string $ dir , $ ext = null , $ recursive = false ): array
184209 {
185210 $ list = [];
186- $ dir = self ::pathFormat ($ dir );
187- $ ext = \ is_array ($ ext ) ? \ implode ('| ' , $ ext ) : \ trim ($ ext );
211+ $ dir = self ::pathFormat ($ dir );
212+ $ ext = is_array ($ ext ) ? implode ('| ' , $ ext ) : trim ($ ext );
188213
189- if (!\ is_dir ($ dir )) {
214+ if (!is_dir ($ dir )) {
190215 throw new FileNotFoundException ("directory not exists! DIR: $ dir " );
191216 }
192217
193218 // glob()寻找与模式匹配的文件路径 $file is pull path
194- foreach (\ glob ($ dir . '* ' ) as $ file ) {
219+ foreach (glob ($ dir . '* ' ) as $ file ) {
195220
196221 // 匹配文件 如果没有传入$ext 则全部遍历,传入了则按传入的类型来查找
197- if (\ is_file ($ file ) && (!$ ext || \ preg_match ("/\.( $ ext)$/i " , $ file ))) {
222+ if (is_file ($ file ) && (!$ ext || preg_match ("/\.( $ ext)$/i " , $ file ))) {
198223 //basename — 返回路径中的 文件名部分
199- $ list [] = \ basename ($ file );
224+ $ list [] = basename ($ file );
200225
201226 // is directory
202227 } else {
203- $ list [] = '/ ' . \ basename ($ file );
228+ $ list [] = '/ ' . basename ($ file );
204229
205230 if ($ recursive ) {
206231 $ list = array_merge ($ list , self ::simpleInfo ($ file , $ ext , $ recursive ));
@@ -213,30 +238,37 @@ public static function simpleInfo(string $dir, $ext = null, $recursive = false):
213238
214239 /**
215240 * 获得目录下的文件,可选择类型、是否遍历子文件夹
216- * @param string $path string 目标目录
217- * @param array|string $ext array('css','html','php') css|html|php
241+ *
242+ * @param string $path string 目标目录
243+ * @param array|string $ext array('css','html','php') css|html|php
218244 * @param bool $recursive 是否包含子目录
219245 * @param null|string $parent
220246 * @param array $list
247+ *
221248 * @return array
222249 * @throws FileNotFoundException
223250 */
224- public static function getFiles (string $ path , $ ext = null , $ recursive = false , $ parent = null , array $ list = []): array
225- {
251+ public static function getFiles (
252+ string $ path ,
253+ $ ext = null ,
254+ $ recursive = false ,
255+ $ parent = null ,
256+ array $ list = []
257+ ): array {
226258 $ path = self ::pathFormat ($ path );
227259
228260 if (!is_dir ($ path )) {
229261 throw new FileNotFoundException ("directory not exists! DIR: $ path " );
230262 }
231263
232- $ len = \ strlen ($ path );
233- $ ext = \ is_array ($ ext ) ? \ implode ('| ' , $ ext ) : \ trim ($ ext );
264+ $ len = strlen ($ path );
265+ $ ext = is_array ($ ext ) ? implode ('| ' , $ ext ) : trim ($ ext );
234266
235267 foreach (glob ($ path . '* ' ) as $ v ) {
236268 $ relatePath = substr ($ v , $ len );
237269
238270 // 匹配文件 如果没有传入$ext 则全部遍历,传入了则按传入的类型来查找
239- if (\ is_file ($ v ) && (!$ ext || \ preg_match ("/\.( $ ext)$/i " , $ v ))) {
271+ if (is_file ($ v ) && (!$ ext || preg_match ("/\.( $ ext)$/i " , $ v ))) {
240272 $ list [] = $ parent . $ relatePath ;
241273
242274 } elseif ($ recursive ) {
@@ -249,12 +281,14 @@ public static function getFiles(string $path, $ext = null, $recursive = false, $
249281
250282 /**
251283 * 获得目录下的文件以及详细信息,可选择类型、是否遍历子文件夹
252- * @param $path string 目标目录
253- * @param array|string $ext array('css','html','php') css|html|php
284+ *
285+ * @param $path string 目标目录
286+ * @param array|string $ext array('css','html','php') css|html|php
254287 * @param $recursive int|bool 是否包含子目录
255288 * @param array $list
289+ *
256290 * @return array
257- * @throws \ InvalidArgumentException
291+ * @throws InvalidArgumentException
258292 * @throws FileNotFoundException
259293 */
260294 public static function getFilesInfo ($ path , $ ext = null , $ recursive = 0 , &$ list = []): array
@@ -265,7 +299,7 @@ public static function getFilesInfo($path, $ext = null, $recursive = 0, &$list =
265299 throw new FileNotFoundException ("directory not exists! DIR: $ path " );
266300 }
267301
268- $ ext = \ is_array ($ ext ) ? implode ('| ' , $ ext ) : trim ($ ext );
302+ $ ext = is_array ($ ext ) ? implode ('| ' , $ ext ) : trim ($ ext );
269303
270304 static $ id = 0 ;
271305
@@ -288,9 +322,11 @@ public static function getFilesInfo($path, $ext = null, $recursive = 0, &$list =
288322
289323 /**
290324 * 支持层级目录的创建
325+ *
291326 * @param $path
292327 * @param int|string $mode
293328 * @param bool $recursive
329+ *
294330 * @return bool
295331 */
296332 public static function create ($ path , $ mode = 0775 , $ recursive = true ): bool
@@ -300,8 +336,10 @@ public static function create($path, $mode = 0775, $recursive = true): bool
300336
301337 /**
302338 * 复制目录内容
339+ *
303340 * @param $oldDir
304341 * @param $newDir
342+ *
305343 * @return bool
306344 * @throws FileNotFoundException
307345 */
@@ -337,8 +375,10 @@ public static function copy($oldDir, $newDir): bool
337375
338376 /**
339377 * 删除目录及里面的文件
378+ *
340379 * @param $path
341- * @param boolean $delSelf 默认最后删掉自己
380+ * @param boolean $delSelf 默认最后删掉自己
381+ *
342382 * @return bool
343383 */
344384 public static function delete ($ path , $ delSelf = true ): bool
0 commit comments