Skip to content

Commit a86ab5b

Browse files
committedDec 30, 2022
up: update the dir copy and file tree build
·
v2.0.7v2.0.5
1 parent d5309c7 commit a86ab5b

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed
 

‎src/Directory.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

‎src/Extra/FileTreeBuilder.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,17 @@ public function copyDir(string $srcDir, string $dstDir, array $options = []): se
144144
$this->printMsg("copy dir $srcDir to $dstDir");
145145

146146
Dir::copy($srcDir, $dstDir, [
147-
'beforeFn' => function (string $oldFile) use ($options): bool {
148-
if ($this->dryRun) {
149-
$this->printMsgf('- copy file %s', $oldFile);
150-
return false;
151-
}
152-
147+
'filterFn' => function (string $oldFile) use ($options): bool {
153148
if ($options['include']) {
154149
return File::isInclude($oldFile, $options['include']);
155150
}
156-
157151
return !File::isExclude($oldFile, $options['exclude']);
158152
},
153+
'beforeFn' => function (string $oldFile, string $newFile): bool {
154+
$this->printMsgf('- copy file %s to %s', $oldFile, $newFile);
155+
156+
return !$this->dryRun;
157+
},
159158
'afterFn' => function (string $newFile) use ($options) {
160159
if ($fn = $options['afterFn']) {
161160
$fn($newFile);

0 commit comments

Comments
 (0)
Please sign in to comment.