exolnet / laravel-bento
Laravel模块,用于向人群推出功能。
v2.5.0
2024-03-28 14:21 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^10.0|^11.0
- illuminate/http: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
- illuminate/view: ^10.0|^11.0
Requires (Dev)
- exolnet/phpcs-config: ^2.0
- laravel/pint: ^1.1
- mockery/mockery: ^1.4
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.5
- squizlabs/php_codesniffer: ^3.6
README
Bento帮助您通过自定义用户段组织功能推出。创建和组织规则,使特定用户可以使用功能。
定义您的功能,定义您的分段策略,让Bento将每个功能推向正确的人。Bento还可以帮助您在应用程序上进行A/B测试。
本库的核心概念受到Airbnb的Trebuchet项目的启发。
安装
使用composer安装此包
composer require eXolnet/laravel-bento
安装Bento后,发布其示例服务提供者以保存您的功能定义
php artisan vendor:publish --tag=bento-provider
然后,将其添加到config/app.php
中的providers
数组中
App\Providers\BentoServiceProvider::class
用法
创建功能
定义功能和它们的推出分段策略。您可以使用feature
方法定义一个策略
Bento::feature('feature')->visitorPercent(10);
或者您可以将多个策略组合起来
Bento::feature('feature')->visitorPercent(10)->hostname('example.com');
您的功能可以分组在服务提供者的boot
方法中
<?php namespace App\Providers; use Exolnet\Bento\Facades\Bento; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * @return void */ public function boot(): void { Bento::feature('foo')->everyone(); Bento::feature('bar')->everyone(); } }
推出您的功能
您可以使用launch
方法检查功能是否已推出给访客
if (Bento::launch('feature')) { // }
或检查功能是否正在等待推出
if (Bento::await('feature')) { // }
Blade
在Blade模板中,还提供了一些方便的宏
@launch('feature')
Feature is launched!
@else
Coming soon!
@endlaunch
@await('feature')
Coming soon!
@else
Feature is launched!
@endawait
中间件
由于某些策略需要评估请求上下文,建议使用中间件来限制路由
- 在您应用程序的HTTP Kernel的
$routeMiddleware
中添加以下中间件
protected $routeMiddleware = [ // ... 'await' => \Exolnet\Bento\Middleware\Await::class, 'launch' => \Exolnet\Bento\Middleware\Launch::class, // ... ];
- 然后,您可以使用它们来限制您的路由
Route::middleware('launch:feature')->group(function () { // });
Route::middleware('await:feature')->group(function () { // });
基本分段策略
以下分段策略可供快速定位您的用户
- 回调
- 配置
- 日期
- 环境
- 所有人
- 访客
- 主机名
- 没有人
- 占位符
- 用户(认证用户或特定用户ID)
- 用户百分比(所有连接访客的比例)
- 访客百分比(所有访客的比例)
逻辑分段策略
还有额外的逻辑分段策略可供使用,以帮助您通过更复杂的规则定位您的用户。
非
Bento::feature('feature')->not->everyone();
所有
use \Exolnet\Bento\Strategy\AimsStrategies; Bento::feature('feature')->all(function (AimsStrategies $aims) { $aims ->environment('production') ->visitorPercent(20); });
任何
use \Exolnet\Bento\Strategy\AimsStrategies; Bento::feature('feature')->any(function (AimsStrategies $aims) { $aims ->environment('staging') ->user([1, 2]); });
自定义分段策略
您可以通过依赖注入支持创建自定义策略,类似于Laravel控制器的方法注入。方法注入的常见用例是将Illuminate\Contracts\Auth\Guard
实例注入到策略中,以根据属性定位用户
回调
use Illuminate\Contracts\Auth\Guard; Bento::feature('feature')->custom(function (Guard $guard, $role) { return $guard->user() && $guard->user()->role === 'admin'; });
类
use Illuminate\Contracts\Auth\Guard; class RoleStrategy { /** * @var \Illuminate\Contracts\Auth\Guard */ protected $guard; /** * @var string */ protected $role; /** * @param \Illuminate\Contracts\Auth\Guard $guard */ public function __construct(Guard $guard, string $role) { $this->guard = $guard; $this->role = $role; } /** * @return bool */ public function launch(): bool { return $this->guard->user() && $this->guard->user()->role === $this->role; } } Bento::feature('feature')->aim(RoleStrategy::class, 'admin');
测试
要运行PHPUnit测试,请使用
$ composer test
贡献
有关详细信息,请参阅CONTRIBUTING和CODE OF CONDUCT。
安全
如果您发现任何与安全相关的问题,请通过security@exolnet.com而不是使用问题跟踪器来发送电子邮件。