Skip to content

Commit

Permalink
fix: fetch dir files error
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 19, 2021
1 parent 2b7aede commit 1390604
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
38 changes: 16 additions & 22 deletions src/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@

namespace Toolkit\FsUtil;

use DirectoryIterator;
use Exception;
use InvalidArgumentException;
use LogicException;
use RecursiveIteratorIterator;
use Toolkit\FsUtil\Exception\FileNotFoundException;
use Toolkit\FsUtil\Exception\FileSystemException;
use Toolkit\FsUtil\Traits\DirOperateTrait;
use function basename;
use function glob;
Expand All @@ -40,17 +35,16 @@ class Directory extends FileSystem
* 只获得目录结构
*
* @param string $path
* @param int $pid
* @param bool $son
* @param array $list
* @param int $pid
* @param bool $son
* @param array $list
*
* @return array
* @throws FileNotFoundException
*/
public static function getList(string $path, int $pid = 0, bool $son = false, array $list = []): array
{
$path = self::pathFormat($path);

if (!is_dir($path)) {
throw new FileNotFoundException("directory not exists! DIR: $path");
}
Expand Down Expand Up @@ -136,11 +130,11 @@ public static function simpleInfo(string $dir, $ext = null, bool $recursive = fa
//basename — 返回路径中的 文件名部分
$list[] = basename($file);

// is directory
// is directory
} else {
$list[] = '/' . basename($file);

if ($recursive) {
if ($recursive && is_dir($file)) {
/** @noinspection SlowArrayOperationsInLoopInspection */
$list = array_merge($list, self::simpleInfo($file, $ext, $recursive));
}
Expand All @@ -156,7 +150,7 @@ public static function simpleInfo(string $dir, $ext = null, bool $recursive = fa
* @param string $path string 目标目录
* @param array|string $ext array('css','html','php') css|html|php
* @param bool $recursive 是否包含子目录
* @param string $parent
* @param string $parent
* @param array $list
*
* @return array
Expand Down Expand Up @@ -184,7 +178,7 @@ public static function getFiles(
// 匹配文件 如果没有传入$ext 则全部遍历,传入了则按传入的类型来查找
if (is_file($v) && (!$ext || preg_match("/\.($ext)$/i", $v))) {
$list[] = $parent . $relatePath;
} elseif ($recursive) {
} elseif ($recursive && is_dir($v)) {
$list = self::getFiles($v, $ext, $recursive, $relatePath . '/', $list);
}
}
Expand All @@ -195,9 +189,9 @@ public static function getFiles(
/**
* 获得目录下的文件以及详细信息,可选择类型、是否遍历子文件夹
*
* @param string $path string 目标目录
* @param string $path string 目标目录
* @param array|string $ext array('css','html','php') css|html|php
* @param bool $recursive 是否包含子目录
* @param bool $recursive 是否包含子目录
* @param array $list
*
* @return array
Expand All @@ -223,8 +217,8 @@ public static function getFilesInfo(string $path, $ext = null, bool $recursive =
if (is_file($file) && (!$ext || preg_match("/\.($ext)$/i", $file))) {
$list[$id] = File::info($file);

//是否遍历子目录
} elseif ($recursive) {
// 是否遍历子目录
} elseif ($recursive && is_dir($file)) {
$list = self::getFilesInfo($file, $ext, $recursive, $list);
}
}
Expand All @@ -235,9 +229,9 @@ public static function getFilesInfo(string $path, $ext = null, bool $recursive =
/**
* 支持层级目录的创建
*
* @param string $path
* @param int $mode
* @param bool $recursive
* @param string $path
* @param int $mode
* @param bool $recursive
*
* @return bool
*/
Expand Down Expand Up @@ -287,8 +281,8 @@ public static function copy(string $oldDir, string $newDir): bool
/**
* 删除目录及里面的文件
*
* @param string $path
* @param boolean $delSelf 默认最后删掉自己
* @param string $path
* @param boolean $delSelf 默认最后删掉自己
*
* @return bool
*/
Expand Down
17 changes: 10 additions & 7 deletions src/Traits/FileSystemFuncTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ public static function chown($files, string $user, bool $recursive = false): voi
}

/**
* clear invalid sep and will parse ~ as user home dir.
*
* @param string $path
*
* @return string
Expand All @@ -172,12 +174,6 @@ public static function realpath(string $path): string
return '';
}

$start = '';
$isUnix = DIRECTORY_SEPARATOR === '/';
if ($isUnix) {
$start = '/';
}

// ~: is user home dir in *nix OS
if ($parts[0] === '~') {
$parts[0] = OS::getUserHomeDir();
Expand All @@ -196,7 +192,14 @@ public static function realpath(string $path): string
}
}

return $start . implode(DIRECTORY_SEPARATOR, $absolutes);
$fullPath = implode(DIRECTORY_SEPARATOR, $absolutes);

// is unix like OS
if (DIRECTORY_SEPARATOR === '/' && $fullPath[0] !== '/') {
return '/' . $fullPath;
}

return $fullPath;
}

/**********************************************************************************
Expand Down

0 comments on commit 1390604

Please sign in to comment.