From abee6ddd0d9c9104722b147f10d33902a63af157 Mon Sep 17 00:00:00 2001 From: inhere Date: Mon, 30 Apr 2018 16:03:11 +0800 Subject: [PATCH] some update for file-parse lib --- README.md | 2 +- composer.json | 8 ++++---- src/BaseParser.php | 10 +++++----- src/IniParser.php | 26 +++++++++++++------------- src/JsonParser.php | 24 ++++++++++++------------ src/YmlParser.php | 24 ++++++++++++------------ test/IniParserTest.php | 6 +++--- test/boot.php | 12 ++++++------ 8 files changed, 56 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index e0e1fc0..10cbb13 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ parse ini,json,yml file. ## install ```bash -composer require mylib/file-parse +composer require toolkit/file-parse ``` ## license diff --git a/composer.json b/composer.json index 1f255c3..6957046 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,9 @@ { - "name": "mylib/file-parse", + "name": "toolkit/file-parse", "type": "library", "description": "some file parse tool library of the php", "keywords": ["library","tool","php"], - "homepage": "https://github.com/php-mylib/file-parse", + "homepage": "https://github.com/php-toolkit/file-parse", "license": "MIT", "authors": [ { @@ -14,11 +14,11 @@ ], "require": { "php": ">=7.0.0", - "mylib/str-utils": "~1.0" + "toolkit/str-utils": "~1.0" }, "autoload": { "psr-4": { - "MyLib\\FileParse\\" : "src/" + "Toolkit\\File\\Parse\\" : "src/" } }, "suggest": { diff --git a/src/BaseParser.php b/src/BaseParser.php index b129758..430b884 100644 --- a/src/BaseParser.php +++ b/src/BaseParser.php @@ -6,11 +6,11 @@ * Time: 上午11:43 */ -namespace MyLib\FileParse; +namespace Toolkit\File\Parse; /** * Class BaseParser - * @package MyLib\FileParse + * @package Toolkit\File\Parse */ abstract class BaseParser { @@ -42,7 +42,7 @@ abstract protected static function doParse( */ public static function parse($string, $enhancement = false, callable $pathHandler = null, $fileDir = ''): array { - if (is_file($string)) { + if (\is_file($string)) { return self::parseFile($string, $enhancement, $pathHandler, $fileDir); } @@ -59,12 +59,12 @@ public static function parse($string, $enhancement = false, callable $pathHandle */ public static function parseFile($file, $enhancement = false, callable $pathHandler = null, $fileDir = ''): array { - if (!is_file($file)) { + if (!\is_file($file)) { throw new \InvalidArgumentException("Target file [$file] not exists"); } $fileDir = $fileDir ?: \dirname($file); - $data = file_get_contents($file); + $data = \file_get_contents($file); return static::doParse($data, $enhancement, $pathHandler, $fileDir); } diff --git a/src/IniParser.php b/src/IniParser.php index 0da94bc..2a3968c 100644 --- a/src/IniParser.php +++ b/src/IniParser.php @@ -6,11 +6,11 @@ * Time: 上午11:41 */ -namespace MyLib\FileParse; +namespace Toolkit\File\Parse; /** * Class IniParser - * @package MyLib\FileParse + * @package Toolkit\File\Parse */ class IniParser extends BaseParser { @@ -53,14 +53,14 @@ protected static function doParse($string, $enhancement = false, callable $pathH } // if $importFile is not exists AND $importFile is not a absolute path AND have $parentFile - if ($fileDir && !file_exists($extendFile) && $extendFile[0] !== '/') { - $extendFile = $fileDir . '/' . trim($extendFile, './'); + if ($fileDir && !\file_exists($extendFile) && $extendFile[0] !== '/') { + $extendFile = $fileDir . '/' . \trim($extendFile, './'); } // $importFile is file - if (is_file($extendFile)) { - $data = file_get_contents($extendFile); - $array = array_merge(parse_ini_string(trim($data), true), $array); + if (\is_file($extendFile)) { + $data = \file_get_contents($extendFile); + $array = \array_merge(parse_ini_string(trim($data), true), $array); } else { throw new \UnexpectedValueException("needed extended file [$extendFile] don't exists!"); } @@ -71,8 +71,8 @@ protected static function doParse($string, $enhancement = false, callable $pathH continue; } - if (0 === strpos($item, self::IMPORT_KEY . '#')) { - $importFile = trim(substr($item, 7)); + if (0 === \strpos($item, self::IMPORT_KEY . '#')) { + $importFile = trim(\substr($item, 7)); // if needed custom handle $importFile path. e.g: Maybe it uses custom alias path if ($pathHandler && \is_callable($pathHandler)) { @@ -80,14 +80,14 @@ protected static function doParse($string, $enhancement = false, callable $pathH } // if $importFile is not exists AND $importFile is not a absolute path AND have $parentFile - if ($fileDir && !file_exists($importFile) && $importFile[0] !== '/') { + if ($fileDir && !\file_exists($importFile) && $importFile[0] !== '/') { $importFile = $fileDir . '/' . trim($importFile, './'); } // $importFile is file - if (is_file($importFile)) { - $data = file_get_contents($importFile); - $array[$key] = parse_ini_string(trim($data), true); + if (\is_file($importFile)) { + $data = \file_get_contents($importFile); + $array[$key] = \parse_ini_string(trim($data), true); } else { throw new \UnexpectedValueException("needed imported file [$importFile] don't exists!"); } diff --git a/src/JsonParser.php b/src/JsonParser.php index c022884..8694587 100644 --- a/src/JsonParser.php +++ b/src/JsonParser.php @@ -6,13 +6,13 @@ * Time: 上午11:41 */ -namespace MyLib\FileParse; +namespace Toolkit\File\Parse; -use MyLib\StrUtil\JsonHelper; +use Toolkit\StrUtil\JsonHelper; /** * Class JsonParser - * @package MyLib\FileParse + * @package Toolkit\File\Parse */ class JsonParser extends BaseParser { @@ -55,14 +55,14 @@ protected static function doParse($string, $enhancement = false, callable $pathH } // if $importFile is not exists AND $importFile is not a absolute path AND have $parentFile - if ($fileDir && !file_exists($extendFile) && $extendFile[0] !== '/') { + if ($fileDir && !\file_exists($extendFile) && $extendFile[0] !== '/') { $extendFile = $fileDir . '/' . trim($extendFile, './'); } // $importFile is file - if (is_file($extendFile)) { - $data = file_get_contents($extendFile); - $array = array_merge(JsonHelper::parse($data), $array); + if (\is_file($extendFile)) { + $data = \file_get_contents($extendFile); + $array = \array_merge(JsonHelper::parse($data), $array); } else { throw new \UnexpectedValueException("needed extended file [$extendFile] don't exists!"); } @@ -73,8 +73,8 @@ protected static function doParse($string, $enhancement = false, callable $pathH continue; } - if (0 === strpos($item, self::IMPORT_KEY . '#')) { - $importFile = trim(substr($item, 6)); + if (0 === \strpos($item, self::IMPORT_KEY . '#')) { + $importFile = \trim(\substr($item, 6)); // if needed custom handle $importFile path. e.g: Maybe it uses custom alias path if ($pathHandler && \is_callable($pathHandler)) { @@ -82,13 +82,13 @@ protected static function doParse($string, $enhancement = false, callable $pathH } // if $importFile is not exists AND $importFile is not a absolute path AND have $parentFile - if ($fileDir && !file_exists($importFile) && $importFile[0] !== '/') { + if ($fileDir && !\file_exists($importFile) && $importFile[0] !== '/') { $importFile = $fileDir . '/' . trim($importFile, './'); } // $importFile is file - if (is_file($importFile)) { - $data = file_get_contents($importFile); + if (\is_file($importFile)) { + $data = \file_get_contents($importFile); $array[$key] = JsonHelper::parse($data); } else { throw new \UnexpectedValueException("needed imported file [$importFile] don't exists!"); diff --git a/src/YmlParser.php b/src/YmlParser.php index 6cb730b..518a0e8 100644 --- a/src/YmlParser.php +++ b/src/YmlParser.php @@ -6,13 +6,13 @@ * Time: 上午11:41 */ -namespace MyLib\FileParse; +namespace Toolkit\File\Parse; use Symfony\Component\Yaml\Parser; /** * Class YmlParser - * @package MyLib\FileParse + * @package Toolkit\File\Parse */ class YmlParser extends BaseParser { @@ -36,7 +36,7 @@ protected static function doParse($string, $enhancement = false, callable $pathH throw new \InvalidArgumentException('param type error! must is string.'); } - if (!class_exists(Parser::class)) { + if (!\class_exists(Parser::class)) { throw new \UnexpectedValueException('yml format parser Class ' . Parser::class . " don't exists! please install package 'symfony/yaml'."); } @@ -60,14 +60,14 @@ protected static function doParse($string, $enhancement = false, callable $pathH } // if $importFile is not exists AND $importFile is not a absolute path AND have $parentFile - if ($fileDir && !file_exists($extendFile) && $extendFile[0] !== '/') { + if ($fileDir && !\file_exists($extendFile) && $extendFile[0] !== '/') { $extendFile = $fileDir . '/' . trim($extendFile, './'); } // $importFile is file - if (is_file($extendFile)) { - $data = file_get_contents($extendFile); - $array = array_merge($parser->parse(trim($data)), $array); + if (\is_file($extendFile)) { + $data = \file_get_contents($extendFile); + $array = \array_merge($parser->parse(trim($data)), $array); } else { throw new \UnexpectedValueException("needed extended file $extendFile don't exists!"); } @@ -78,8 +78,8 @@ protected static function doParse($string, $enhancement = false, callable $pathH continue; } - if (0 === strpos($item, self::IMPORT_KEY . '#')) { - $importFile = trim(substr($item, 6)); + if (0 === \strpos($item, self::IMPORT_KEY . '#')) { + $importFile = \trim(\substr($item, 6)); // if needed custom handle $importFile path. e.g: Maybe it uses custom alias path if ($pathHandler && \is_callable($pathHandler)) { @@ -87,13 +87,13 @@ protected static function doParse($string, $enhancement = false, callable $pathH } // if $importFile is not exists AND $importFile is not a absolute path AND have $parentFile - if ($fileDir && !file_exists($importFile) && $importFile[0] !== '/') { + if ($fileDir && !\file_exists($importFile) && $importFile[0] !== '/') { $importFile = $fileDir . '/' . trim($importFile, './'); } // $importFile is file - if (is_file($importFile)) { - $data = file_get_contents($importFile); + if (\is_file($importFile)) { + $data = \file_get_contents($importFile); $array[$key] = $parser->parse(trim($data)); } else { throw new \UnexpectedValueException("needed imported file $importFile don't exists!"); diff --git a/test/IniParserTest.php b/test/IniParserTest.php index 4ef491a..40bb0ac 100644 --- a/test/IniParserTest.php +++ b/test/IniParserTest.php @@ -6,14 +6,14 @@ * Time: 20:25 */ -namespace MyLib\FileParse\Test; +namespace Toolkit\File\Parse\Test; -use MyLib\FileParse\IniParser; +use Toolkit\File\Parse\IniParser; use PHPUnit\Framework\TestCase; /** * Class IniParserTest - * @package MyLib\FileParse\Test + * @package Toolkit\File\Parse\Test */ class IniParserTest extends TestCase { diff --git a/test/boot.php b/test/boot.php index a17fe0a..f4e82a2 100644 --- a/test/boot.php +++ b/test/boot.php @@ -10,14 +10,14 @@ spl_autoload_register(function ($class) { $file = null; - if (0 === strpos($class,'MyLib\FileParse\Example\\')) { - $path = str_replace('\\', '/', substr($class, strlen('MyLib\FileParse\Example\\'))); + if (0 === strpos($class,'Toolkit\File\Parse\Example\\')) { + $path = str_replace('\\', '/', substr($class, strlen('Toolkit\File\Parse\Example\\'))); $file = dirname(__DIR__) . "/example/{$path}.php"; - } elseif (0 === strpos($class,'MyLib\FileParse\Test\\')) { - $path = str_replace('\\', '/', substr($class, strlen('MyLib\FileParse\Test\\'))); + } elseif (0 === strpos($class,'Toolkit\File\Parse\Test\\')) { + $path = str_replace('\\', '/', substr($class, strlen('Toolkit\File\Parse\Test\\'))); $file = __DIR__ . "/{$path}.php"; - } elseif (0 === strpos($class,'MyLib\FileParse\\')) { - $path = str_replace('\\', '/', substr($class, strlen('MyLib\FileParse\\'))); + } elseif (0 === strpos($class,'Toolkit\File\Parse\\')) { + $path = str_replace('\\', '/', substr($class, strlen('Toolkit\File\Parse\\'))); $file = dirname(__DIR__) . "/src/{$path}.php"; }