-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDirectory.php
374 lines (324 loc) · 10.7 KB
/
Directory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php declare(strict_types=1);
/**
* This file is part of toolkit/fsutil.
*
* @author https://github.com/inhere
* @link https://github.com/toolkit/fsutil
* @license MIT
*/
namespace Toolkit\FsUtil;
use InvalidArgumentException;
use Toolkit\FsUtil\Exception\FileNotFoundException;
use Toolkit\FsUtil\Traits\DirOperateTrait;
use function array_merge;
use function basename;
use function glob;
use function implode;
use function is_array;
use function is_dir;
use function is_file;
use function preg_match;
use function strlen;
use function trim;
use const GLOB_BRACE;
/**
* Class Directory
*
* @package Toolkit\FsUtil
*/
class Directory extends FileSystem
{
use DirOperateTrait;
/**
* 只获得目录结构
*
* @param string $path
* @param int $pid
* @param bool $son
* @param array $list
*
* @return array
* @throws FileNotFoundException
*/
public static function getList(string $path, int $pid = 0, bool $son = false, array $list = []): array
{
$path = self::pathFormat($path);
if (!is_dir($path)) {
throw new FileNotFoundException("directory not exists! DIR: $path");
}
static $id = 0;
foreach (glob($path . '*') as $v) {
if (is_dir($v)) {
$id++;
$list[$id]['id'] = $id;
$list[$id]['pid'] = $pid;
$list[$id]['name'] = basename($v);
$list[$id]['path'] = realpath($v);
//是否遍历子目录
if ($son) {
$list = self::getList($v, $id, $son, $list);
}
}
}
return $list;
}
/**
* @param string $path
* @param bool $loop
* @param null $parent
* @param array $list
*
* @return array
*/
public static function getDirs(string $path, bool $loop = false, $parent = null, array $list = []): array
{
$path = self::pathFormat($path);
if (!is_dir($path)) {
throw new FileNotFoundException("directory not exists! DIR: $path");
}
$len = strlen($path);
foreach (glob($path . '*') as $v) {
if (is_dir($v)) {
$relatePath = substr($v, $len);
$list[] = $parent . $relatePath;
//是否遍历子目录
if ($loop) {
$list = self::getDirs($v, $loop, $relatePath . '/', $list);
}
}
}
return $list;
}
/**
* 获得目录下的文件,可选择类型、是否遍历子文件夹
*
* @param string $dir string 目标目录
* @param array|string $ext array('css','html','php') css|html|php
* @param bool $recursive int|bool 是否包含子目录
*
* @return array
* @throws FileNotFoundException
*/
public static function simpleInfo(string $dir, array|string $ext = '', bool $recursive = false): array
{
$list = [];
$dir = self::pathFormat($dir);
$ext = is_array($ext) ? implode('|', $ext) : trim($ext);
if (!is_dir($dir)) {
throw new FileNotFoundException("directory not exists! DIR: $dir");
}
// glob()寻找与模式匹配的文件路径 $file is pull path
foreach (glob($dir . '*') as $file) {
// 匹配文件 如果没有传入$ext 则全部遍历,传入了则按传入的类型来查找
if (is_file($file) && (!$ext || preg_match("/\.($ext)$/i", $file))) {
//basename — 返回路径中的 文件名部分
$list[] = basename($file);
// is directory
} else {
$list[] = '/' . basename($file);
if ($recursive && is_dir($file)) {
/** @noinspection SlowArrayOperationsInLoopInspection */
$list = array_merge($list, self::simpleInfo($file, $ext, $recursive));
}
}
}
return $list;
}
/**
* 获得目录下的文件,可选择类型、是否遍历子文件夹
*
* @param string $path string 目标目录
* @param array|string $ext array('css','html','php') css|html|php
* @param bool $recursive 是否包含子目录
* @param string $parent
* @param array $list
*
* @return array
* @throws FileNotFoundException
*/
public static function getFiles(
string $path,
array|string $ext = '',
bool $recursive = false,
string $parent = '',
array $list = []
): array {
$path = self::pathFormat($path);
if (!is_dir($path)) {
throw new FileNotFoundException("directory not exists! DIR: $path");
}
$len = strlen($path);
$ext = is_array($ext) ? implode('|', $ext) : trim($ext);
foreach (glob($path . '*') as $v) {
$relatePath = substr($v, $len);
// 匹配文件 如果没有传入$ext 则全部遍历,传入了则按传入的类型来查找
if (is_file($v) && (!$ext || preg_match("/\.($ext)$/i", $v))) {
$list[] = $parent . $relatePath;
} elseif ($recursive && is_dir($v)) {
$list = self::getFiles($v, $ext, $recursive, $relatePath . '/', $list);
}
}
return $list;
}
/**
* 获得目录下的文件以及详细信息,可选择类型、是否遍历子文件夹
*
* @param string $path string 目标目录
* @param array|string $ext array('css','html','php') css|html|php
* @param bool $recursive 是否包含子目录
* @param array $list
*
* @return array
* @throws InvalidArgumentException
* @throws FileNotFoundException
*/
public static function getFilesInfo(string $path, array|string $ext = '', bool $recursive = false, array &$list = []): array
{
$path = self::pathFormat($path);
if (!is_dir($path)) {
throw new FileNotFoundException("directory not exists! DIR: $path");
}
static $id = 0;
$ext = is_array($ext) ? implode('|', $ext) : trim($ext);
// glob()寻找与模式匹配的文件路径
foreach (glob($path . '*') as $file) {
$id++;
// 匹配文件 如果没有传入$ext 则全部遍历,传入了则按传入的类型来查找
if (is_file($file) && (!$ext || preg_match("/\.($ext)$/i", $file))) {
$list[$id] = File::info($file);
// 是否遍历子目录
} elseif ($recursive && is_dir($file)) {
$list = self::getFilesInfo($file, $ext, $recursive, $list);
}
}
return $list;
}
/**
* 支持层级目录的创建
*
* @param string $path
* @param int $mode
* @param bool $recursive
*
* @return bool
*/
public static function create(string $path, int $mode = 0765, bool $recursive = true): bool
{
return (is_dir($path) || !(!@mkdir($path, $mode, $recursive) && !is_dir($path))) && is_writable($path);
}
/**
* Quick make sub-dirs in the given parent dir.
*
* @param string $parentDir
* @param array $subDirs
* @param int $mode
*
* @return bool
*/
public static function mkSubDirs(string $parentDir, array $subDirs, int $mode = 0666): bool
{
if (!self::create($parentDir)) {
return false;
}
foreach ($subDirs as $subPath) {
self::create($parentDir . '/' . $subPath, $mode);
}
return true;
}
/**
* Copy dir files, contains sub-dir.
*
* ### `$options`
*
* - skipExist: bool, whether skip exist file.
* - filterFn: callback func on handle each file. return false to skip copy
* - beforeFn: callback func on before copy file. return false to skip copy
* - afterFn: callback func on after copy file.
*
* @param string $oldDir source directory path.
* @param string $newDir target directory path.
* @param array $options = [
* 'skipExist' => true,
* 'filterFn' => function (string $old): bool { },
* 'beforeFn' => function (string $old, string $new): bool { },
* 'afterFn' => function (string $new): void { },
* ]
*
* @return bool
*/
public static function copy(string $oldDir, string $newDir, array $options = []): bool
{
if (!is_dir($oldDir)) {
throw new FileNotFoundException("Copy error: source dir does not exist! path: $oldDir");
}
self::doCopy($oldDir, $newDir, array_merge([
'skipExist' => true, // skip exist file
'filterFn' => null,
'beforeFn' => null,
'afterFn' => null,
], $options));
return true;
}
/**
* @param string $oldDir
* @param string $newDir
* @param array $options
*
* @return void
*/
private static function doCopy(string $oldDir, string $newDir, array $options): void
{
self::create($newDir);
$beforeFn = $options['beforeFn'];
$filterFn = $options['filterFn']; // filter file or dir.
// use '{,.}*' match hidden files
foreach (glob($oldDir . '/{,.}*', GLOB_BRACE) as $old) {
$name = basename($old);
if ($name === '.' || $name === '..') {
continue;
}
// return false to skip copy
if ($filterFn && !$filterFn($old)) {
continue;
}
$new = self::joinPath($newDir, $name);
if (is_dir($old)) {
self::doCopy($old, $new, $options);
continue;
}
// return false to skip copy
if ($beforeFn && !$beforeFn($old, $new)) {
continue;
}
if ($options['skipExist'] && file_exists($new)) {
continue;
}
// do copy
copy($old, $new);
@chmod($new, 0664); // 权限 0777
if ($afterFn = $options['afterFn']) {
$afterFn($new);
}
}
}
/**
* 删除目录及里面的文件
*
* @param string $path
* @param boolean $delSelf 默认最后删掉自己
*
* @return bool
*/
public static function delete(string $path, bool $delSelf = true): bool
{
$dirPath = self::pathFormat($path);
if (is_file($dirPath)) {
return unlink($dirPath);
}
foreach (glob($dirPath . '*') as $v) {
is_dir($v) ? self::delete($v) : unlink($v);
}
$delSelf && rmdir($dirPath);//默认最后删掉自己
return true;
}
}