panda843 / ini
3.0.5
2022-06-04 09:34 UTC
Requires
- php: >=7.2
Requires (Dev)
- phpbench/phpbench: ^1.0
- phpunit/phpunit: ^8.5
README
读写INI配置。
安装
composer require panda843/ini
为什么?
PHP提供了一个parse_ini_file()
函数来读取INI文件。
该组件相对于内置函数有以下优点
- 允许写入INI文件
- 类可以与依赖注入一起使用,并在单元测试中进行模拟
- 抛出异常而不是PHP错误
- 更好的类型支持
- 解析布尔值(
true
/false
,on
/off
,yes
/no
)为实际的PHP布尔值(而不是字符串"1"
和""
) - 解析null为PHP
null
(而不是空字符串)
- 解析布尔值(
- 即使在
php.ini
中禁用了parse_ini_file()
或parse_ini_string()
,也能正常工作(可能在某些共享主机上发生)
用法
读取
$reader = new IniReader(); // Read a string $array = $reader->readString($string); // Read a file $array = $reader->readFile('config.ini');
故障排除
在行X处未知中的意外BOOL_TRUE
PHP默认的read_ini_file实现不允许在读取ini文件时将布尔值作为键。
如yes = "Yes"
这样的数据会导致以下错误
Syntax error in INI configuration: syntax error, unexpected BOOL_TRUE in Unknown on line 6
为了避免该错误,请通过使用以下方式切换到自定义ini读取器实现
$reader = new IniReader(); $reader->setUseNativeFunction(false);
写入
$writer = new IniWriter(); // Write to a string $string = $writer->writeToString($array); // Write to a file $writer->writeToFile('config.ini', $array);
许可证
Ini组件以LGPL v3.0发布。
贡献
运行单元测试
vendor/bin/phpunit
运行性能测试
php vendor/bin/phpbench run tests/PerformanceTest --report=default