Skip to content

Commit 6758a63

Browse files
committed
init from inhere/librarys
0 parents  commit 6758a63

File tree

11 files changed

+529
-0
lines changed

11 files changed

+529
-0
lines changed

.editorconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
root = true
2+
3+
# 对所有文件生效
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
12+
# 对后缀名为 md 的文件生效
13+
[*.md]
14+
trim_trailing_whitespace = false
15+
16+
[*.php]
17+
indent_size = 4

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.idea/
2+
.phpintel/
3+
!README.md
4+
!.gitkeep
5+
composer.lock
6+
*.swp
7+
*.log
8+
*.pid
9+
*.patch
10+
.DS_Store

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 inhere
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# file-parse for php
2+
3+
4+
## license
5+
6+
MIT

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "mylib/file-parse",
3+
"type": "library",
4+
"description": "some file parse tool library of the php",
5+
"keywords": ["library","tool","php"],
6+
"homepage": "https://github.com/php-mylib/file-parse",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "inhere",
11+
"email": "[email protected]",
12+
"homepage": "http://www.yzone.net/"
13+
}
14+
],
15+
"require": {
16+
"php": ">=7.0.0"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"MyLib\\FileParse\\" : "src/"
21+
}
22+
},
23+
"suggest": {
24+
"inhere/simple-print-tool": "Very lightweight data printing tools",
25+
"inhere/php-validate": "Very lightweight data validate tool",
26+
"inhere/console": "a lightweight php console application library."
27+
}
28+
}

phpunit.xml.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
bootstrap="test/boot.php"
6+
colors="false"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Php Library Test Suite">
15+
<directory>test</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
<filter>
20+
<whitelist>
21+
<directory suffix=".php">src</directory>
22+
</whitelist>
23+
</filter>
24+
</phpunit>

src/BaseParser.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017/6/17
6+
* Time: 上午11:43
7+
*/
8+
9+
namespace MyLib\FileParse;
10+
11+
/**
12+
* Class BaseParser
13+
* @package MyLib\FileParse
14+
*/
15+
abstract class BaseParser
16+
{
17+
const EXTEND_KEY = 'extend';
18+
const IMPORT_KEY = 'import';
19+
const REFERENCE_KEY = 'reference';
20+
21+
/**
22+
* parse data
23+
* @param string $string Waiting for the parse data
24+
* @param bool $enhancement 启用增强功能,支持通过关键字 继承、导入、参考
25+
* @param callable $pathHandler When the second param is true, this param is valid.
26+
* @param string $fileDir When the second param is true, this param is valid.
27+
* @return array
28+
*/
29+
abstract protected static function doParse(
30+
$string,
31+
$enhancement = false,
32+
callable $pathHandler = null,
33+
$fileDir = ''
34+
);
35+
36+
/**
37+
* @param $string
38+
* @param bool $enhancement
39+
* @param callable|null $pathHandler
40+
* @param string $fileDir
41+
* @return array
42+
*/
43+
public static function parse($string, $enhancement = false, callable $pathHandler = null, $fileDir = '')
44+
{
45+
if (is_file($string)) {
46+
return self::parseFile($string, $enhancement, $pathHandler, $fileDir);
47+
}
48+
49+
return static::doParse($string, $enhancement, $pathHandler, $fileDir);
50+
}
51+
52+
/**
53+
* @param $file
54+
* @param bool $enhancement
55+
* @param callable|null $pathHandler
56+
* @param string $fileDir
57+
* @return array
58+
* @throws \InvalidArgumentException
59+
*/
60+
public static function parseFile($file, $enhancement = false, callable $pathHandler = null, $fileDir = '')
61+
{
62+
if (!is_file($file)) {
63+
throw new \InvalidArgumentException("Target file [$file] not exists");
64+
}
65+
66+
$fileDir = $fileDir ?: \dirname($file);
67+
$data = file_get_contents($file);
68+
69+
return static::doParse($data, $enhancement, $pathHandler, $fileDir);
70+
}
71+
72+
/**
73+
* @param $string
74+
* @param bool $enhancement
75+
* @param callable|null $pathHandler
76+
* @param string $fileDir
77+
* @return array
78+
*/
79+
public static function parseString($string, $enhancement = false, callable $pathHandler = null, $fileDir = '')
80+
{
81+
return static::doParse($string, $enhancement, $pathHandler, $fileDir);
82+
}
83+
}

