juststeveking/laravel-feature-flags

一款简单易用的Laravel功能开关包

1.4.0 2024-03-13 09:48 UTC

This package is auto-updated.

Last update: 2024-09-13 11:03:28 UTC


README

Software License PHP Version Run Tests Latest Version on Packagist Total Downloads

我建议使用laravel/pennant来满足任何未来的功能开关需求。此包将保持现状,不再更新。

一款简单易用的Laravel功能开关包,允许您创建功能组并将用户分配到其中 - 同时还可以赋予用户超出其组之外的功能覆盖访问权限。

安装

您可以通过composer安装此包

composer require juststeveking/laravel-feature-flags

您可以使用以下命令发布迁移文件

php artisan vendor:publish --provider="JustSteveKing\Laravel\FeatureFlags\FeatureFlagsServiceProvider" --tag="migrations"

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="JustSteveKing\Laravel\FeatureFlags\FeatureFlagsServiceProvider" --tag="config"

这是已发布配置文件的内容

return [
    'middleware' => [
        'mode' => 'abort',

        'redirect_route' => '/',

        'status_code' => 404,
    ],
    
    'enable_time_bombs' => false,
    
    'time_bomb_environments' => ['production']
];

然后您需要迁移数据库更改

php artisan migrate

使用方法

此包允许您在数据库中管理用户功能和功能组。

所有功能和功能组的名称在保存时将转换为小写。

要使用此包,您的用户模型需要具有HasFeatures特性

<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use JustSteveKing\Laravel\FeatureFlags\Concerns\HasFeatures;

class User extends Authenticatable
{
    use HasFeatures;
}

这将允许您在用户模型上管理功能和功能组。

用户可以属于多个功能组,但也可以分配对特定功能的访问权限。

与功能组一起工作

// This will create the Feature Group if not already created and attach the user to it.
auth()->user()->addToGroup('beta testers');

// Alternatively you can use the following syntax
auth()->user()->joinGroup('beta testers');

// You can check if a user is a member of a feature group
auth()->user()->inGroup('beta testers');

// You can also get a user to leave a feature group
auth()->user()->leaveGroup('beta testers');

// You can also pass in more than one group name
auth()->user()->joinGroup('beta testers', 'api testers');

与功能一起工作

// This will create the Feature if not already created and attach the user to it.
auth()->user()->giveFeature('run reports');

// You can check if a user has a specific feature
auth()->user()->hasFeature('run reports');

// You can also remove a feature for a user
auth()->user()->removeFeature('run reports');

// Like with Feature Groups you can pass in more than one option
// These will return if any are matched.
auth()->user()->hasFeature('run reports', 'admin');

整合在一起

要整体使用此包

// Create a Feature Group
$group = FeatureGroup::create([
    'name' => 'Beta Testers'
]);

// Create a Feature
$feature = Feature::create([
    'name' => 'API Access'
]);

// Add the Feature to the Feature Group
$group->addFeature($feature);

// Assign a User to the Group
auth()->user()->joinGroup($group->name);

if (auth()->user()->groupHasFeature('api access')) {
    // The user belongs to a group that has access to this feature.
}

if (auth()->user()->hasFeature('run reports')) {
    // The user has been given access to this feature outside of group features
}

if (auth()->user()->hasFeature('user level feature')) {
    // The user has access to this feature as a user or through a group.
}

功能的时间炸弹

功能开关的一个常见用途是允许开发者在不破坏现有代码的情况下添加新功能。

当与良好的CI/CD管道配合使用时,这个过程非常出色。但最大的缺点是,当开发人员忘记在代码库中删除已实现的标志时,可能会产生残留的技术债务。

为了处理这种情况,此包的用户可以利用时间炸弹!时间炸弹用于在应该从代码库中删除标志时引发功能开关的异常。

要使用时间炸弹,您需要明确在配置中启用它们('enable_time_bombs' => true)。并定义您不希望引发异常的环境。(这对于CI/CD特别有用,因为您希望在本地、CI和预发布环境中抛出异常,但在生产环境中不抛出。)

定义时间炸弹何时引发异常

启用时间炸弹后,在创建新标志时,您将被询问希望标志何时到期(这是天数)。当当前时间超过到期日期时,功能标志将引发异常。

要扩展标志,可以使用以下方便的命令

php artisan feature-flags:extend-feature

您将被提示定义多少天后标志应再次引发异常。

进一步阅读

要了解更多关于功能标志和时间炸弹的信息,请参阅Martin Fowler的出色文章这里

模板使用

有一些Blade指令可以帮助控制UI中功能的访问

// You can check if a user has a specific feature
@feature('api access')
    <x-api-console />
@endfeature

// You can check if a user is a member of a feature group
@featuregroup('beta testers')
    <x-group-feature />
@endfeaturegroup

// You can check if a user is a member of a group with access to a feature
@groupfeature('api access')
    <x-api-console />
@endgroupfeature

中间件

有一些中间件类您可以使用

默认情况下,您可以使用

  • \JustSteveKing\Laravel\FeatureFlags\Http\Middleware\FeatureMiddleware::class
  • \JustSteveKing\Laravel\FeatureFlags\Http\Middleware\GroupMiddleware::class

这里有2个中间件类,在失败时可能会终止或重定向。这些中间件的工作方式可以通过包的配置文件来管理。它允许你为中间件设置模式(abortredirect),同时还可以设置 redirect_routestatus_code

然后还有

  • \JustSteveKing\Laravel\FeatureFlags\Http\Middleware\API\FeatureMiddleware::class
  • \JustSteveKing\Laravel\FeatureFlags\Http\Middleware\API\GroupMiddleware::class

这两个中间件类只有一种模式 abort,但会从你的配置文件中获取包的返回状态码,这些类是专门为API设计的。

为了限制具有特定功能的用户的访问

请将以下内容添加到你的 app/Http/Kernel.php

protected $routeMiddleware = [
    'feature' => \JustSteveKing\Laravel\FeatureFlags\Http\Middleware\FeatureMiddleware::class,
];

你可以传递多个功能名称,并且可以使用更友好的格式或直接传递

Route::middleware(['feature:run-reports,print reports'])->group(/* */);

为了限制属于功能组的用户的访问

请将以下内容添加到你的 app/Http/Kernel.php

protected $routeMiddleware = [
    'feature-group' => \JustSteveKing\Laravel\FeatureFlags\Http\Middleware\GroupMiddleware::class,
];

你可以传递多个功能组名称,并且可以使用更友好的格式或直接传递

Route::middleware(['feature-group:beta-testers,internal,developer advocates'])->group(/* */);

Artisan命令

有几个Artisan命令可用于与功能标志交互。

  feature-flags:activate-feature           Activates a feature
  feature-flags:activate-feature-group     Activates a feature group
  feature-flags:add-feature                Add a new feature
  feature-flags:add-feature-group          Add a new feature group
  feature-flags:add-feature-to-group       Add a feature to a group
  feature-flags:deactivate-feature         Deactivates a feature
  feature-flags:deactivate-feature-group   Deactivates a feature group
  feature-flags:view-feature-groups        View feature groups
  feature-flags:view-features              View features
  feature-flags:view-groups-with-features  View groups with features

测试

$ composer run test

贡献

有关详细信息,请参阅 CONTRIBUTINGCODE_OF_CONDUCT

安全性

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

鸣谢

许可协议

MIT许可协议(MIT)。有关更多信息,请参阅 许可文件