Skip to content

Commit 6ed0c4c

Browse files
committed
chore: add more fs util method, add more tests
1 parent 3f4bfda commit 6ed0c4c

File tree

4 files changed

+94
-10
lines changed

4 files changed

+94
-10
lines changed

src/Directory.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public static function getDirs(string $path, bool $loop = false, $parent = null,
8787
}
8888

8989
$len = strlen($path);
90-
9190
foreach (glob($path . '*') as $v) {
9291
if (is_dir($v)) {
9392
$relatePath = substr($v, $len);
@@ -205,9 +204,8 @@ public static function getFilesInfo(string $path, $ext = null, bool $recursive =
205204
throw new FileNotFoundException("directory not exists! DIR: $path");
206205
}
207206

208-
$ext = is_array($ext) ? implode('|', $ext) : trim($ext);
209-
210207
static $id = 0;
208+
$ext = is_array($ext) ? implode('|', $ext) : trim($ext);
211209

212210
// glob()寻找与模式匹配的文件路径
213211
foreach (glob($path . '*') as $file) {
@@ -235,11 +233,32 @@ public static function getFilesInfo(string $path, $ext = null, bool $recursive =
235233
*
236234
* @return bool
237235
*/
238-
public static function create(string $path, int $mode = 0775, bool $recursive = true): bool
236+
public static function create(string $path, int $mode = 0665, bool $recursive = true): bool
239237
{
240238
return (is_dir($path) || !(!@mkdir($path, $mode, $recursive) && !is_dir($path))) && is_writable($path);
241239
}
242240

241+
/**
242+
* quick make sub-dirs in the given parent dir.
243+
*
244+
* @param string $parentDir
245+
* @param array $subDirs
246+
* @param int $mode
247+
*
248+
* @return bool
249+
*/
250+
public static function mkSubDirs(string $parentDir, array $subDirs, int $mode = 0665): bool
251+
{
252+
if (!self::create($parentDir)) {
253+
return false;
254+
}
255+
256+
foreach ($subDirs as $subPath) {
257+
258+
}
259+
return true;
260+
}
261+
243262
/**
244263
* 复制目录内容
245264
*
@@ -258,7 +277,6 @@ public static function copy(string $oldDir, string $newDir): bool
258277
}
259278

260279
self::create($newDir);
261-
262280
foreach (glob($oldDir . '*') as $v) {
263281
$newFile = $newDir . basename($v);//文件
264282

src/FileSystem.php

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@
1616
use Toolkit\FsUtil\Exception\FileNotFoundException;
1717
use Toolkit\FsUtil\Traits\FileSystemFuncTrait;
1818
use Toolkit\Stdlib\OS;
19+
use function array_filter;
1920
use function count;
2021
use function file_exists;
22+
use function implode;
2123
use function is_array;
2224
use function is_string;
2325
use function preg_match;
2426
use function str_ireplace;
2527
use function strlen;
2628
use function strpos;
2729
use function substr;
30+
use const DIRECTORY_SEPARATOR;
2831

2932
/**
3033
* Class FileSystem
@@ -46,7 +49,7 @@ public static function isAbsPath(string $path): bool
4649
return false;
4750
}
4851

49-
if (strpos($path, '/') === 0 || // linux/mac
52+
if (str_starts_with($path, '/') || // linux/mac
5053
1 === preg_match('#^[a-z]:[\/|\\\].+#i', $path) // windows
5154
) {
5255
return true;
@@ -71,6 +74,16 @@ public static function isAbsolutePath(string $file): bool
7174
null !== parse_url($file, PHP_URL_SCHEME);
7275
}
7376

77+
/**
78+
* @param string $path
79+
*
80+
* @return string
81+
*/
82+
public static function getAbsPath(string $path): string
83+
{
84+
return self::realpath($path);
85+
}
86+
7487
/**
7588
* 转换为标准的路径结构
7689
*
@@ -82,7 +95,34 @@ public static function pathFormat(string $dirName): string
8295
{
8396
$dirName = (string)str_ireplace('\\', '/', trim($dirName));
8497

85-
return substr($dirName, -1) === '/' ? $dirName : $dirName . '/';
98+
return str_ends_with($dirName, '/') ? $dirName : $dirName . '/';
99+
}
100+
101+
/**
102+
* @param string $path
103+
* @param string ...$subPaths
104+
*
105+
* @return string
106+
*/
107+
public static function join(string $path, string ...$subPaths): string
108+
{
109+
return self::joinPath($path, ...$subPaths);
110+
}
111+
112+
/**
113+
* @param string $path
114+
* @param string ...$subPaths
115+
*
116+
* @return string
117+
*/
118+
public static function joinPath(string $path, string ...$subPaths): string
119+
{
120+
$subPaths = array_filter($subPaths, 'strlen');
121+
if (!$subPaths) {
122+
return $path;
123+
}
124+
125+
return $path . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $subPaths);
86126
}
87127

88128
/**
@@ -102,8 +142,8 @@ public static function clearPharPath(string $path): string
102142
*/
103143
public static function clearPharMark(string $path): string
104144
{
105-
if (strpos($path, 'phar://') === 0) {
106-
$path = (string)substr($path, 7);
145+
if (str_starts_with($path, 'phar://')) {
146+
$path = substr($path, 7);
107147

108148
if (strpos($path, '.phar') > 0) {
109149
return preg_replace('/\/[\w\.-]+\.phar/', '', $path);

src/Traits/FileSystemFuncTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public static function realpath(string $path): string
245245
return '';
246246
}
247247

248-
// ~: is user home dir in *nix OS
248+
// ~: is user home dir in OS
249249
if ($parts[0] === '~') {
250250
$parts[0] = OS::getUserHomeDir();
251251
}

test/FsTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use PHPUnit\Framework\TestCase;
66
use Toolkit\FsUtil\FS;
7+
use function strlen;
8+
use function vdump;
79

810
/**
911
* class FsTest
@@ -26,6 +28,30 @@ public function testIsAbsPath(): void
2628
}
2729
}
2830

31+
public function testBasicFsMethods(): void
32+
{
33+
$this->assertEquals('/ab', FS::join('/ab'));
34+
$this->assertEquals('/ab/d', FS::join('/ab', '', 'd'));
35+
$this->assertEquals('/ab/d/e', FS::join('/ab', 'd', 'e'));
36+
}
37+
38+
public function testRealpath(): void
39+
{
40+
$rPaths = [];
41+
$tests = [
42+
'~',
43+
'~/.kite',
44+
];
45+
foreach ($tests as $path) {
46+
$rPaths[$path] = $rPath = FS::realpath($path);
47+
$this->assertTrue(strlen($rPath) > strlen($path));
48+
$rPath = FS::getAbsPath($path);
49+
$this->assertTrue(strlen($rPath) > strlen($path));
50+
}
51+
52+
vdump($rPaths);
53+
}
54+
2955
public function testClearPharPath(): void
3056
{
3157
$tests = [

0 commit comments

Comments
 (0)