Skip to content

Commit e8e525d

Browse files
committed
update some logic for file parse
1 parent 8ec97f0 commit e8e525d

File tree

7 files changed

+191
-190
lines changed

7 files changed

+191
-190
lines changed

src/File.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Toolkit\FsUtil\Exception\IOException;
1919
use Toolkit\FsUtil\Parser\IniParser;
2020
use Toolkit\FsUtil\Parser\JsonParser;
21-
use Toolkit\FsUtil\Parser\YmlParser;
21+
use Toolkit\FsUtil\Parser\YamlParser;
2222
use Toolkit\FsUtil\Traits\FileSnippetReadTrait;
2323
use function dirname;
2424
use function file_get_contents;
@@ -39,11 +39,11 @@ abstract class File extends FileSystem
3939
{
4040
use FileSnippetReadTrait;
4141

42-
public const FORMAT_JSON = 'json';
4342
public const FORMAT_PHP = 'php';
43+
public const FORMAT_JSON = 'json';
4444
public const FORMAT_INI = 'ini';
4545
public const FORMAT_YML = 'yml';
46-
public const FORMAT_YAML = 'yml';
46+
public const FORMAT_YAML = 'yaml';
4747

4848
/**
4949
* 获得文件名称
@@ -147,10 +147,10 @@ public static function getStat($filename): array
147147
public static function load(string $src, string $format = self::FORMAT_PHP)
148148
{
149149
$src = trim($src);
150-
151150
switch ($format) {
152151
case self::FORMAT_YML:
153-
$array = self::loadYml($src);
152+
case self::FORMAT_YAML:
153+
$array = self::loadYaml($src);
154154
break;
155155

156156
case self::FORMAT_JSON:
@@ -222,9 +222,9 @@ public static function loadIni(string $ini)
222222
*
223223
* @return array|bool
224224
*/
225-
public static function loadYml(string $yml)
225+
public static function loadYaml(string $yml)
226226
{
227-
return YmlParser::parse($yml);
227+
return YamlParser::parse($yml);
228228
}
229229

230230
/**********************************************************************************

src/FileSystem.php

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,15 @@
1010

1111
namespace Toolkit\FsUtil;
1212

13-
use FilesystemIterator;
1413
use InvalidArgumentException;
1514
use RecursiveCallbackFilterIterator;
1615
use RecursiveDirectoryIterator;
1716
use RecursiveIteratorIterator;
1817
use Toolkit\FsUtil\Exception\FileNotFoundException;
19-
use Toolkit\FsUtil\Exception\IOException;
2018
use Toolkit\FsUtil\Traits\FileSystemFuncTrait;
21-
use Toolkit\Stdlib\Arr;
2219
use Toolkit\Stdlib\OS;
23-
use Traversable;
2420
use function count;
2521
use function file_exists;
26-
use function function_exists;
2722
use function is_array;
2823
use function is_string;
2924
use function preg_match;
@@ -117,7 +112,7 @@ public function clearPharPath(string $path): string
117112
*
118113
* @return array|string
119114
*/
120-
public static function exists(string $file, $type = null)
115+
public static function exists(string $file, string $type = null)
121116
{
122117
if (!$type) {
123118
return file_exists($file);
@@ -160,102 +155,6 @@ public static function check(string $file, $ext = null): void
160155
}
161156
}
162157

163-
/**
164-
* Renames a file or a directory.
165-
*
166-
* @from Symfony-filesystem
167-
*
168-
* @param string $origin The origin filename or directory
169-
* @param string $target The new filename or directory
170-
* @param bool $overwrite Whether to overwrite the target if it already exists
171-
*
172-
* @throws IOException When target file or directory already exists
173-
* @throws IOException When origin cannot be renamed
174-
*/
175-
public static function rename(string $origin, string $target, bool $overwrite = false): void
176-
{
177-
// we check that target does not exist
178-
if (!$overwrite && static::isReadable($target)) {
179-
throw new IOException(sprintf('Cannot rename because the target "%s" already exists.', $target));
180-
}
181-
182-
if (true !== rename($origin, $target)) {
183-
throw new IOException(sprintf('Cannot rename "%s" to "%s".', $origin, $target));
184-
}
185-
}
186-
187-
/**
188-
* Tells whether a file exists and is readable.
189-
*
190-
* @from Symfony-filesystem
191-
*
192-
* @param string $filename Path to the file
193-
*
194-
* @return bool
195-
* @throws IOException When windows path is longer than 258 characters
196-
*/
197-
public static function isReadable(string $filename): bool
198-
{
199-
if ('\\' === DIRECTORY_SEPARATOR && strlen($filename) > 258) {
200-
throw new IOException('Could not check if file is readable because path length exceeds 258 characters.');
201-
}
202-
203-
return is_readable($filename);
204-
}
205-
206-
/**
207-
* Change mode for an array of files or directories.
208-
*
209-
* @from Symfony-filesystem
210-
*
211-
* @param string|array|Traversable $files A filename, an array of files, or a \Traversable instance to change mode
212-
* @param int $mode The new mode (octal)
213-
* @param int $umask The mode mask (octal)
214-
* @param bool $recursive Whether change the mod recursively or not
215-
*
216-
* @throws IOException When the change fail
217-
*/
218-
public static function chmod($files, $mode, $umask = 0000, $recursive = false): void
219-
{
220-
foreach (Arr::toIterator($files) as $file) {
221-
if (true !== @chmod($file, $mode & ~$umask)) {
222-
throw new IOException(sprintf('Failed to chmod file "%s".', $file));
223-
}
224-
225-
if ($recursive && is_dir($file) && !is_link($file)) {
226-
self::chmod(new FilesystemIterator($file), $mode, $umask, true);
227-
}
228-
}
229-
}
230-
231-
/**
232-
* Change the owner of an array of files or directories.
233-
*
234-
* @from Symfony-filesystem
235-
*
236-
* @param string|array|Traversable $files A filename, an array of files, or a \Traversable instance to change owner
237-
* @param string $user The new owner user name
238-
* @param bool $recursive Whether change the owner recursively or not
239-
*
240-
* @throws IOException When the change fail
241-
*/
242-
public static function chown($files, string $user, $recursive = false): void
243-
{
244-
foreach (Arr::toIterator($files) as $file) {
245-
if ($recursive && is_dir($file) && !is_link($file)) {
246-
self::chown(new FilesystemIterator($file), $user, true);
247-
}
248-
249-
if (is_link($file) && function_exists('lchown')) {
250-
if (true !== lchown($file, $user)) {
251-
throw new IOException(sprintf('Failed to chown file "%s".', $file));
252-
}
253-
} elseif (true !== chown($file, $user)) {
254-
throw new IOException(sprintf('Failed to chown file "%s".', $file));
255-
}
256-
}
257-
}
258-
259158
/**
260159
* @param string $srcDir
261160
* @param callable $filter

src/Parser/BaseParser.php renamed to src/Parser/AbstractParser.php

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @package Toolkit\FsUtil\Parser
2020
*/
21-
abstract class BaseParser
21+
abstract class AbstractParser
2222
{
2323
public const EXTEND_KEY = 'extend';
2424
public const IMPORT_KEY = 'import';
@@ -35,22 +35,26 @@ abstract class BaseParser
3535
* @return array
3636
*/
3737
abstract protected static function doParse(
38-
$string,
39-
$enhancement = false,
38+
string $string,
39+
bool $enhancement = false,
4040
callable $pathHandler = null,
4141
string $fileDir = ''
4242
): array;
4343

4444
/**
45-
* @param $string
45+
* @param string $string
4646
* @param bool $enhancement
4747
* @param callable|null $pathHandler
4848
* @param string $fileDir
4949
*
5050
* @return array
5151
*/
52-
public static function parse($string, $enhancement = false, callable $pathHandler = null, $fileDir = ''): array
53-
{
52+
public static function parse(
53+
string $string,
54+
bool $enhancement = false,
55+
callable $pathHandler = null,
56+
string $fileDir = ''
57+
): array {
5458
if (is_file($string)) {
5559
return self::parseFile($string, $enhancement, $pathHandler, $fileDir);
5660
}
@@ -59,24 +63,28 @@ public static function parse($string, $enhancement = false, callable $pathHandle
5963
}
6064

6165
/**
62-
* @param $file
66+
* @param string $file
6367
* @param bool $enhancement
6468
* @param callable|null $pathHandler
6569
* @param string $fileDir
6670
*
6771
* @return array
6872
* @throws InvalidArgumentException
6973
*/
70-
public static function parseFile($file, $enhancement = false, callable $pathHandler = null, $fileDir = ''): array
71-
{
74+
public static function parseFile(
75+
string $file,
76+
bool $enhancement = false,
77+
callable $pathHandler = null,
78+
$fileDir = ''
79+
): array {
7280
if (!is_file($file)) {
7381
throw new InvalidArgumentException("Target file [$file] not exists");
7482
}
7583

76-
$fileDir = $fileDir ?: dirname($file);
77-
$data = file_get_contents($file);
84+
$fileDir = $fileDir ?: dirname($file);
85+
$contents = file_get_contents($file);
7886

79-
return static::doParse($data, $enhancement, $pathHandler, $fileDir);
87+
return static::doParse($contents, $enhancement, $pathHandler, $fileDir);
8088
}
8189

8290
/**
@@ -88,11 +96,36 @@ public static function parseFile($file, $enhancement = false, callable $pathHand
8896
* @return array
8997
*/
9098
public static function parseString(
91-
$string,
92-
$enhancement = false,
99+
string $string,
100+
bool $enhancement = false,
93101
callable $pathHandler = null,
94-
$fileDir = ''
102+
string $fileDir = ''
95103
): array {
96104
return static::doParse($string, $enhancement, $pathHandler, $fileDir);
97105
}
106+
107+
/**
108+
* @param string $value
109+
* @param string $fileDir
110+
* @param callable|null $pathHandler
111+
*
112+
* @return string
113+
*/
114+
protected static function getImportFile(string $value, string $fileDir, $pathHandler = null): string
115+
{
116+
// eg: 'import#other.yaml'
117+
$importFile = trim(substr($value, 7));
118+
119+
// if needed custom handle $importFile path. e.g: Maybe it uses custom alias path
120+
if ($pathHandler && is_callable($pathHandler)) {
121+
$importFile = $pathHandler($importFile);
122+
}
123+
124+
// if $importFile is not exists AND $importFile is not a absolute path AND have $parentFile
125+
if ($fileDir && !file_exists($importFile) && $importFile[0] !== '/') {
126+
$importFile = $fileDir . '/' . trim($importFile, './');
127+
}
128+
129+
return $importFile;
130+
}
98131
}

src/Parser/IniParser.php

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@
1818
use function is_string;
1919
use function parse_ini_string;
2020
use function strpos;
21-
use function substr;
2221
use function trim;
2322

2423
/**
2524
* Class IniParser
2625
*
2726
* @package Toolkit\FsUtil\Parser
2827
*/
29-
class IniParser extends BaseParser
28+
class IniParser extends AbstractParser
3029
{
30+
public const INI = 'ini';
31+
3132
/**
3233
* parse INI
3334
*
@@ -41,21 +42,16 @@ class IniParser extends BaseParser
4142
* @throws UnexpectedValueException
4243
*/
4344
protected static function doParse(
44-
$string,
45-
$enhancement = false,
45+
string $string,
46+
bool $enhancement = false,
4647
callable $pathHandler = null,
4748
string $fileDir = ''
4849
): array {
4950
if (!$string) {
5051
return [];
5152
}
5253

53-
if (!is_string($string)) {
54-
throw new InvalidArgumentException('parameter type error! must is string.');
55-
}
56-
57-
/** @var array $array */
58-
$array = parse_ini_string(trim($string), true);
54+
$array = (array)parse_ini_string(trim($string), true);
5955

6056
/*
6157
* Parse special keywords
@@ -65,7 +61,7 @@ protected static function doParse(
6561
* [cache]
6662
* debug = reference#debug
6763
*/
68-
if ($enhancement === true) {
64+
if ($enhancement) {
6965
if (isset($array[self::EXTEND_KEY]) && ($extendFile = $array[self::EXTEND_KEY])) {
7066
// if needed custom handle $importFile path. e.g: Maybe it uses custom alias path
7167
if ($pathHandler && is_callable($pathHandler)) {
@@ -79,8 +75,9 @@ protected static function doParse(
7975

8076
// $importFile is file
8177
if (is_file($extendFile)) {
82-
$data = file_get_contents($extendFile);
83-
$array = array_merge(parse_ini_string(trim($data), true), $array);
78+
$contents = file_get_contents($extendFile);
79+
// merge data
80+
$array = array_merge(parse_ini_string(trim($contents), true), $array);
8481
} else {
8582
throw new UnexpectedValueException("needed extended file [$extendFile] don't exists!");
8683
}
@@ -92,22 +89,13 @@ protected static function doParse(
9289
}
9390

9491
if (0 === strpos($item, self::IMPORT_KEY . '#')) {
95-
$importFile = trim(substr($item, 7));
96-
97-
// if needed custom handle $importFile path. e.g: Maybe it uses custom alias path
98-
if ($pathHandler && is_callable($pathHandler)) {
99-
$importFile = $pathHandler($importFile);
100-
}
101-
102-
// if $importFile is not exists AND $importFile is not a absolute path AND have $parentFile
103-
if ($fileDir && !file_exists($importFile) && $importFile[0] !== '/') {
104-
$importFile = $fileDir . '/' . trim($importFile, './');
105-
}
92+
$importFile = self::getImportFile($item, $fileDir, $pathHandler);
10693

10794
// $importFile is file
10895
if (is_file($importFile)) {
109-
$data = file_get_contents($importFile);
110-
$array[$key] = parse_ini_string(trim($data), true);
96+
$contents = file_get_contents($importFile);
97+
98+
$array[$key] = parse_ini_string(trim($contents), true);
11199
} else {
112100
throw new UnexpectedValueException("needed imported file [$importFile] don't exists!");
113101
}

0 commit comments

Comments
 (0)