uspilot/ini_parser

PHP .INI 文件解析器

v1.0.0 2020-10-13 20:24 UTC

This package is auto-updated.

Last update: 2024-09-30 01:54:20 UTC


README

Build Status

IniParser 是一个简单的复杂 INI 文件解析器,为内置的 INI 解析函数提供了许多额外的语法特性,包括部分继承、属性嵌套和数组字面量。

重要: IniParser 应该被视为测试版质量,可能仍然存在错误。请随意打开一个问题或提交一个拉取请求,我会查看!

通过 Composer 安装

将您的 composer.json 文件设置为具有

{
	"require": {
		"uspilot/ini_parser": "dev-master"
	}
}

然后安装依赖项

composer install

或者直接使用

composer require uspilot/ini_parser

示例

标准 INI 文件看起来像这样

key = value
another_key = another value

[section_name]
a_sub_key = yet another value

当使用 PHP 内置的 parse_ini_string()parse_ini_file() 解析时,看起来像这样

array(
    'key' => 'value',
    'another_key' => 'another value',
    'section_name' => array(
        'a_sub_key' => 'yet another value'
    )
);

当你只想有一个简单的配置文件时,这是很棒的,但这里有一个超级增强的 INI 文件,你可能在野外找到

environment = testing

[testing]
debug = true
database.connection = "mysql:host=127.0.0.1"
database.name = test
database.username = 
database.password =
secrets = [1,2,3]

[staging : testing]
database.name = stage
database.username = staging
database.password = 12345

[production : staging]
debug = false;
database.name = production
database.username = root

当使用 IniParser 解析时

$parser = new \IniParser('sample.ini');
$config = $parser->parse();

你会得到以下结构

array(
    'environment' => 'testing',
    'testing' => array(
        'debug' => '1',
        'database' => array(
            'connection' => 'mysql:host=127.0.0.1',
            'name' => 'test',
            'username' => '',
            'password' => ''
        ),
        'secrets' => array('1','2','3')
    ),
    'staging' => array(
        'debug' => '1',
        'database' => array(
            'connection' => 'mysql:host=127.0.0.1',
            'name' => 'stage',
            'username' => 'staging',
            'password' => '12345'
        ),
       'secrets' => array('1','2','3')
    ),
    'production' => array(
        'debug' => '',
        'database' => array(
            'connection' => 'mysql:host=127.0.0.1',
            'name' => 'production',
            'username' => 'root',
            'password' => '12345'
        ),
        'secrets' => array('1','2','3')
    )
);

支持的功能

数组字面量

您可以直接使用赋值右侧的语法 [a, b, c] 创建数组。例如

colors = [blue, green, red]

注意: 目前,数组字面量内的引号字符串具有未定义的行为。

字典和复杂结构

除了数组之外,您还可以使用 JSON 语法创建字典和更复杂的结构。例如,您可以使用

 people = '{
    "boss": {
       "name": "John", 
       "age": 42 
    }, 
    "staff": [
       {
          "name": "Mark",
          "age": 35 
       }, 
       {
          "name": "Bill", 
          "age": 44 
       }
    ] 
 }'

这会变成一个类似下面的数组

array(
    'boss' => array(
        'name' => 'John',
        'age' => 42
    ),
    'staff' => array(
        array (
            'name' => 'Mark',
            'age' => 35,
        ),
        array (
            'name' => 'Bill',
            'age' => 44,
        ),
    ),
);

注意: 记住用单引号将 JSON 字符串括起来以正确分析。JSON 名称必须用双引号括起来,不允许有尾随逗号。

属性嵌套

IniParser 允许您将属性视为关联数组

person.age = 42
person.name.first = John
person.name.last = Doe

这会变成一个类似下面的数组

array (
    'person' => array (
        'age' => 42,
        'name' => array (
            'first' => 'John',
            'last' => 'Doe'
        )
    )
);

部分继承

遵循 DRY 原则,IniParser 允许您从其他部分“继承”(类似于面向对象继承),这意味着您不必反复重新定义相同的属性。如上例所示,“production”从“staging”继承,而“staging”又从“testing”继承。

您甚至可以从多个父级继承,如 [child : p1 : p2 : p3]。每个父级的属性按从左到右的顺序合并到子级中,因此 p1 的属性会被 p2 的属性覆盖,然后被 p3 的属性覆盖,最后再被 child 上的属性覆盖。

在继承过程中,如果键以 + 结尾,合并行为会从覆盖父级值变为添加父级值(或添加子级值 - 相同的东西)。因此,以下示例文件

[parent]
arr = [a,b,c]
val = foo

[child : parent]
arr += [x,y,z]
val += bar

将被解析为以下内容

array(
    'parent' => array(
        'arr' => array('a','b','c'),
        'val' => 'foo'
    ),
    'child' => array(
        'arr' => array('a','b','c','x','y','z'),
        'val' => 'foobar'
    )
);

如果您能想到比连接对于非数组类型更有用的操作,请打开一个问题

最后,您甚至可以从特殊的 ^ 部分继承,代表顶层或全局属性

foo = bar

[sect : ^]

解析为

array (
    'foo' => 'bar',
    'sect' => array (
        'foo' => 'bar'
    )
);

ArrayObject

作为额外的好处,IniParser 还允许您以面向对象的方式访问值

echo $config->production->database->connection; // output: mysql:host=127.0.0.1
echo $config->staging->debug; // output: 1