xety / configurator
一个无依赖的简单配置类,使用Fluent模式。
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: ~6.0
- squizlabs/php_codesniffer: ~3.0
This package is auto-updated.
Last update: 2024-09-05 00:13:05 UTC
README
一个无依赖的简单配置类,使用Fluent模式。
需求
安装
composer require xety/configurator
使用
Configurator
类是一个抽象类,所以您只需extends
它
<?php namespace App; use Xety\Configurator\Configurator; class MyClass extends Configurator { }
如果您想为您的类设置默认配置,只需执行以下操作
<?php class MyClass extends Configurator { protected $defaultConfig = [ 'key' => 'value', //etc ]; public function __construct() { $this->setConfig($defaultConfig); } }
文档
方法
Configurator::setConfig
public setConfig (array $values)
描述
将值设置到选项数组中。此函数将替换所有配置选项。
参数
(array) $values
: 要推送到配置的值。
返回值
\Xety\Configurator\Configurator::class
Configurator::getConfig
public getConfig (void)
描述
获取所有选项及其值。
参数
此函数没有参数。
返回值
array
Configurator::flushConfig
public flushConfig (string ...$filter)
描述
从选项数组中清除选项列表。
使用
$this->flushConfig('key1', 'key2', 'key3');
参数
(string) ...$filter
: 要从配置中删除的所有选项。
返回值
\Xety\Configurator\Configurator::class
Configurator::mergeConfig
public mergeConfig (array $values, bool $invert = false)
描述
将值合并到选项数组中。
参数
(array) $values
: 要合并到配置中的值。(bool) $invert
: 通过将实际配置合并到值中来实现合并的倒置。
返回值
\Xety\Configurator\Configurator::class
Configurator::clearConfig
public clearConfig (void)
描述
清除所有存储的选项。
参数
此函数没有参数。
返回值
\Xety\Configurator\Configurator::class
Configurator::setOption
public setOption (string $name, mixed $value)
描述
设置给定选项的值。
使用
$this->setOption('key', 'value'); $this->setOption('key', ['key2' => ['value2']]);
参数
(string) $name
: 选项名称。(mixed) $value
: 选项值。
返回值
\Xety\Configurator\Configurator::class
Configurator::getOption
public getOption (string $name)
描述
获取选项值。
使用
$this->getOption('key');
参数
(string) $name
: 要获取的选项名称。
返回值
mixed
Configurator::hasOption
public hasOption (string $name)
描述
检查选项是否存在。
参数
(string) $name
: 要检查的选项名称。
返回值
bool
Configurator::flushOption
public flushOption (string $name)
描述
清除选项。
参数
(string) $name
: 要清除的选项名称。
返回值
\Xety\Configurator\Configurator::class
Configurator::pushOption
public pushOption (string $name, array $args)
描述
将列出的参数推送到命名选项。
使用
$this->pushOption('key', ['key1' => 'value1'], ['key2' => ['value2' => 'value3']]);
结果
'key' => [ 'key1' => 'value1', 'key2' => [ 'value2' => 'value3' ] ]
参数
(string) $name
: 选项名称。(array) $args
: 要推送到选项键中的值列表。
返回值
\Xety\Configurator\Configurator::class
Configurator::consumeOption
public consumeOption (string $name)
描述
读取并清除选项。示例
配置
$config = [ 'key1' => 'value1' ];
使用
$result = $this->consumeOption('key1');
结果
echo $result; // value1 var_dump($config); // []
参数
(string) $name
: 要读取并清除的选项名称。
返回值
mixed
Configurator::transientOption
public transientOption (string $name, mixed $value)
描述
添加一个短暂的配置键/值。
使用
// Will update the value of the key `key` if it exist, // or it will create it with the value `value`. $this->transientOption('key', 'value');
参数
(string) $name
: 选项名称。(mixed) $value
: 要设置的值。
返回值
\Xety\Configurator\Configurator::class
贡献
如果您想贡献,请遵循此指南。