piwik/ini

此包已被废弃且不再维护。作者建议使用 matomo/ini 包。

3.0.1 2022-11-09 08:19 UTC

This package is auto-updated.

Last update: 2023-05-01 18:40:13 UTC


README

读写INI配置。

Build Status Latest Version

安装

composer require matomo/ini

为什么?

PHP提供了一个parse_ini_file()函数来读取INI文件。

此组件相对于内置函数有以下优势

  • 允许写入INI文件
  • 类可以使用依赖注入并在单元测试中进行模拟
  • 抛出异常而不是PHP错误
  • 更好的类型支持
    • 将布尔值(true/falseon/offyes/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文件时将bool-ish值作为键。

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