clagiordano / weblibs-configmanager
weblibs-configmanager 是一个易于读取和访问 PHP 配置数组的工具库,并可直接读取/写入配置文件/对象
v1.5.0
2021-07-12 15:27 UTC
Requires
- php: >=5.4
- ext-json: *
- symfony/yaml: ^2.8
Requires (Dev)
README
weblibs-configmanager
weblibs-configmanager 是一个工具库,用于轻松读取和访问 PHP 配置数组文件以及直接读取/写入配置文件/对象。
为什么使用 weblibs-configmanager ?
此项目的目的是提出一个简单且轻量级的库来管理 PHP 层次化配置文件。
支持格式
此包支持以下格式的配置文件
所有支持格式都将以相同的方式进行解析和内部处理,提供相同的功能。
安装
推荐通过 Composer 安装 weblibs-configmanager。
composer require clagiordano/weblibs-configmanager
使用示例(数组格式)
编写一个示例配置文件如下
<?php return array ( 'app' => 'app_name', 'db' => array ( 'host' => 'localhost', 'user' => 'sample_user', 'pass' => 'sample_pass', 'port' => 3306, ), 'other' => array ( 'multi' => array ( 'deep' => array ( 'nested' => 'config_value', ), ), ), );
实例化 ConfigManager 对象
use clagiordano\weblibs\configmanager\ConfigManager; /** * Instance object to read argument file */ $config = new ConfigManager("configfile.php");
检查配置文件中是否存在某个值
/** * Check if a value exists into config file */ $value = $config->existValue('app');
从配置文件中读取简单元素
/** * Read a simple element from config file */ $value = $config->getValue('app');
从配置中访问嵌套元素
/** * Access to a nested element from config */ $nestedValue = $config->getValue('other.multi.deep.nested');
在运行时更改配置值
/** * Change config value at runtime */ $this->config->setValue('other.multi.deep.nested', "SUPERNESTED");
以原始名称保存配置文件(覆盖)
/** * Save config file with original name (OVERWRITE) */ $this->config->saveConfigFile();
或以不同的名称保存配置文件
/** * Save config file with original name (OVERWRITE) */ $this->config->saveConfigFile('/new/file/name/or/path/test.php');
可选地,您还可以在保存后从磁盘重新加载配置文件
/** * Optionally you can also reload config file from disk after save */ $this->config->saveConfigFile('/new/file/name/or/path/test.php', true);
加载另一个配置文件,无需重新实例化 ConfigManager
/** * Load another configuration file without reinstance ConfigManager */ $this->config->loadConfig('another_config_file.php');
法律
版权 (C) Claudio Giordano claudio.giordano@autistici.org