aloframework / config
允许在运行时轻松更改配置的接口式类配置器
Requires
- php: >=5.4
- aloframework/common: ^2.0
This package is not auto-updated.
Last update: 2024-09-14 18:26:02 UTC
README
一个允许通过默认和覆盖设置轻松进行对象配置的组件
最新版本API文档:https://aloframework.github.io/config/
安装
可以通过Composer进行安装
composer require aloframework/config
抽象类
AloFramework\Config\AbstractConfig
是您自己的库配置应扩展的。它有三个主要私有属性:$defaults
存储默认配置,$custom
存储自定义配置/覆盖,以及$merged
,它存储将实际使用的配置。您的覆盖配置类将填充默认数组并在构造函数中接受任何自定义变量,例如:
use AloFramework\Config\AbstractConfig;
class MyConfig extends AbstractConfig {
private static $defaultConfig = ['foo' => 'bar',
'importantVar' => 'andItsValue'];
function __construct($customConfig = []) {
parent::__construct(self::$defaultConfig, $customConfig);
}
}
在读取配置时,值是从$merged
数组中获取的,这实际上是array_merge($this->defaults, $this->custom)
。有关更多信息,请参阅上面的API文档。
接口
您可以在配置读取类中实现AloFramework\Config\Configurable
接口,以指示其运行时设置可以使用此包进行更改。下面描述的特质可以用来实现所需的方法。
特质
如果您不想自己编写方法,可以简单地包含提供的AloFramework\Config\ConfigurableTrait
,这将实现接口所需的所有方法。
更新配置
设置项
您可以通过调用$config->set('myKey', 'myValue')
,使用__set()
如$config->myKey = 'myValue'
,或者简单地像数组一样使用它:$config['myKey'] = 'myValue'
来添加自定义配置键或默认设置覆盖。
删除项
您可以通过$config->remove('myKey')
删除自定义
配置,或者通过取消设置数组值来删除它:unset($config['myKey']);
读取配置
特定值
您可以通过调用$config->get('myKey')
,使用__get()
如$config->myKey
或像数组一样使用对象来从合并数组中检索特定配置项:$config['myKey']
。
合并的配置
您可以通过$config->getAll()
检索合并数组。
默认配置
为此,您将使用$config->getDefaultConfig()
。
自定义覆盖
为此,您将使用$config->getCustomConfig()
。