10
10
namespace Toolkit \File ;
11
11
12
12
use DirectoryIterator ;
13
+ use Exception ;
14
+ use InvalidArgumentException ;
15
+ use LogicException ;
16
+ use RecursiveIteratorIterator ;
13
17
use Toolkit \File \Exception \FileNotFoundException ;
14
18
use 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 ;
15
28
16
29
/**
17
30
* Class Directory
31
+ *
18
32
* @package Toolkit\File
19
33
*/
20
34
class Directory extends FileSystem
@@ -40,19 +54,23 @@ class Directory extends FileSystem
40
54
* // $info->getFilename(); ...
41
55
* }
42
56
* ```
57
+ *
43
58
* @param string $srcDir
44
59
* @param callable $filter
45
- * @return \RecursiveIteratorIterator
46
- * @throws \LogicException
60
+ *
61
+ * @return RecursiveIteratorIterator
62
+ * @throws LogicException
47
63
*/
48
- public static function getRecursiveIterator ($ srcDir , callable $ filter ): \ RecursiveIteratorIterator
64
+ public static function getRecursiveIterator ($ srcDir , callable $ filter ): RecursiveIteratorIterator
49
65
{
50
66
return self ::getIterator ($ srcDir , $ filter );
51
67
}
52
68
53
69
/**
54
70
* 判断文件夹是否为空
71
+ *
55
72
* @param $dir
73
+ *
56
74
* @return bool
57
75
* @throws FileSystemException
58
76
*/
@@ -80,9 +98,11 @@ public static function isEmpty($dir): bool
80
98
81
99
/**
82
100
* 查看一个目录中的所有文件和子目录
101
+ *
83
102
* @param $path
84
- * @throws FileNotFoundException
103
+ *
85
104
* @return array
105
+ * @throws FileNotFoundException
86
106
*/
87
107
public static function ls ($ path ): array
88
108
{
@@ -94,7 +114,7 @@ public static function ls($path): array
94
114
$ list [] = $ item ;
95
115
}
96
116
/*** if an exception is thrown, catch it here ***/
97
- } catch (\ Exception $ e ) {
117
+ } catch (Exception $ e ) {
98
118
throw new FileNotFoundException ($ path . ' 没有任何内容 ' );
99
119
}
100
120
@@ -103,10 +123,12 @@ public static function ls($path): array
103
123
104
124
/**
105
125
* 只获得目录结构
126
+ *
106
127
* @param $path
107
128
* @param int $pid
108
129
* @param int $son
109
130
* @param array $list
131
+ *
110
132
* @return array
111
133
* @throws FileNotFoundException
112
134
*/
@@ -124,8 +146,8 @@ public static function getList($path, $pid = 0, $son = 0, array $list = []): arr
124
146
if (is_dir ($ v )) {
125
147
$ id ++;
126
148
127
- $ list [$ id ]['id ' ] = $ id ;
128
- $ list [$ id ]['pid ' ] = $ pid ;
149
+ $ list [$ id ]['id ' ] = $ id ;
150
+ $ list [$ id ]['pid ' ] = $ pid ;
129
151
$ list [$ id ]['name ' ] = basename ($ v );
130
152
$ list [$ id ]['path ' ] = realpath ($ v );
131
153
@@ -144,6 +166,7 @@ public static function getList($path, $pid = 0, $son = 0, array $list = []): arr
144
166
* @param bool $loop
145
167
* @param null $parent
146
168
* @param array $list
169
+ *
147
170
* @return array
148
171
* @throws FileNotFoundException
149
172
*/
@@ -155,12 +178,12 @@ public static function getDirs($path, $loop = false, $parent = null, array $list
155
178
throw new FileNotFoundException ("directory not exists! DIR: $ path " );
156
179
}
157
180
158
- $ len = \ strlen ($ path );
181
+ $ len = strlen ($ path );
159
182
160
183
foreach (glob ($ path . '* ' ) as $ v ) {
161
184
if (is_dir ($ v )) {
162
185
$ relatePath = substr ($ v , $ len );
163
- $ list [] = $ parent . $ relatePath ;
186
+ $ list [] = $ parent . $ relatePath ;
164
187
165
188
//是否遍历子目录
166
189
if ($ loop ) {
@@ -174,33 +197,35 @@ public static function getDirs($path, $loop = false, $parent = null, array $list
174
197
175
198
/**
176
199
* 获得目录下的文件,可选择类型、是否遍历子文件夹
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
179
203
* @param bool $recursive int|bool 是否包含子目录
204
+ *
180
205
* @return array
181
206
* @throws FileNotFoundException
182
207
*/
183
208
public static function simpleInfo (string $ dir , $ ext = null , $ recursive = false ): array
184
209
{
185
210
$ 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 );
188
213
189
- if (!\ is_dir ($ dir )) {
214
+ if (!is_dir ($ dir )) {
190
215
throw new FileNotFoundException ("directory not exists! DIR: $ dir " );
191
216
}
192
217
193
218
// glob()寻找与模式匹配的文件路径 $file is pull path
194
- foreach (\ glob ($ dir . '* ' ) as $ file ) {
219
+ foreach (glob ($ dir . '* ' ) as $ file ) {
195
220
196
221
// 匹配文件 如果没有传入$ext 则全部遍历,传入了则按传入的类型来查找
197
- if (\ is_file ($ file ) && (!$ ext || \ preg_match ("/\.( $ ext)$/i " , $ file ))) {
222
+ if (is_file ($ file ) && (!$ ext || preg_match ("/\.( $ ext)$/i " , $ file ))) {
198
223
//basename — 返回路径中的 文件名部分
199
- $ list [] = \ basename ($ file );
224
+ $ list [] = basename ($ file );
200
225
201
226
// is directory
202
227
} else {
203
- $ list [] = '/ ' . \ basename ($ file );
228
+ $ list [] = '/ ' . basename ($ file );
204
229
205
230
if ($ recursive ) {
206
231
$ list = array_merge ($ list , self ::simpleInfo ($ file , $ ext , $ recursive ));
@@ -213,30 +238,37 @@ public static function simpleInfo(string $dir, $ext = null, $recursive = false):
213
238
214
239
/**
215
240
* 获得目录下的文件,可选择类型、是否遍历子文件夹
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
218
244
* @param bool $recursive 是否包含子目录
219
245
* @param null|string $parent
220
246
* @param array $list
247
+ *
221
248
* @return array
222
249
* @throws FileNotFoundException
223
250
*/
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 {
226
258
$ path = self ::pathFormat ($ path );
227
259
228
260
if (!is_dir ($ path )) {
229
261
throw new FileNotFoundException ("directory not exists! DIR: $ path " );
230
262
}
231
263
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 );
234
266
235
267
foreach (glob ($ path . '* ' ) as $ v ) {
236
268
$ relatePath = substr ($ v , $ len );
237
269
238
270
// 匹配文件 如果没有传入$ext 则全部遍历,传入了则按传入的类型来查找
239
- if (\ is_file ($ v ) && (!$ ext || \ preg_match ("/\.( $ ext)$/i " , $ v ))) {
271
+ if (is_file ($ v ) && (!$ ext || preg_match ("/\.( $ ext)$/i " , $ v ))) {
240
272
$ list [] = $ parent . $ relatePath ;
241
273
242
274
} elseif ($ recursive ) {
@@ -249,12 +281,14 @@ public static function getFiles(string $path, $ext = null, $recursive = false, $
249
281
250
282
/**
251
283
* 获得目录下的文件以及详细信息,可选择类型、是否遍历子文件夹
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
254
287
* @param $recursive int|bool 是否包含子目录
255
288
* @param array $list
289
+ *
256
290
* @return array
257
- * @throws \ InvalidArgumentException
291
+ * @throws InvalidArgumentException
258
292
* @throws FileNotFoundException
259
293
*/
260
294
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 =
265
299
throw new FileNotFoundException ("directory not exists! DIR: $ path " );
266
300
}
267
301
268
- $ ext = \ is_array ($ ext ) ? implode ('| ' , $ ext ) : trim ($ ext );
302
+ $ ext = is_array ($ ext ) ? implode ('| ' , $ ext ) : trim ($ ext );
269
303
270
304
static $ id = 0 ;
271
305
@@ -288,9 +322,11 @@ public static function getFilesInfo($path, $ext = null, $recursive = 0, &$list =
288
322
289
323
/**
290
324
* 支持层级目录的创建
325
+ *
291
326
* @param $path
292
327
* @param int|string $mode
293
328
* @param bool $recursive
329
+ *
294
330
* @return bool
295
331
*/
296
332
public static function create ($ path , $ mode = 0775 , $ recursive = true ): bool
@@ -300,8 +336,10 @@ public static function create($path, $mode = 0775, $recursive = true): bool
300
336
301
337
/**
302
338
* 复制目录内容
339
+ *
303
340
* @param $oldDir
304
341
* @param $newDir
342
+ *
305
343
* @return bool
306
344
* @throws FileNotFoundException
307
345
*/
@@ -337,8 +375,10 @@ public static function copy($oldDir, $newDir): bool
337
375
338
376
/**
339
377
* 删除目录及里面的文件
378
+ *
340
379
* @param $path
341
- * @param boolean $delSelf 默认最后删掉自己
380
+ * @param boolean $delSelf 默认最后删掉自己
381
+ *
342
382
* @return bool
343
383
*/
344
384
public static function delete ($ path , $ delSelf = true ): bool
0 commit comments