Skip to content

Commit 1390604

Browse files
committed
fix: fetch dir files error
1 parent 2b7aede commit 1390604

File tree

2 files changed

+26
-29
lines changed

2 files changed

+26
-29
lines changed

src/Directory.php

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@
99

1010
namespace Toolkit\FsUtil;
1111

12-
use DirectoryIterator;
13-
use Exception;
1412
use InvalidArgumentException;
15-
use LogicException;
16-
use RecursiveIteratorIterator;
1713
use Toolkit\FsUtil\Exception\FileNotFoundException;
18-
use Toolkit\FsUtil\Exception\FileSystemException;
1914
use Toolkit\FsUtil\Traits\DirOperateTrait;
2015
use function basename;
2116
use function glob;
@@ -40,17 +35,16 @@ class Directory extends FileSystem
4035
* 只获得目录结构
4136
*
4237
* @param string $path
43-
* @param int $pid
44-
* @param bool $son
45-
* @param array $list
38+
* @param int $pid
39+
* @param bool $son
40+
* @param array $list
4641
*
4742
* @return array
4843
* @throws FileNotFoundException
4944
*/
5045
public static function getList(string $path, int $pid = 0, bool $son = false, array $list = []): array
5146
{
5247
$path = self::pathFormat($path);
53-
5448
if (!is_dir($path)) {
5549
throw new FileNotFoundException("directory not exists! DIR: $path");
5650
}
@@ -136,11 +130,11 @@ public static function simpleInfo(string $dir, $ext = null, bool $recursive = fa
136130
//basename — 返回路径中的 文件名部分
137131
$list[] = basename($file);
138132

139-
// is directory
133+
// is directory
140134
} else {
141135
$list[] = '/' . basename($file);
142136

143-
if ($recursive) {
137+
if ($recursive && is_dir($file)) {
144138
/** @noinspection SlowArrayOperationsInLoopInspection */
145139
$list = array_merge($list, self::simpleInfo($file, $ext, $recursive));
146140
}
@@ -156,7 +150,7 @@ public static function simpleInfo(string $dir, $ext = null, bool $recursive = fa
156150
* @param string $path string 目标目录
157151
* @param array|string $ext array('css','html','php') css|html|php
158152
* @param bool $recursive 是否包含子目录
159-
* @param string $parent
153+
* @param string $parent
160154
* @param array $list
161155
*
162156
* @return array
@@ -184,7 +178,7 @@ public static function getFiles(
184178
// 匹配文件 如果没有传入$ext 则全部遍历,传入了则按传入的类型来查找
185179
if (is_file($v) && (!$ext || preg_match("/\.($ext)$/i", $v))) {
186180
$list[] = $parent . $relatePath;
187-
} elseif ($recursive) {
181+
} elseif ($recursive && is_dir($v)) {
188182
$list = self::getFiles($v, $ext, $recursive, $relatePath . '/', $list);
189183
}
190184
}
@@ -195,9 +189,9 @@ public static function getFiles(
195189
/**
196190
* 获得目录下的文件以及详细信息,可选择类型、是否遍历子文件夹
197191
*
198-
* @param string $path string 目标目录
192+
* @param string $path string 目标目录
199193
* @param array|string $ext array('css','html','php') css|html|php
200-
* @param bool $recursive 是否包含子目录
194+
* @param bool $recursive 是否包含子目录
201195
* @param array $list
202196
*
203197
* @return array
@@ -223,8 +217,8 @@ public static function getFilesInfo(string $path, $ext = null, bool $recursive =
223217
if (is_file($file) && (!$ext || preg_match("/\.($ext)$/i", $file))) {
224218
$list[$id] = File::info($file);
225219

226-
//是否遍历子目录
227-
} elseif ($recursive) {
220+
// 是否遍历子目录
221+
} elseif ($recursive && is_dir($file)) {
228222
$list = self::getFilesInfo($file, $ext, $recursive, $list);
229223
}
230224
}
@@ -235,9 +229,9 @@ public static function getFilesInfo(string $path, $ext = null, bool $recursive =
235229
/**
236230
* 支持层级目录的创建
237231
*
238-
* @param string $path
239-
* @param int $mode
240-
* @param bool $recursive
232+
* @param string $path
233+
* @param int $mode
234+
* @param bool $recursive
241235
*
242236
* @return bool
243237
*/
@@ -287,8 +281,8 @@ public static function copy(string $oldDir, string $newDir): bool
287281
/**
288282
* 删除目录及里面的文件
289283
*
290-
* @param string $path
291-
* @param boolean $delSelf 默认最后删掉自己
284+
* @param string $path
285+
* @param boolean $delSelf 默认最后删掉自己
292286
*
293287
* @return bool
294288
*/

src/Traits/FileSystemFuncTrait.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ public static function chown($files, string $user, bool $recursive = false): voi
158158
}
159159

160160
/**
161+
* clear invalid sep and will parse ~ as user home dir.
162+
*
161163
* @param string $path
162164
*
163165
* @return string
@@ -172,12 +174,6 @@ public static function realpath(string $path): string
172174
return '';
173175
}
174176

175-
$start = '';
176-
$isUnix = DIRECTORY_SEPARATOR === '/';
177-
if ($isUnix) {
178-
$start = '/';
179-
}
180-
181177
// ~: is user home dir in *nix OS
182178
if ($parts[0] === '~') {
183179
$parts[0] = OS::getUserHomeDir();
@@ -196,7 +192,14 @@ public static function realpath(string $path): string
196192
}
197193
}
198194

199-
return $start . implode(DIRECTORY_SEPARATOR, $absolutes);
195+
$fullPath = implode(DIRECTORY_SEPARATOR, $absolutes);
196+
197+
// is unix like OS
198+
if (DIRECTORY_SEPARATOR === '/' && $fullPath[0] !== '/') {
199+
return '/' . $fullPath;
200+
}
201+
202+
return $fullPath;
200203
}
201204

202205
/**********************************************************************************

0 commit comments

Comments
 (0)