charlgottschalk/feature-toggle-lumen

提供了一种创建和管理功能开关的简单实现

v2.1.1 2021-08-18 17:05 UTC

This package is auto-updated.

Last update: 2024-09-18 23:49:09 UTC


README

Latest Version on Packagist Total Downloads

功能开关是一种与源代码控制结合使用的编码策略,用于简化持续集成和部署。开关的基本思想是,如果某个功能被禁用,则防止代码部分执行。

* 这项工作正在进行中,但已稳定并可用

LFT 提供了一个简单的包,用于实现功能开关,允许您使用 Artisan 命令切换功能的开/关。

哦,它还支持用户角色,您的用户角色。

安装

1. 使用 composer 安装包

$ composer require charlgottschalk\feature-toggle-lumen

该包应自动由 Lumen 发现,但如果未发现,请简单地在您的 bootstrap/app.php 中注册服务提供者。

$app->register(CharlGottschalk\FeatureToggleLumen\FeatureToggleServiceProvider::class);

2. 启用 Eloquent

如果尚未启用,请在您的 bootstrap/app.php 中取消注释此行以启用 Eloquent。

$app->withEloquent();

3. 启用 Facades

如果尚未启用,请在您的 bootstrap/app.php 中取消注释此行以启用 Facades。

$app->withFacades();

4. 迁移

运行 $ php artisan migrate 以创建功能开关表。

使用方法

LFT 提供了一些易于使用的辅助函数和中间件来决定功能是否启用。

外观

LFT 为控制器等提供了外观,以便轻松检查功能开关。

检查功能是否启用 - 不检查角色

use CharlGottschalk\FeatureToggleLumen\Facades\Feature;

if (Feature::enabled('feature_name')) {
    // Feature is enabled
}

检查功能是否启用,包括检查 $request 用户是否有通过角色的权限

use CharlGottschalk\FeatureToggleLumen\Facades\Feature;

if (Feature::enabledFor('feature_name')) {
    // Feature is enabled and user has permission
}

LFT 将尝试通过属性和角色配置(参见配置)获取用户的角色,以确定用户是否有权访问功能。

辅助函数

LFT 提供了检查功能开关的辅助函数。

if (feature_enabled('feature_name')) {
    // Feature is enabled
}

if (enabled_for('feature_name')) {
    // Feature is enabled and user has permission
}

中间件

LFT 提供了中间件,如果功能被禁用或用户没有权限,则禁用路由。

将中间件添加到您的 bootstrap\app.php

$app->routeMiddleware([
    // Other middleware
    'feature' => \CharlGottschalk\FeatureToggleLumen\Http\Middleware\CheckFeature::class,
    'feature.role' => \CharlGottschalk\FeatureToggleLumen\Http\Middleware\CheckFeatureRole::class,
]);

将中间件添加到您的必需路由中。

$router->get('some/url', ['middleware' => 'feature:feature_name', function () {
    //
}]);

$router->get('another/url', ['middleware' => 'feature.role:feature_name', function () {
    //
}]);

如果给定的功能被禁用或用户没有权限,中间件将 abort(404)

配置

<?php

return [
    /*
    |--------------------------------------------------------------------------
    | Feature Toggle switch
    |--------------------------------------------------------------------------
    |
    | This option controls if Feature Toggle is switched on or off.
    | If switched on (true), feature toggle will check if the given feature is enabled.
    | If switched off (false), feature toggle will return whether all features are enabled ('all_on').
    |
    */
    'on' => true,

    /*
    |--------------------------------------------------------------------------
    | Features switch
    |--------------------------------------------------------------------------
    |
    | This option controls if all features are 'enabled' or 'disabled' based on the 'on' option.
    | If 'all_on' => true, then all features will be 'enabled'.
    | If 'all_on' => false, then all features will be 'disabled'.
    |
    */
    'all_on' => true,
    
    /*
    |--------------------------------------------------------------------------
    | Default status
    |--------------------------------------------------------------------------
    |
    | This option controls how a non-existent feature's 'enabled' status is considered.
    |
    */
    'default' => true,

    /*
    |--------------------------------------------------------------------------
    | Database connection
    |--------------------------------------------------------------------------
    |
    | Specify which database connection to use for migrations and models.
    | Add a connection if you want to separate the feature toggles from your main
    | database.
    |
    */
    'connection' => env('DB_CONNECTION', 'mysql'),

    /*
    |--------------------------------------------------------------------------
    | User roles
    |--------------------------------------------------------------------------
    |
    | This option is used to assist with role based checks.
    |
    */
    'roles' => [
        /*
        |--------------------------------------------------------------------------
        | User role property
        |--------------------------------------------------------------------------
        |
        | This option is used to determine the property on the user model
        | that retrieves the user's role.
        | Feature Toggle will check if the property exists and use it, otherwise
        | check if a function with the name exists and use that.
        |
        */
        'property' => 'role',

        /*
        |--------------------------------------------------------------------------
        | User roles table
        |--------------------------------------------------------------------------
        |
        | This option is used to determine the table containing the user roles.
        |
        */
        'model' => \App\Models\Role::class,

        /*
        |--------------------------------------------------------------------------
        | User roles table name column
        |--------------------------------------------------------------------------
        |
        | This option is used to determine the name of the roles table column containing
        | the name of roles.
        |
        */
        'column' => 'name',
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication
    |--------------------------------------------------------------------------
    |
    | This option is used to assist with authentication of feature management UI.
    |
    */
    'auth' => [
        /*
        |--------------------------------------------------------------------------
        | User role
        |--------------------------------------------------------------------------
        |
        | The role an authenticated user needs in order to access the feature toggle UI.
        |
        */
        'role' => 'developer',
    ],

    'route' => [
        /*
        |--------------------------------------------------------------------------
        | Auth middleware
        |--------------------------------------------------------------------------
        |
        | The middleware to use for feature toggle routes.
        |
        */
        'middleware' => [],

        /*
        |--------------------------------------------------------------------------
        | Prefix
        |--------------------------------------------------------------------------
        |
        | The route prefix to use for feature toggle routes.
        |
        */
        'prefix' => 'features',
    ],
];

命令

添加功能开关

$php artisan feature:add feature_name

删除功能开关

$php artisan feature:remove feature_name

启用功能开关

$php artisan feature:enable feature_name

禁用功能开关

$php artisan feature:disable feature_name

切换功能开关的状态

$php artisan feature:toggle feature_name

将角色附加到功能开关

$php artisan feature:add:role feature_name role_name

从功能开关中分离角色

$php artisan feature:remove:role feature_name role_name

待办事项

  • 验证规则
  • 调度检查

许可证

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