@@ -265,10 +265,18 @@ public static function mkSubDirs(string $parentDir, array $subDirs, int $mode =
265
265
/**
266
266
* Copy dir files, contains sub-dir.
267
267
*
268
- * @param string $oldDir
269
- * @param string $newDir
268
+ * ### $options
269
+ *
270
+ * - skipExist: bool, whether skip exist file.
271
+ * - filterFn: callback func on handle each file.
272
+ * - beforeFn: callback func on before copy file.
273
+ * - afterFn: callback func on after copy file.
274
+ *
275
+ * @param string $oldDir source directory path.
276
+ * @param string $newDir target directory path.
270
277
* @param array $options = [
271
278
* 'skipExist' => true,
279
+ * 'filterFn' => function (string $old): bool { },
272
280
* 'beforeFn' => function (string $old, string $new): bool { },
273
281
* 'afterFn' => function (string $new): void { },
274
282
* ]
@@ -278,11 +286,12 @@ public static function mkSubDirs(string $parentDir, array $subDirs, int $mode =
278
286
public static function copy (string $ oldDir , string $ newDir , array $ options = []): bool
279
287
{
280
288
if (!is_dir ($ oldDir )) {
281
- throw new FileNotFoundException (' copy failed: ' . $ oldDir . ' does not exist!' );
289
+ throw new FileNotFoundException (" copy error:source dir does not exist!path: $ oldDir " );
282
290
}
283
291
284
292
self ::doCopy ($ oldDir , $ newDir , array_merge ([
285
293
'skipExist ' => true ,
294
+ 'filterFn ' => null ,
286
295
'beforeFn ' => null ,
287
296
'afterFn ' => null ,
288
297
], $ options ));
@@ -301,6 +310,7 @@ private static function doCopy(string $oldDir, string $newDir, array $options):
301
310
{
302
311
self ::create ($ newDir );
303
312
$ beforeFn = $ options ['beforeFn ' ];
313
+ $ filterFn = $ options ['filterFn ' ];
304
314
305
315
// use '{,.}*' match hidden files
306
316
foreach (glob ($ oldDir . '/{,.}* ' , GLOB_BRACE ) as $ old ) {
@@ -316,6 +326,11 @@ private static function doCopy(string $oldDir, string $newDir, array $options):
316
326
continue ;
317
327
}
318
328
329
+ // return false to skip copy
330
+ if ($ filterFn && !$ filterFn ($ old )) {
331
+ continue ;
332
+ }
333
+
319
334
// return false to skip copy
320
335
if ($ beforeFn && !$ beforeFn ($ old , $ new )) {
321
336
continue ;
0 commit comments