Skip to content

Commit

Permalink
some update for file-parse lib
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 30, 2018
1 parent 8b26550 commit abee6dd
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 56 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ parse ini,json,yml file.
## install

```bash
composer require mylib/file-parse
composer require toolkit/file-parse
```

## license
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": [
{
Expand All @@ -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": {
Expand Down
10 changes: 5 additions & 5 deletions src/BaseParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}
Expand Down
26 changes: 13 additions & 13 deletions src/IniParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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!");
}
Expand All @@ -71,23 +71,23 @@ 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)) {
$importFile = $pathHandler($importFile);
}

// 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!");
}
Expand Down
24 changes: 12 additions & 12 deletions src/JsonParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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!");
}
Expand All @@ -73,22 +73,22 @@ 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)) {
$importFile = $pathHandler($importFile);
}

// 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!");
Expand Down
24 changes: 12 additions & 12 deletions src/YmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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'.");
}

Expand All @@ -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!");
}
Expand All @@ -78,22 +78,22 @@ 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)) {
$importFile = $pathHandler($importFile);
}

// 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!");
Expand Down
6 changes: 3 additions & 3 deletions test/IniParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
12 changes: 6 additions & 6 deletions test/boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down

0 comments on commit abee6dd

Please sign in to comment.