exolnet/laravel-bento

Laravel 模块,用于面向人群启动功能。

v2.5.0 2024-03-28 14:21 UTC

This package is auto-updated.

Last update: 2024-08-28 15:37:47 UTC


README

Latest Stable Version Software License Build Status Total Downloads

Bento 帮助您通过自定义用户段来组织功能发布。创建和组织规则,使某些用户可以使用功能。

定义您的功能,定义您的分段策略,然后让 Bento 将每个功能发布给正确的人。Bento 还可以帮助您在应用程序上进行 A/B 测试。

此库的核心概念灵感来源于 Airbnb 的 Trebuchet 项目(Ruby)。

安装

使用 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

中间件

由于某些策略需要评估请求上下文,建议使用中间件来限制路由

  1. 在您的应用程序的 HTTP Kernel 的 $routeMiddleware 中添加以下中间件
    protected $routeMiddleware = [
        // ...
        'await' => \Exolnet\Bento\Middleware\Await::class,
        'launch' => \Exolnet\Bento\Middleware\Launch::class,
        // ...
    ];
  1. 然后,您可以使用它们来限制您的路由
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

贡献

有关详细信息,请参阅 CONTRIBUTINGCODE OF CONDUCT

安全

如果您发现任何安全相关的问题,请通过电子邮件 security@exolnet.com 而不是使用问题跟踪器。

鸣谢

许可

此代码根据 MIT 许可证 许可。有关更多信息,请参阅 许可文件