Skip to content

Commit 0661bb1

Browse files
committed
up: add new traits for file,dir. add more param type limit
Signed-off-by: inhere <[email protected]>
1 parent fe485e3 commit 0661bb1

File tree

10 files changed

+376
-323
lines changed

10 files changed

+376
-323
lines changed

.github/workflows/php.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ jobs:
1010
strategy:
1111
fail-fast: true
1212
matrix:
13-
php: [7.1, 7.2, 7.3, 7.4, 8.0] #
13+
php: [7.3, 7.4, 8.0] #
1414
os: [ubuntu-latest, macOS-latest] # windows-latest,
15+
include:
16+
- os: 'ubuntu-latest'
17+
php: '7.2'
18+
phpunit: '8.5.13'
1519

1620
steps:
1721
- name: Checkout
@@ -23,7 +27,7 @@ jobs:
2327
uses: shivammathur/setup-php@v2
2428
with:
2529
php-version: ${{ matrix.php}}
26-
tools: pecl, php-cs-fixer, phpunit
30+
tools: pecl, php-cs-fixer, phpunit:${{ matrix.phpunit }}
2731
extensions: mbstring, dom, fileinfo, mysql, openssl, igbinary, redis # , swoole-4.4.19 #optional, setup extensions
2832
ini-values: post_max_size=56M, short_open_tag=On #optional, setup php.ini configuration
2933
coverage: none #optional, setup coverage driver: xdebug, none

src/Directory.php

Lines changed: 11 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use RecursiveIteratorIterator;
1717
use Toolkit\FsUtil\Exception\FileNotFoundException;
1818
use Toolkit\FsUtil\Exception\FileSystemException;
19+
use Toolkit\FsUtil\Traits\DirOperateTrait;
1920
use function basename;
2021
use function glob;
2122
use function implode;
@@ -33,92 +34,7 @@
3334
*/
3435
class Directory extends FileSystem
3536
{
36-
/**
37-
* ```php
38-
* $filter = function ($current, $key, $iterator) {
39-
* // \SplFileInfo $current
40-
* // Skip hidden files and directories.
41-
* if ($current->getFilename()[0] === '.') {
42-
* return false;
43-
* }
44-
* if ($current->isDir()) {
45-
* // Only recurse into intended subdirectories.
46-
* return $current->getFilename() !== '.git';
47-
* }
48-
* // Only consume files of interest.
49-
* return strpos($current->getFilename(), '.php') !== false;
50-
* };
51-
*
52-
* // $info is instance of \SplFileInfo
53-
* foreach(Directory::getRecursiveIterator($srcDir, $filter) as $info) {
54-
* // $info->getFilename(); ...
55-
* }
56-
* ```
57-
*
58-
* @param string $srcDir
59-
* @param callable $filter
60-
*
61-
* @return RecursiveIteratorIterator
62-
* @throws LogicException
63-
*/
64-
public static function getRecursiveIterator(string $srcDir, callable $filter): RecursiveIteratorIterator
65-
{
66-
return self::getIterator($srcDir, $filter);
67-
}
68-
69-
/**
70-
* 判断文件夹是否为空
71-
*
72-
* @param string $dir
73-
*
74-
* @return bool
75-
* @throws FileSystemException
76-
*/
77-
public static function isEmpty(string $dir): bool
78-
{
79-
$handler = opendir($dir);
80-
81-
if (false === $handler) {
82-
throw new FileSystemException("Open the dir failure! DIR: $dir");
83-
}
84-
85-
while (($file = readdir($handler)) !== false) {
86-
if ($file !== '.' && $file !== '..') {
87-
closedir($handler);
88-
89-
return false;
90-
}
91-
}
92-
93-
closedir($handler);
94-
95-
return true;
96-
}
97-
98-
/**
99-
* 查看一个目录中的所有文件和子目录
100-
*
101-
* @param string $path
102-
*
103-
* @return array
104-
* @throws FileNotFoundException
105-
*/
106-
public static function ls(string $path): array
107-
{
108-
$list = [];
109-
110-
try {
111-
/*** class create new DirectoryIterator Object ***/
112-
foreach (new DirectoryIterator($path) as $item) {
113-
$list[] = $item;
114-
}
115-
/*** if an exception is thrown, catch it here ***/
116-
} catch (Exception $e) {
117-
throw new FileNotFoundException($path . ' 没有任何内容');
118-
}
119-
120-
return $list;
121-
}
37+
use DirOperateTrait;
12238

12339
/**
12440
* 只获得目录结构
@@ -161,13 +77,12 @@ public static function getList(string $path, int $pid = 0, bool $son = false, ar
16177
}
16278

16379
/**
164-
* @param $path
165-
* @param bool $loop
166-
* @param null $parent
167-
* @param array $list
80+
* @param string $path
81+
* @param bool $loop
82+
* @param null $parent
83+
* @param array $list
16884
*
16985
* @return array
170-
* @throws FileNotFoundException
17186
*/
17287
public static function getDirs(string $path, bool $loop = false, $parent = null, array $list = []): array
17388
{
@@ -216,7 +131,6 @@ public static function simpleInfo(string $dir, $ext = null, bool $recursive = fa
216131

217132
// glob()寻找与模式匹配的文件路径 $file is pull path
218133
foreach (glob($dir . '*') as $file) {
219-
220134
// 匹配文件 如果没有传入$ext 则全部遍历,传入了则按传入的类型来查找
221135
if (is_file($file) && (!$ext || preg_match("/\.($ext)$/i", $file))) {
222136
//basename — 返回路径中的 文件名部分
@@ -242,7 +156,7 @@ public static function simpleInfo(string $dir, $ext = null, bool $recursive = fa
242156
* @param string $path string 目标目录
243157
* @param array|string $ext array('css','html','php') css|html|php
244158
* @param bool $recursive 是否包含子目录
245-
* @param null|string $parent
159+
* @param string $parent
246160
* @param array $list
247161
*
248162
* @return array
@@ -252,7 +166,7 @@ public static function getFiles(
252166
string $path,
253167
$ext = null,
254168
bool $recursive = false,
255-
$parent = null,
169+
string $parent = '',
256170
array $list = []
257171
): array {
258172
$path = self::pathFormat($path);
@@ -293,7 +207,6 @@ public static function getFiles(
293207
public static function getFilesInfo(string $path, $ext = null, bool $recursive = false, array &$list = []): array
294208
{
295209
$path = self::pathFormat($path);
296-
297210
if (!is_dir($path)) {
298211
throw new FileNotFoundException("directory not exists! DIR: $path");
299212
}
@@ -302,7 +215,7 @@ public static function getFilesInfo(string $path, $ext = null, bool $recursive =
302215

303216
static $id = 0;
304217

305-
//glob()寻找与模式匹配的文件路径
218+
// glob()寻找与模式匹配的文件路径
306219
foreach (glob($path . '*') as $file) {
307220
$id++;
308221

@@ -336,11 +249,10 @@ public static function create(string $path, int $mode = 0775, bool $recursive =
336249
/**
337250
* 复制目录内容
338251
*
339-
* @param $oldDir
340-
* @param $newDir
252+
* @param string $oldDir
253+
* @param string $newDir
341254
*
342255
* @return bool
343-
* @throws FileNotFoundException
344256
*/
345257
public static function copy(string $oldDir, string $newDir): bool
346258
{
@@ -393,7 +305,6 @@ public static function delete(string $path, bool $delSelf = true): bool
393305
}
394306

395307
$delSelf && rmdir($dirPath);//默认最后删掉自己
396-
397308
return true;
398309
}
399310
}

0 commit comments

Comments
 (0)