3.0.5 2022-06-04 09:34 UTC

This package is auto-updated.

Last update: 2024-09-04 14:24:13 UTC


README

读写INI配置。

Build Status Latest Version

安装

composer require panda843/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处未知中的意外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