src/IniParser.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017/6/17
6+
* Time: 上午11:41
7+
*/
8+
9+
namespace MyLib\FileParse;
10+
11+
/**
12+
* Class IniParser
13+
* @package MyLib\FileParse
14+
*/
15+
class IniParser extends BaseParser
16+
{
17+
/**
18+
* parse INI
19+
* @param string $string Waiting for the parse data
20+
* @param bool $enhancement 启用增强功能,支持通过关键字 继承、导入、参考
21+
* @param callable $pathHandler When the second param is true, this param is valid.
22+
* @param string $fileDir When the second param is true, this param is valid.
23+
* @return array
24+
* @throws \InvalidArgumentException
25+
* @throws \UnexpectedValueException
26+
*/
27+
protected static function doParse($string, $enhancement = false, callable $pathHandler = null, $fileDir = '')
28+
{
29+
if (!$string) {
30+
return [];
31+
}
32+
33+
if (!\is_string($string)) {
34+
throw new \InvalidArgumentException('parameter type error! must is string.');
35+
}
36+
37+
/** @var array $array */
38+
$array = parse_ini_string(trim($string), true);
39+
40+
/*
41+
* Parse special keywords
42+
*
43+
* extend = ../parent.yml
44+
* db = import#../db.yml
45+
* [cache]
46+
* debug = reference#debug
47+
*/
48+
if ($enhancement === true) {
49+
if (isset($array[self::EXTEND_KEY]) && ($extendFile = $array[self::EXTEND_KEY])) {
50+
// if needed custom handle $importFile path. e.g: Maybe it uses custom alias path
51+
if ($pathHandler && \is_callable($pathHandler)) {
52+
$extendFile = $pathHandler($extendFile);
53+
}
54+
55+
// if $importFile is not exists AND $importFile is not a absolute path AND have $parentFile
56+
if ($fileDir && !file_exists($extendFile) && $extendFile[0] !== '/') {
57+
$extendFile = $fileDir . '/' . trim($extendFile, './');
58+
}
59+
60+
// $importFile is file
61+
if (is_file($extendFile)) {
62+
$data = file_get_contents($extendFile);
63+
$array = array_merge(parse_ini_string(trim($data), true), $array);
64+
} else {
65+
throw new \UnexpectedValueException("needed extended file [$extendFile] don't exists!");
66+
}
67+
}
68+
69+
foreach ($array as $key => $item) {
70+
if (!\is_string($item)) {
71+
continue;
72+
}
73+
74+
if (0 === strpos($item, self::IMPORT_KEY . '#')) {
75+
$importFile = trim(substr($item, 6));
76+
77+
// if needed custom handle $importFile path. e.g: Maybe it uses custom alias path
78+
if ($pathHandler && \is_callable($pathHandler)) {
79+
$importFile = $pathHandler($importFile);
80+
}
81+
82+
// if $importFile is not exists AND $importFile is not a absolute path AND have $parentFile
83+
if ($fileDir && !file_exists($importFile) && $importFile[0] !== '/') {
84+
$importFile = $fileDir . '/' . trim($importFile, './');
85+
}
86+
87+
// $importFile is file
88+
if (is_file($importFile)) {
89+
$data = file_get_contents($importFile);
90+
$array[$key] = parse_ini_string(trim($data), true);
91+
} else {
92+
throw new \UnexpectedValueException("needed imported file [$importFile] don't exists!");
93+
}
94+
}
95+
}
96+
97+
}
98+
99+
return $array;
100+
}
101+
102+
}

0 commit comments

Comments
 (0)