antonkuku/featureflag

一个用于轻松处理功能标志的库

1.0.0 2020-07-11 08:12 UTC

This package is auto-updated.

Last update: 2024-09-11 16:32:30 UTC


README

功能标志

PHP中轻松处理功能标志的库。

该库允许添加多个数据源(如果您想有默认值和覆盖值的配置)。

数据源

例如,我有一些来源

  • SimpleArray
  • SimpleArrayWritable - 可写数据源的示例
  • SimpleObject
  • RemoteJsonCurl

要添加另一个数据源,您需要创建一个新类并实现 \FeatureFlag\Contracts\DataSourceInterface,包含3个方法

  • exists - 确定所选标志是否存在
  • get - 获取标志值
  • all - 从数据源返回可用的标志

如果您需要添加/更新/删除标志值,可以实现 \FeatureFlag\Contracts\WritableSourceInterface,包含3个方法

  • add
  • update
  • delete

示例

单个来源

// init array of flags with values
$config = ['first_flag' => true, 'second_flag' => false];
// init data source
$ds = new \FeatureFlag\DataSources\SimpleArray($config);
// create instance of service and add data source
$service = new \FeatureFlag\FeatureFlag();
$service->addSource($ds);
// single condition check
if($service->enabled('first_flag')) {
    // do something
}
// multiple flags check
if($service->enabledWithDependencies('first_flag', ['second_flag' => false])) {
    // do something
}

多个来源

// init first data source
$firstDsConfig = ['first_flag' => true, 'second_flag' => false];
$firstDs = new \FeatureFlag\DataSources\SimpleArray($firstDsConfig);
// init second data source
$secondDsConfig = new \stdClass;
$secondDsConfig->first_flag = false;
$secondDs = new \FeatureFlag\DataSources\SimpleObject($secondDsConfig);
// init service
$service = new \FeatureFlag\FeatureFlag([$firstDs, $secondDs]);
//
$service->enabled('second_flag'); // false
$service->enabled('first_flag'); // false

安装

composer require antonkuku/featureflag