tajawal/php-ini-parser
最新稳定版本:2.0.2
Composer 安装命令:
composer require tajawal/php-ini-parser
包简介
A Zend_Config_Ini like parser for .ini files.
关键字:
README 文档
README
IniParser is a simple parser for complex INI files, providing a number of extra syntactic features to the built-in INI parsing functions, including section inheritance, property nesting, and array literals.
Installing by Composer
Set your composer.json file to have :
{
"require": {
"tajawal/php-ini-parser": "dev-master"
}
}
Then install the dependencies :
composer install
An Example
Standard INI files look like this:
key = value
another_key = another value
[section_name]
a_sub_key = yet another value
And when parsed with PHP's built-in parse_ini_string() or parse_ini_file(), looks like
array( 'key' => 'value', 'another_key' => 'another value', 'section_name' => array( 'a_sub_key' => 'yet another value' ) )
This is great when you just want a simple configuration file, but here is a super-charged INI file that you might find in the wild:
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
And when parsed with IniParser:
$parser = new \IniParser('sample.ini');
$config = $parser->parse();
You get the following structure:
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') ) )
Supported Features
Array Literals
You can directly create arrays using the syntax [a, b, c] on the right hand side of an assignment. For example:
colors = [blue, green, red]
NOTE: At the moment, quoted strings inside array literals have undefined behavior.
Dictionaries and complex structures
Besides arrays, you can create dictionaries and more complex structures using JSON syntax. For example, you can use:
people = '{
"boss": {
"name": "John",
"age": 42
},
"staff": [
{
"name": "Mark",
"age": 35
},
{
"name": "Bill",
"age": 44
}
]
}'
This turns into an array like:
array( 'boss' => array( 'name' => 'John', 'age' => 42 ), 'staff' => array( array ( 'name' => 'Mark', 'age' => 35, ), array ( 'name' => 'Bill', 'age' => 44, ), ), )
NOTE: Remember to wrap the JSON strings in single quotes for a correct analysis. The JSON names must be enclosed in double quotes and trailing commas are not allowed.
Property Nesting
IniParser allows you to treat properties as associative arrays:
person.age = 42
person.name.first = John
person.name.last = Doe
This turns into an array like:
array ( 'person' => array ( 'age' => 42, 'name' => array ( 'first' => 'John', 'last' => 'Doe' ) ) )
Section Inheritance
Keeping to the DRY principle, IniParser allows you to "inherit" from other sections (similar to OOP inheritance), meaning you don't have to continually re-define the same properties over and over again. As you can see in the example above, "production" inherits from "staging", which in turn inherits from "testing".
You can even inherit from multiple parents, as in [child : p1 : p2 : p3]. The properties of each parent are merged into the child from left to right, so that the properties in p1 are overridden by those in p2, then by p3, then by those in child on top of that.
During the inheritance process, if a key ends in a +, the merge behavior changes from overwriting the parent value to prepending the parent value (or appending the child value - same thing). So the example file
[parent]
arr = [a,b,c]
val = foo
[child : parent]
arr += [x,y,z]
val += bar
would be parsed into the following:
array( 'parent' => array( 'arr' => array('a','b','c'), 'val' => 'foo' ), 'child' => array( 'arr' => array('a','b','c','x','y','z'), 'val' => 'foobar' ) )
If you can think of a more useful operation than concatenation for non-array types, please open an issue
Finally, it is possible to inherit from the special ^ section, representing the top-level or global properties:
foo = bar
[sect : ^]
Parses to:
array ( 'foo' => 'bar', 'sect' => array ( 'foo' => 'bar' ) )
ArrayObject
As an added bonus, IniParser also allows you to access the values OO-style:
echo $config->production->database->connection; // output: mysql:host=127.0.0.1 echo $config->staging->debug; // output: 1
tajawal/php-ini-parser 适用场景与选型建议
tajawal/php-ini-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.99k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2017 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「configuration」 「ini」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tajawal/php-ini-parser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tajawal/php-ini-parser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tajawal/php-ini-parser 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Configuration component
Framework Agnostic Configuration Library
A Zend Framework module to quickly and easily set PHP settings.
The slick/configuration component provides an easy-to-use solution for managing configuration files in your PHP applications.
Load configuration files the easy way. This configuration loader supports PHP, INI, JSON, XML, YAML and NEON file extensions.
Utils to load, parse and work with configuration on Mezzio projects
统计信息
- 总下载量: 2.99k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-02-16