Skip to content

Commit 8ec97f0

Browse files
committedJun 11, 2020
upsome logic
1 parent 0641f8d commit 8ec97f0

File tree

8 files changed

+304
-227
lines changed

8 files changed

+304
-227
lines changed
 

‎.php_cs

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,49 @@
11
<?php
22

33
$header = <<<'EOF'
4-
This file is part of toolkit/phpkit.
4+
This file is part of toolkit/fsutil.
55
66
@author https://github.com/inhere
7-
@link https://github.com/php-toolkit/phpkit
7+
@link https://github.com/toolkit/fsutil
88
@license MIT
99
EOF;
1010

11+
$rules = [
12+
'@PSR2' => true,
13+
'array_syntax' => [
14+
'syntax' => 'short'
15+
],
16+
'list_syntax' => [
17+
'syntax' => 'short'
18+
],
19+
'class_attributes_separation' => true,
20+
'declare_strict_types' => true,
21+
'global_namespace_import' => [
22+
'import_constants' => true,
23+
'import_functions' => true,
24+
],
25+
'header_comment' => [
26+
'comment_type' => 'PHPDoc',
27+
'header' => $header,
28+
'separate' => 'bottom'
29+
],
30+
'no_unused_imports' => true,
31+
'return_type_declaration' => [
32+
'space_before' => 'none',
33+
],
34+
'single_quote' => true,
35+
'standardize_not_equals' => true,
36+
'void_return' => true, // add :void for method
37+
];
38+
1139
return PhpCsFixer\Config::create()
12-
->setRiskyAllowed(true)
13-
->setRules([
14-
'@PSR2' => true,
15-
'array_syntax' => [
16-
'syntax' => 'short'
17-
],
18-
'list_syntax' => [
19-
'syntax' => 'short'
20-
],
21-
'class_attributes_separation' => true,
22-
'declare_strict_types' => true,
23-
'global_namespace_import' => [
24-
'import_constants' => true,
25-
'import_functions' => true,
26-
],
27-
'header_comment' => [
28-
'comment_type' => 'PHPDoc',
29-
'header' => $header,
30-
'separate' => 'bottom'
31-
],
32-
'no_unused_imports' => true,
33-
'single_quote' => true,
34-
'standardize_not_equals' => true,
35-
])
36-
->setFinder(
37-
PhpCsFixer\Finder::create()
38-
// ->exclude('test')
39-
->exclude('runtime')
40-
->exclude('vendor')
41-
->in(__DIR__)
42-
)
43-
->setUsingCache(false);
40+
->setRiskyAllowed(true)
41+
->setRules($rules)
42+
->setFinder(
43+
PhpCsFixer\Finder::create()
44+
// ->exclude('test')
45+
->exclude('runtime')
46+
->exclude('vendor')
47+
->in(__DIR__)
48+
)
49+
->setUsingCache(false);

‎src/Dir.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\FsUtil;
4+
5+
/**
6+
* Class Dir
7+
*
8+
* @package Toolkit\FsUtil
9+
*/
10+
final class Dir extends Directory
11+
{
12+
13+
}

