jadb/feature_toggle

功能翻转,功能标志,功能开关

v0.1.0 2016-06-17 03:07 UTC

This package is auto-updated.

Last update: 2024-08-28 01:07:56 UTC


README

Build Status Total Downloads License

功能开关

a.k.a. 功能翻转,功能标志,功能开关

来自 维基百科

功能开关是软件开发中的一种技术,旨在提供一种替代维护多个源代码分支(称为功能分支)的方法。

持续发布和持续部署使您能够快速获得关于编码的反馈。这需要您尽早集成您的更改。功能分支引入了绕过此过程的路径。功能开关将您带回正轨,但如果开关是“关闭”的,则您的功能执行路径仍然“无效”且“未经测试”。但是,只需将开关设置为“开启”,就可以轻松启用新的执行路径。

常见用例

  • 有限测试(例如,基于电子邮件地址的员工、用户子集等)
  • 逐步功能发布(例如,按位置、按订阅、按浏览器等)

安装

FeatureToggle可以使用Composer(当然,您也可以从GitHub克隆它)进行安装。

注意:对于PHP5.x支持,请检查0.1.0分支。

composer.json

{
    "require": {
        "jadb/feature_toggle": "^1.0"
    }
}

要安装,您可以运行以下命令

$ php composer.phar install

示例

在您的应用程序引导程序中

use FeatureToggle\FeatureRegistry;
use Predis\Client as Redis;

FeatureRegistry::setStorage(new Redis());

FeatureRegistry::init('Cool Feature', [
	'description' => 'A cool new feature!',
	'strategies' => [
		'UserAgent' => [['/opera/', '/Mozilla\/5\.0/']],
		function ($Feature) {
			return !empty($_SESSION['isAdmin']);
		},
		function ($Feature) {
			return !empty($_SESSION[$Feature->getName()]);
		}
	]
]);

FeatureRegistry::init('Another Cool Feature', [
	'type' => 'threshold', // use the `ThresholdFeature`
	'description' => 'Another cool new feature!',
	'strategies' => [
		'UserAgent' => [['/opera/', '/Mozilla\/5\.0/']],
		function ($Feature) {
			return !empty($_SESSION['isAdmin']);
		},
		function ($Feature) {
			return !empty($_SESSION[$Feature->getName()]);
		}
	]
])->threshold(2); // Require at least 2 strategies to pass

然后,在您的代码中的任何地方,您都可以检查此功能的状态,如下所示

if (\FeatureToggle\FeatureManager::isEnabled('Cool Feature')) {
	// do something
}

包含内容

功能

  • BooleanFeature:如果一个或多个策略通过,则启用。
  • StrictBooleanFeature:只有当整个策略集通过时才启用。
  • ThresholdFeature:只有当策略通过的最小数量时才启用。
  • EnabledFeature:强制功能始终启用。
  • DisabledFeature:强制功能始终禁用。

功能必须实现FeatureInterface

策略

  • DateTimeStrategy:将今天的日期和时间与设定的日期和时间进行比较。
  • DateTimeRangeStrategy:检查今天的日期和时间是否在设定的日期时间范围内。
  • UserAgentStrategy:检查浏览器用户代理是否与任何允许的代理匹配。

策略必须实现StrategyInterface

存储适配器

  • HashStorage:默认。基本关联数组(即内存中)
  • FileStorage:使用文件系统(仅当功能存储在数据库中时适用)。
  • MemcachedStorage:Memcached存储,需要Memcached扩展。
  • RedisStorage:Redis存储,需要predis/predis包。

存储适配器必须实现StorageInterface

待办事项

  • PercentageStrategy:使功能针对用户百分比启用 - 需要RedisStorage
  • 当达到错误阈值时自动禁用功能 - 需要RedisStorage

贡献

  • 分支
  • 修改、修复、测试
  • 可选编写一些文档(目前在README.md中)
  • 发送拉取请求

所有贡献的代码必须根据[BSD 3-Clause License][bsd3clause]授权。

错误 & 反馈

http://github.com/jadb/feature_toggle/issues

许可证

版权所有(c)2014,Jad Bitar

MIT许可证下授权。

文件的重新分发必须保留上述版权声明。