ssola / enabler
通过特定IP、特定组/用户或访客百分比启用功能。
Requires
- phpunit/phpunit: 4.3.5
- predis/predis: ~1.0
This package is not auto-updated.
Last update: 2024-09-24 07:38:37 UTC
README
Enabler 是一个简单的功能标志包。使用此包,您可以向客户展示/隐藏您的新特性,以便顺利部署新内容,并保持一切可控。
我想知道您对这个包的看法,如果您有改进此包的想法,请创建一个新的问题。
要求
要使用此包,您只需要具备以下条件
- PHP >= 5.4
- Redis(这是我们默认的持久化工具)
- Composer
如果您需要不同的持久化工具,如Memcache或MySQL,您可以简单地编写自己的Storable适配器。
安装
composer require ssola/enabler
composer update
它如何工作?
使用 Enabler,向用户展示/隐藏功能非常简单。让我用一个简单的例子来展示它是如何工作的。
要求:我们需要逐步部署我们的新杀手级功能。我们预计会有很多人使用它,并且我们需要完全确信它能正常工作。在这个阶段,我们只想将此功能启用给一小部分客户:正好是1%。
首先,我们必须创建我们的新功能,并包含应使用的过滤器。
$feature = new Enabler\Feature( 'secret-feature', true, [ 'Enabler\Filter\Distributed' => [1] ] ); $enabler = new Enabler\Enabler($storage); $enabler->storage()->create($feature); // At this point feature has been created and will use the Distributed filter to display it only to 1% of our visitors // after this we can create the condition: if ($enabler->enabled('secret-feature')) { // Here your feature for only 1% of your visitors. }
最后,您决定在一个新场景中测试您的新功能。在这种情况下,我们只想向已登录的客户展示。为此,我们实现了标识符过滤器。您只需要实例化一个Identity对象,传递所需用户数据。在这种情况下,我们必须传递用户ID及其所属的组。
$identity = new Enabler\Identity(MyUserClass::getUserId(), MyUserClass::getGroup()); $feature = new Enabler\Feature( 'secret-feature', true, [ 'Enabler\Filter\Identifier' => ['groups' => ['early-adopters', 'test-users']] ] ); $enabler = new Enabler\Enabler($storage, $identity); $enabler->storage()->create($feature); if ($enabler->enabled('secret-feature')) { // Here your feature for users that belongs to early-adopters or test-users group }
但是,现在假设我们需要向我们的测试用户组中的更大一部分(10%)显示,特定日期范围内。我们可以通过向功能过滤器链中添加多个过滤器轻松做到这一点,如下所示
$identity = new Enabler\Identity(MyUserClass::getUserId(), MyUserClass::getGroup()); $feature = new Enabler\Feature( 'secret-feature', true, [ 'Enabler\Filter\Distributed' => [10], 'Enabler\Filter\Identifier' => ['groups' => ['test-users']], 'Enabler\Filter\Date' => ['from' => new DateTime('2014-10-30'), 'to' => new DateTime('2015-10-30')] ] ); $enabler = new Enabler\Enabler($storage, $identity); $enabler->storage()->create($feature); if ($enabler->enabled('secret-feature')) { // Here your feature for users that belongs to early-adopters or test-users group }
扩展我
存储
我们需要一个存储适配器,以便能够将您的功能配置存储在某个地方。我们的默认存储工具是Redis,但您也可以自由编写自己的适配器。
这就像这样
class MyAwesomeStorageAdapter implements Enabler\Storage\Storable { public function create (Feature $feature) { // do your magic here! } public function delete ($name) { // delete a specific Feature } public function get ($name) { return $myFeature; } }
#### 过滤器
目前我们支持四种不同的过滤器:随机加权分布、IP、日期/日期范围和标识符。但我们认为人们会有很多好的想法,创建自己的过滤器也非常简单。
class FilterByWeather implements Enabler\Filter\Filterable { public function filter ($value, Feature $feature, Identity $identity) { // our value is Sunny $currentWeather = Weather::getFromIp(IP::getIp()); if($currentWeather == $value) { return true; } return false; } }
功能
您可以选择不同的过滤器或创建自己的过滤器来显示/隐藏您的功能
- 按IP:仅向特定IP或IP范围显示您的功能(待定)
- 按随机权重分布:例如,您可以将功能仅向10%的访客显示
- 按标识符:使用我们的Identity类或创建自己的类,以便访问用户ID和/或组
- 按日期或日期范围:根据特定日期显示您的功能,或设置一个范围,以便仅在特定日期范围内显示它。