‎src/Directory.php

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ public static function getRecursiveIterator($srcDir, callable $filter): Recursiv
6868
/**
6969
* 判断文件夹是否为空
7070
*
71-
* @param $dir
71+
* @param string $dir
7272
*
7373
* @return bool
7474
* @throws FileSystemException
7575
*/
76-
public static function isEmpty($dir): bool
76+
public static function isEmpty(string $dir): bool
7777
{
7878
$handler = opendir($dir);
7979

@@ -98,12 +98,12 @@ public static function isEmpty($dir): bool
9898
/**
9999
* 查看一个目录中的所有文件和子目录
100100
*
101-
* @param $path
101+
* @param string $path
102102
*
103103
* @return array
104104
* @throws FileNotFoundException
105105
*/
106-
public static function ls($path): array
106+
public static function ls(string $path): array
107107
{
108108
$list = [];
109109

@@ -123,15 +123,15 @@ public static function ls($path): array
123123
/**
124124
* 只获得目录结构
125125
*
126-
* @param $path
126+
* @param string $path
127127
* @param int $pid
128-
* @param int $son
128+
* @param bool $son
129129
* @param array $list
130130
*
131131
* @return array
132132
* @throws FileNotFoundException
133133
*/
134-
public static function getList($path, $pid = 0, $son = 0, array $list = []): array
134+
public static function getList(string $path, int $pid = 0, bool $son = false, array $list = []): array
135135
{
136136
$path = self::pathFormat($path);
137137

@@ -169,7 +169,7 @@ public static function getList($path, $pid = 0, $son = 0, array $list = []): arr
169169
* @return array
170170
* @throws FileNotFoundException
171171
*/
172-
public static function getDirs($path, $loop = false, $parent = null, array $list = []): array
172+
public static function getDirs(string $path, bool $loop = false, $parent = null, array $list = []): array
173173
{
174174
$path = self::pathFormat($path);
175175

@@ -204,7 +204,7 @@ public static function getDirs($path, $loop = false, $parent = null, array $list
204204
* @return array
205205
* @throws FileNotFoundException
206206
*/
207-
public static function simpleInfo(string $dir, $ext = null, $recursive = false): array
207+
public static function simpleInfo(string $dir, $ext = null, bool $recursive = false): array
208208
{
209209
$list = [];
210210
$dir = self::pathFormat($dir);
@@ -227,6 +227,7 @@ public static function simpleInfo(string $dir, $ext = null, $recursive = false):
227227
$list[] = '/' . basename($file);
228228

229229
if ($recursive) {
230+
/** @noinspection SlowArrayOperationsInLoopInspection */
230231
$list = array_merge($list, self::simpleInfo($file, $ext, $recursive));
231232
}
232233
}
@@ -250,7 +251,7 @@ public static function simpleInfo(string $dir, $ext = null, $recursive = false):
250251
public static function getFiles(
251252
string $path,
252253
$ext = null,
253-
$recursive = false,
254+
bool $recursive = false,
254255
$parent = null,
255256
array $list = []
256257
): array {
@@ -281,16 +282,16 @@ public static function getFiles(
281282
/**
282283
* 获得目录下的文件以及详细信息,可选择类型、是否遍历子文件夹
283284
*
284-
* @param $path string 目标目录
285+
* @param string $path string 目标目录
285286
* @param array|string $ext array('css','html','php') css|html|php
286-
* @param $recursive int|bool 是否包含子目录
287+
* @param bool $recursive 是否包含子目录
287288
* @param array $list
288289
*
289290
* @return array
290291
* @throws InvalidArgumentException
291292
* @throws FileNotFoundException
292293
*/
293-
public static function getFilesInfo($path, $ext = null, $recursive = 0, &$list = []): array
294+
public static function getFilesInfo(string $path, $ext = null, bool $recursive = false, array &$list = []): array
294295
{
295296
$path = self::pathFormat($path);
296297

@@ -322,13 +323,13 @@ public static function getFilesInfo($path, $ext = null, $recursive = 0, &$list =
322323
/**
323324
* 支持层级目录的创建
324325
*
325-
* @param $path
326-
* @param int|string $mode
326+
* @param string $path
327+
* @param int $mode
327328
* @param bool $recursive
328329
*
329330
* @return bool
330331
*/
331-
public static function create($path, $mode = 0775, $recursive = true): bool
332+
public static function create(string $path, int $mode = 0775, bool $recursive = true): bool
332333
{
333334
return (is_dir($path) || !(!@mkdir($path, $mode, $recursive) && !is_dir($path))) && is_writable($path);
334335
}
@@ -342,16 +343,16 @@ public static function create($path, $mode = 0775, $recursive = true): bool
342343
* @return bool
343344
* @throws FileNotFoundException
344345
*/
345-
public static function copy($oldDir, $newDir): bool
346+
public static function copy(string $oldDir, string $newDir): bool
346347
{
347348
$oldDir = self::pathFormat($oldDir);
348349
$newDir = self::pathFormat($newDir);
349350

350351
if (!is_dir($oldDir)) {
351-
throw new FileNotFoundException('复制失败' . $oldDir . ' 不存在');
352+
throw new FileNotFoundException('copy failed' . $oldDir . ' does not exist');
352353
}
353354

354-
$newDir = self::create($newDir);
355+
self::create($newDir);
355356

356357
foreach (glob($oldDir . '*') as $v) {
357358
$newFile = $newDir . basename($v);//文件
@@ -375,12 +376,12 @@ public static function copy($oldDir, $newDir): bool
375376
/**
376377
* 删除目录及里面的文件
377378
*
378-
* @param $path
379+
* @param string $path
379380
* @param boolean $delSelf 默认最后删掉自己
380381
*
381382
* @return bool
382383
*/
383-
public static function delete($path, $delSelf = true): bool
384+
public static function delete(string $path, bool $delSelf = true): bool
384385
{
385386
$dirPath = self::pathFormat($path);
386387

‎src/File.php

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
use Toolkit\FsUtil\Exception\FileSystemException;
1717
use Toolkit\FsUtil\Exception\FileWriteException;
1818
use Toolkit\FsUtil\Exception\IOException;
19+
use Toolkit\FsUtil\Parser\IniParser;
20+
use Toolkit\FsUtil\Parser\JsonParser;
21+
use Toolkit\FsUtil\Parser\YmlParser;
22+
use Toolkit\FsUtil\Traits\FileSnippetReadTrait;
1923
use function dirname;
2024
use function file_get_contents;
2125
use function file_put_contents;
@@ -33,7 +37,7 @@
3337
*/
3438
abstract class File extends FileSystem
3539
{
36-
use FileReadTrait;
40+
use FileSnippetReadTrait;
3741

3842
public const FORMAT_JSON = 'json';
3943
public const FORMAT_PHP = 'php';
@@ -129,6 +133,100 @@ public static function getStat($filename): array
129133
return stat($filename);
130134
}
131135

136+
/**********************************************************************************
137+
* config file load
138+
*********************************************************************************/
139+
140+
/**
141+
* @param string $src 要解析的 文件 或 字符串内容。
142+
* @param string $format
143+
*
144+
* @return array|bool
145+
* @throws FileNotFoundException
146+
*/
147+
public static function load(string $src, string $format = self::FORMAT_PHP)
148+
{
149+
$src = trim($src);
150+
151+
switch ($format) {
152+
case self::FORMAT_YML:
153+
$array = self::loadYml($src);
154+
break;
155+
156+
case self::FORMAT_JSON:
157+
$array = self::loadJson($src);
158+
break;
159+
160+
case self::FORMAT_INI:
161+
$array = self::loadIni($src);
162+
break;
163+
164+
case self::FORMAT_PHP:
165+
default:
166+
$array = self::loadPhp($src);
167+
break;
168+
}
169+
170+
return $array;
171+
}
172+
173+
/**
174+
* load array data form file.
175+
*
176+
* @param string $file
177+
* @param bool $throwError
178+
*
179+
* @return array
180+
* @throws FileNotFoundException
181+
*/
182+
public static function loadPhp($file, $throwError = true): array
183+
{
184+
$ary = [];
185+
186+
if (is_file($file)) {
187+
/** @noinspection PhpIncludeInspection */
188+
$ary = require $file;
189+
190+
if (!is_array($ary)) {
191+
$ary = [];
192+
}
193+
} elseif ($throwError) {
194+
throw new FileNotFoundException("php file [$file] not exists.");
195+
}
196+
197+
return $ary;
198+
}
199+
200+
/**
201+
* @param string $file
202+
*
203+
* @return array
204+
*/
205+
public static function loadJson(string $file): array
206+
{
207+
return JsonParser::parse($file);
208+
}
209+
210+
/**
211+
* @param string $ini 要解析的 ini 文件名 或 字符串内容。
212+
*
213+
* @return array|bool
214+
*/
215+
public static function loadIni(string $ini)
216+
{
217+
return IniParser::parse($ini);
218+
}
219+
220+
/**
221+
* @param string $yml 要解析的 yml 文件名 或 字符串内容。
222+
*
223+
* @return array|bool
224+
*/
225+
public static function loadYml(string $yml)
226+
{
227+
return YmlParser::parse($yml);
228+
}
229+
132230
/**********************************************************************************
133231
* php function wrapper, add error handle
134232
*********************************************************************************/

0 commit comments

Comments
 (0)
Please sign in to comment.