stephenjude/filament-feature-flags

Filament 的 Laravel Pennant 特性标志和分段实现。

2.0.2 2024-09-17 12:11 UTC

This package is auto-updated.

Last update: 2024-09-25 10:43:19 UTC


README

Filament Feature Flags

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Filament 的 Laravel Pennant 特性标志和分段实现。

  • 为用户的一部分(例如,按国家或货币)应用功能。
  • 为单个用户(例如,按电子邮件或ID)应用功能。
  • 为所有用户应用功能。
  • 创建自定义功能分段。
  • 功能事件处理(例如,在功能激活/停用时运行代码)。
  • 用户分段的 UI。
  • 从存储中清除所有已解决的功能。

了解更多信息

安装

您可以通过 composer 安装此软件包,并使用以下命令发布和运行迁移:

composer require stephenjude/filament-feature-flags

php artisan vendor:publish --provider="Laravel\Pennant\PennantServiceProvider"

php artisan vendor:publish --provider="Stephenjude\FilamentFeatureFlag\FeatureFlagPluginServiceProvider"

php artisan migrate

用法

此软件包仅用于基于类的功能。

您需要在面板提供者中注册此插件。

public function panel(Panel $panel): Panel
{
    return $panel
        ->plugin(
            Stephenjude\FilamentFeatureFlag\FeatureFlagPlugin::make()
        );
}

您不需要在服务提供者启动方法中调用 Feature::discover(),此软件包已为您完成。

授权/访问控制

您可以授权插件供具有特定角色/权限的用户使用。

FeatureFlagPlugin::make()
    ->authorize(fn() => auth()->user()->can('view.features'));

创建基于类的功能

要创建基于类的功能,您可以使用 pennant:feature Artisan 命令。

php artisan pennant:feature WalletFunding

在编写功能类时,您只需使用 Stephenjude\FilamentFeatureFlag\Traits\WithFeatureResolver 特性,该特性会调用以解决特定范围的功能的初始值。

<?php

namespace App\Features;

use Stephenjude\FilamentFeatureFlag\Traits\WithFeatureResolver;

class WalletFunding
{
    use WithFeatureResolver;
}

功能分段

默认情况下,此软件包使用 App\Models\User 模型和默认分段,通过电子邮件为单个或一组用户应用功能。

要添加新分段,请将以下代码片段添加到 filament-feature-flag 配置中的 segments 部分

示例 1:添加货币分段

[
    'column' => 'currency',
    'source' => [
        'model' => \App\Models\Currency::class,
        'value' => 'code',
        'key' => 'code',
    ],
],

示例 2:添加国家分段

[
    'column' => 'country',
    'source' => [
        'model' => \App\Models\Country::class,
        'value' => 'name',
        'key' => 'name',
    ],
],

功能事件

当功能被激活或停用时,此软件包会发出事件,您的应用程序可以订阅这些事件。您可以在 EventServiceProvider 类内部监听这些事件。

use Stephenjude\FilamentFeatureFlag\Events\{FeatureActivatedForAll,FeatureDeactivatedForAll,FeatureSegmentCreated,FeatureSegmentModified,FeatureSegmentRemoved,RemovingFeatureSegment};

protected $listen = [
    FeatureActivatedForAll::class => [
        // Dispatched after feature is activated for all users.
    ],
    FeatureDeactivatedForAll::class => [
        // Dispatched after feature is deactivated for all users.
    ],
    FeatureSegmentCreated::class => [
        // Dispatched after feature segment is created.
    ],
    FeatureSegmentModified::class => [
        // Dispatched after feature segment is modified.
    ],
    RemovingFeatureSegment::class => [
        // Dispatched before feature segment is removed.
    ],
    FeatureSegmentRemoved::class => [
        // Dispatched after feature segment is removed.
    ],
];

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

return [
    // This package supports only class based features.

    /*
    * This is the default state for all class based features and
     * state will be used if there is no segmentation.
    */
    'default' => true,

    /*
     * Default scope: User::class, Team::class
     */
    'scope' => App\Models\User::class,

    /*
     * Column names and data source that can be used to activate or deactivate for a segment of users.
     * This columns must exist on the users table and the data source must be a model.
     * COLUMN: The column name as defined on the default scope model config.
     * MODEL: The eloquent model of the source table.
     * VALUE: The column to be used as value.
     * KEY: The column to be used as key.
     */
    'segments' => [
        [
            'column' => 'email',
            'source' => [
                'model' => App\Models\User::class,
                'value' => 'email',
                'key' => 'email',
            ],
        ],
    ],

    'panel' => [
        /*
         * Navigation group for admin panel resource.
         */
        'group' => 'Settings',

        /*
         * Navigation item label for admin panel resource.
         */
        'label' => 'Manage Features',

        /*
         * Resource title for admin panel resource.
         */
        'title' => 'Manage Features & Segments',

        /*
         * Navigation item icon for admin panel resource.
         */
        'icon' => 'heroicon-o-cursor-arrow-ripple'
    ]
];

截图

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 变更日志

贡献

有关详细信息,请参阅 贡献指南

安全漏洞

有关如何报告安全漏洞的详细信息,请参阅 我们的安全策略

鸣谢

许可证

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