phower / config
简化PHP配置数据处理的简便方法。
Requires
- php: ^5.6 || ^7.0
Requires (Dev)
- phpunit/phpunit: ^5.2
- squizlabs/php_codesniffer: ^2.5
- symfony/var-dumper: ^3.0
This package is auto-updated.
Last update: 2024-09-10 04:06:10 UTC
README
简化PHP配置数据处理的简便方法。
要求
Phower Config 需要
安装
使用 Composer 将 Phower Config 添加到任何 PHP 项目中
composer require phower/config
入门
可以使用关联数组作为任何应用程序中要处理的配置数据。
// index.php require('path/to/vendor/autoload.php'); use Phower\Config\Config; // array of options $options = [ 'host' => 'example.org', 'email' => 'me@example.org', 'user_name' => 'Pedro', 'password' => 'Secre7!#@', ]; // create config instance $config = new Config($options); // access a configuration key echo $config->get('email'); // 'me@example.org'
包含配置
可选地,可以从返回数组的普通PHP脚本中读取配置数据
// config.php return [ 'host' => 'example.org', 'email' => 'me@example.org', 'password' => 'Secre7!#@', ]; // index.php $config = new Config(include('config.php'));
接口方法
因为我们实现了 ArrayAccess 接口 和 魔术方法,因此也可以以不同的风格访问配置数据
// equivalent methods echo $config->get('email'); // 'me@example.org' echo $config['email']; // 'me@example.org' echo $config->email; // 'me@example.org' echo $config->getEmail(); // 'me@example.org'
规范化的键名
由于键名在内部进行了规范化,因此可以放宽命名约定
// always refer same key echo $config->get('user_name'); // 'Pedro' echo $config->get('userName'); // 'Pedro' echo $config->get('USER-NAME'); // 'Pedro' echo $config->get('username'); // 'Pedro' // same applies when using different methods of example above
请注意,规范化的键名可能会导致重复,应在创建 Phower Config 实例时避免使用某些命名约定。例如,始终在数组元素键上使用 snake-case 或 camel case。
只读模式
默认情况下创建的实例是只读的,这意味着创建后不能更改。但是,可以通过两种方式更改此行为
// 1. create instance with read-only mode set to false $config = new Config(include('config.php'), false); echo $config->readOnly(); // FALSE // 2. optionally set read-only mode after creation $config = new Config(include('config.php')); echo $config->readOnly(); // TRUE $config->readOnly(false); echo $config->readOnly(); // FALSE
请注意,
readOnly方法的参数是可选的。当省略时,该方法返回只读模式的状态;否则,将其状态设置为 TRUE 或 FALSE。
允许覆盖模式
与只读模式一样,也可以控制 Phower Config 实例中的覆盖。最初不允许覆盖,但可以像之前一样更改
// 1. create instance with allow-override mode set to true $config = new Config(include('config.php'), false, true); echo $config->allowOverride(); // TRUE // 2. optionally set allow-override mode after creation $config = new Config(include('config.php')); echo $config->allowOverride(); // FALSE $config->allowOverride(true); echo $config->allowOverride(); // TRUE
请注意,
allowOverride方法的参数是可选的。当省略时,该方法返回允许覆盖模式的状态;否则,将其状态设置为 TRUE 或 FALSE。
显然,只有在将只读模式设置为 FALSE 时,才能执行覆盖。
更改配置
如果需要在创建后更改配置,可以通过设置或删除键来完成
// create empty config instance with read-only set to FALSE $config = new Config([], false); // set some keys $config->set('host', 'example.org') ->set('email', 'me@example.org') ->set('user_name', 'Pedro') ->set('password', 'Secre7!#@'); // remove one key $config->remove('user_name');
与从配置数组中获取键值一样,也提供了访问接口和魔术方法
// setting keys $config['host'] = 'example.org'; $config->host = 'example.org'; $config->setHost('example.org'); // removing keys unset($config['host']); unset($config->host); $config->removeHost();
检查配置
要检查给定的键是否存在于配置中,还有另一个方法可用
// checking 'host' key echo $config->has('host'); // returns TRUE if exists otherwise FALSE // alternative interfaces isset($config['host']); isset($config->host); $config->hasHost();
高级用法
在某些情况下,可能需要将配置实例导出为数组或将另一个配置对象合并到当前实例中。为了提供这些需求,提供了 toArray 和 merge 方法
// exporting configuration $config = new Config($options); $config->toArray(); // returns $options array // merging configurations $config1 = new Config($someOptions); $config2 = new Config($otherOptions); $config1->merge($config2); // $otherOptions are merged with $someOptions internally
运行测试
测试可以在单独的命名空间中找到,并且可以在命令行中与 PHPUnit 一起运行
vendor/bin/phpunit
编码规范
Phower 代码根据 PSR-2 编码风格标准编写。为了强制执行这一点,还提供了 CodeSniffer 工具,可以像下面这样运行
vendor/bin/phpcs
报告问题
如果您发现此代码中存在问题,请在 https://github.com/phower/config/issues 上创建工单。
贡献者
开源是由贡献构成的。如果您想为 Phower 做贡献,请按照以下步骤操作
- 将最新版本分叉到您自己的仓库中。
- 编写您的更改或新增内容,并将其提交。
- 遵循PSR-2编码风格标准。
- 确保您对更改有完整的单元测试覆盖。
- 前往GitHub拉取请求页面 https://github.com/phower/config/pulls 创建新的请求。
谢谢!
更改和版本控制
此代码的所有相关更改都已记录在单独的 日志文件 中。
版本号遵循 语义化版本 的建议。
许可
Phower代码在 MIT许可 下维护。