patipattip/filament-feature-flags

使用 Laravel Pennant 实现的特性标志和分段功能

dev-main 2024-05-01 14:47 UTC

This package is not auto-updated.

Last update: 2024-09-19 14:41:21 UTC


README

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

使用 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()
        );
}

创建基于类的特性

要创建基于类的特性,您可以使用 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

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全漏洞

请查看 我们的安全策略 了解如何报告安全漏洞。

致谢

许可证

MIT 许可证 (MIT)。请参阅 许可证文件 了解更多信息。