Skip to content

Commit 803a0c9

Browse files
committed
✨ up: update some for fs util and file tree builder
1 parent 5c48790 commit 803a0c9

File tree

6 files changed

+44
-28
lines changed

6 files changed

+44
-28
lines changed

src/Directory.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,11 @@ public static function mkSubDirs(string $parentDir, array $subDirs, int $mode =
265265
/**
266266
* Copy dir files, contains sub-dir.
267267
*
268-
* ### $options
268+
* ### `$options`
269269
*
270270
* - skipExist: bool, whether skip exist file.
271-
* - filterFn: callback func on handle each file.
272-
* - beforeFn: callback func on before copy file.
271+
* - filterFn: callback func on handle each file. return false to skip copy
272+
* - beforeFn: callback func on before copy file. return false to skip copy
273273
* - afterFn: callback func on after copy file.
274274
*
275275
* @param string $oldDir source directory path.
@@ -286,7 +286,7 @@ public static function mkSubDirs(string $parentDir, array $subDirs, int $mode =
286286
public static function copy(string $oldDir, string $newDir, array $options = []): bool
287287
{
288288
if (!is_dir($oldDir)) {
289-
throw new FileNotFoundException("Copy error: source dir does not existpath: $oldDir");
289+
throw new FileNotFoundException("Copy error: source dir does not exist! path: $oldDir");
290290
}
291291

292292
self::doCopy($oldDir, $newDir, array_merge([

src/Extra/FileTreeBuilder.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Toolkit\FsUtil\Dir;
66
use Toolkit\FsUtil\File;
77
use Toolkit\FsUtil\FS;
8+
use Toolkit\FsUtil\Path;
89
use Toolkit\Stdlib\Helper\Assert;
910
use Toolkit\Stdlib\Obj\AbstractObj;
1011
use Toolkit\Stdlib\Str;
@@ -715,7 +716,7 @@ public function setShowMsg(bool $showMsg): self
715716
*/
716717
public function setTplDir(string $tplDir): self
717718
{
718-
$this->tplDir = $tplDir;
719+
$this->tplDir = Path::format($tplDir, false);
719720
return $this;
720721
}
721722

src/FileSystem.php

+12-14
Original file line numberDiff line numberDiff line change
@@ -97,35 +97,29 @@ public static function isRelative(string $path): bool
9797

9898
/**
9999
* @param string $path
100-
* @param array $patterns
100+
* @param array $patterns eg: ['*.php', '*.html']
101101
*
102102
* @return bool
103103
*/
104104
public static function isExclude(string $path, array $patterns): bool
105105
{
106-
if (!$patterns) {
107-
return false;
108-
}
109-
return self::isMatch($path, $patterns);
106+
return $patterns && self::isMatch($path, $patterns);
110107
}
111108

112109
/**
113110
* @param string $path
114-
* @param array $patterns
111+
* @param array $patterns eg: ['*.php', '*.html']
115112
*
116113
* @return bool
117114
*/
118115
public static function isInclude(string $path, array $patterns): bool
119116
{
120-
if (!$patterns) {
121-
return true;
122-
}
123-
return self::isMatch($path, $patterns);
117+
return !$patterns || self::isMatch($path, $patterns);
124118
}
125119

126120
/**
127121
* @param string $path
128-
* @param array $patterns
122+
* @param array $patterns eg: ['*.php', '*.html']
129123
*
130124
* @return bool
131125
*/
@@ -154,17 +148,21 @@ public static function getAbsPath(string $path): string
154148
}
155149

156150
/**
157-
* Path format. will replace \ to /, and always end /
151+
* Path format. will replace \ to /
158152
*
159153
* @param string $dirName
154+
* @param bool $endWithSlash set end with slash '/'. default true
160155
*
161156
* @return string
162157
*/
163-
public static function pathFormat(string $dirName): string
158+
public static function pathFormat(string $dirName, bool $endWithSlash = true): string
164159
{
165160
$dirName = (string)str_ireplace('\\', '/', trim($dirName));
166161

167-
return str_ends_with($dirName, '/') ? $dirName : $dirName . '/';
162+
if (str_ends_with($dirName, '/')) {
163+
return $endWithSlash ? $dirName : substr($dirName, 0, -1);
164+
}
165+
return $endWithSlash ? $dirName . '/' : $dirName;
168166
}
169167

170168
/**

src/Path.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ public static function isAbsolute(string $path): bool
3131

3232
/**
3333
* @param string $path
34+
* @param bool $endWithSlash
3435
*
3536
* @return string
3637
*/
37-
public static function format(string $path): string
38+
public static function format(string $path, bool $endWithSlash = true): string
3839
{
39-
return self::pathFormat($path);
40+
return self::pathFormat($path, $endWithSlash);
4041
}
4142
}

test/FsTest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ public function testBasicFsMethods(): void
5555
public function testIsExclude_isInclude(): void
5656
{
5757
$tests = [
58-
['./abc.php', '*', true],
59-
['./abc.php', '*.php', true],
60-
['./abc.php', '*.yml', false],
61-
['path/to/abc.php', '*.php', true],
58+
['./abc.php', ['*'], true],
59+
['./abc.php', ['*.php'], true],
60+
['./abc.php', ['*.yml'], false],
61+
['path/to/abc.php', ['*.php'], true],
6262
];
6363
foreach ($tests as $item) {
64-
$this->assertEquals($item[2], FS::isInclude($item[0], [$item[1]]));
65-
$this->assertEquals($item[2], FS::isExclude($item[0], [$item[1]]));
66-
$this->assertEquals($item[2], FS::isMatch($item[0], [$item[1]]));
64+
$this->assertEquals($item[2], FS::isInclude($item[0], $item[1]));
65+
$this->assertEquals($item[2], FS::isExclude($item[0], $item[1]));
66+
$this->assertEquals($item[2], FS::isMatch($item[0], $item[1]));
6767
}
6868

6969
$this->assertTrue(FS::isInclude('./abc.php', []));

test/PathTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,20 @@ public function testPath_isAbs(): void
2727
$this->assertTrue(Path::isAbsolute($case));
2828
}
2929
}
30+
31+
public function testFormat(): void
32+
{
33+
$tests = [
34+
'/path/to/tmp' => '/path/to/tmp',
35+
'C:\tmp' => 'C:/tmp',
36+
'C:/path/to/dir' => 'C:/path/to/dir',
37+
'C:\\path\\to\\dir' => 'C:/path/to/dir',
38+
"path\\to\\dir" => 'path/to/dir',
39+
];
40+
41+
foreach ($tests as $case => $expect) {
42+
$this->assertSame($expect, Path::format($case, false));
43+
$this->assertSame($expect . '/', Path::format($case));
44+
}
45+
}
3046
}

0 commit comments

Comments
 (0)