@@ -265,10 +265,18 @@ public static function mkSubDirs(string $parentDir, array $subDirs, int $mode =
265265 /**
266266 * Copy dir files, contains sub-dir.
267267 *
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.
270277 * @param array $options = [
271278 * 'skipExist' => true,
279+ * 'filterFn' => function (string $old): bool { },
272280 * 'beforeFn' => function (string $old, string $new): bool { },
273281 * 'afterFn' => function (string $new): void { },
274282 * ]
@@ -278,11 +286,12 @@ public static function mkSubDirs(string $parentDir, array $subDirs, int $mode =
278286 public static function copy (string $ oldDir , string $ newDir , array $ options = []): bool
279287 {
280288 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 " );
282290 }
283291
284292 self ::doCopy ($ oldDir , $ newDir , array_merge ([
285293 'skipExist ' => true ,
294+ 'filterFn ' => null ,
286295 'beforeFn ' => null ,
287296 'afterFn ' => null ,
288297 ], $ options ));
@@ -301,6 +310,7 @@ private static function doCopy(string $oldDir, string $newDir, array $options):
301310 {
302311 self ::create ($ newDir );
303312 $ beforeFn = $ options ['beforeFn ' ];
313+ $ filterFn = $ options ['filterFn ' ];
304314
305315 // use '{,.}*' match hidden files
306316 foreach (glob ($ oldDir . '/{,.}* ' , GLOB_BRACE ) as $ old ) {
@@ -316,6 +326,11 @@ private static function doCopy(string $oldDir, string $newDir, array $options):
316326 continue ;
317327 }
318328
329+ // return false to skip copy
330+ if ($ filterFn && !$ filterFn ($ old )) {
331+ continue ;
332+ }
333+ 319334 // return false to skip copy
320335 if ($ beforeFn && !$ beforeFn ($ old , $ new )) {
321336 continue ;
0 commit comments