comphp / config-ini
提供处理INI配置文件所需的功能
v0.1
2024-02-18 14:36 UTC
Requires
- php: ^8.3
- comphp/config: ^0.1
Requires (Dev)
- phpmd/phpmd: ^2.13
- phpstan/phpstan: ^1.10.58
- phpunit/phpunit: ^10.5.9
README
这个库引入了INI配置驱动程序 IniConfigurationDriver
,作为CommonPHP配置管理生态系统的一部分。它通过允许应用程序无缝地使用INI文件加载和保存配置来扩展CommonPHP的功能。
功能
- 加载INI配置:简化了读取INI文件并将它们转换为关联数组以便在PHP应用程序中访问的过程。
- 以INI保存配置:能够将PHP关联数组序列化回INI格式,保留结构和层次结构。
- 结构化错误处理:包含详细的异常处理,以有效管理潜在的解析和文件操作错误。
- 支持嵌套结构:通过自定义实现,支持INI文件中嵌套结构的表示,提供更灵活的配置管理。
安装
使用Composer将配置管理器和INI驱动程序集成到项目中
composer require comphp/config
composer require comphp/config-ini
用法
要使用配置管理器与INI驱动程序,首先确保 DriverManager
已配置以识别INI驱动程序
use CommonPHP\Drivers\DriverManager; use CommonPHP\Configuration\Drivers\IniConfigurationDriver\IniConfigurationDriver; $driverManager = new DriverManager(); $driverManager->enable(IniConfigurationDriver::class);
配置后,由于 #[ConfigurationDriverAttribute('ini')]
注解,IniConfigurationDriver
将自动用于 .ini
文件扩展名。
加载配置文件
$configManager->loadDriver(IniConfigurationDriver::class); $config = $configManager->get('path/to/configuration.ini');
保存配置文件
根据上述说明加载驱动程序后,可以对INI文件进行修改并保存回来
$config->data['newSection'] = ['newKey' => 'newValue']; $config->save(); // Persists the changes to 'path/to/configuration.ini'
异常处理
驱动程序包括针对常见问题的特定异常处理,例如
- ConfigurationException:当INI文件解析错误或文件格式不符合预期结构时抛出。
- 通用异常:对于文件读写操作或解析失败。