matomo/ini

3.0.1 2022-11-09 08:19 UTC

This package is auto-updated.

Last update: 2024-08-30 01:26:07 UTC


README

读写INI配置。

Build Status Latest Version

安装

composer require matomo/ini

为什么?

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

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

  • 允许写入INI文件
  • 类可以用于依赖注入并在单元测试中模拟
  • 抛出异常而不是PHP错误
  • 更好的类型支持
  • 即使在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的Unknown中遇到意外的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