twentytwo-labs/feature-flag-bundle

用于管理功能标志的捆绑包

1.0.5 2024-09-25 16:41 UTC

This package is auto-updated.

Last update: 2024-09-25 16:45:19 UTC


README

FeatureFlagBundle 是一个用于在 Symfony 应用程序中管理功能标志的捆绑包。

兼容性

此捆绑包至少与所有维护的 Symfony 版本进行了测试。

文档

安装

使用 composer 安装扩展

composer require twentytwo-labs/feature-flag-bundle

如果您不使用 Flex,请在您的 config/bundles.php 文件中启用捆绑包

<?php

return [
    // ...
    
    TwentytwoLabs\FeatureFlagBundle\TwentytwoLabsFeatureFlagBundle::class => ['all' => true],
];

配置

要配置和注册一个功能管理器,您需要一个工厂服务。您可能还需要更改工厂的一些选项。

# ...
twentytwo_labs_feature_flag:
    managers:
        default:
            factory: 'twenty-two-labs.feature-flags.factory.array'
            options:
                features:
                    my_feature_1: false
                    my_feature_2: true
                    my_feature3: '%env(bool:FEATURE_ENVVAR)%'

本捆绑包中包含的工厂可以在下表中找到。

示例配置

# ...
twentytwo_labs_feature_flag:
    managers:
        default:
            factory: twenty-two-labs.feature-flags.factory.array
            options:
                features:
                    my_feature_1:
                        enabled: false
                        description: MyFeature1 description text
                    my_feature_2:
                        enabled: true
                        description: MyFeature2 description text
                        expression: "is_granted('ROLE_ADMIN')"
                    my_feature3:
                        enabled: '%env(bool:FEATURE_ENVVAR)%'
                        description: MyFeature3 description text

您可以声明多个管理器。多个提供者对于您想使用不同的存储提供者或隔离您的功能标志很有用。

# ...
twentytwo_labs_feature_flag:
    managers:
        manager_foo:
            factory: twenty-two-labs.feature-flags.factory.array
            options:
                features:
                    my_feature_1:
                        enabled: false
                        description: MyFeature1 description text
                    my_feature_2:
                        enabled: true
                        description: MyFeature2 description text
                    my_feature3:
                        enabled: '%env(bool:FEATURE_ENVVAR)%'
                        description: MyFeature3 description text
        manager_bar:
            factory: twenty-two-labs.feature-flags.factory.array
            options:
                features:
                    my_feature_4:
                        enabled: false
                        description: MyFeature4 description text
                        expression: "is_granted('ROLE_ADMIN')"
                    my_feature_5: []
                    my_feature_6: ~
                    my_feature_7: false

当定义了多个管理器时,它们将按照以下命名约定作为服务注册到 Symfony 依赖注入容器中: twentytwo_labs_feature_flag.manager.<manager_name>

例如,manager_bar 可以通过以下服务名称访问: twentytwo_labs_feature_flag.manager.manager_bar

管理器存储也按照以下命名约定作为服务注册到 Symfony 依赖注入容器中: twentytwo_labs_feature_flag.storage.<manager_name>

缓存

您可以缓存所有功能标志,PSR6 兼容

twentytwo_labs_feature_flag:
    cache:
       enabled: true
       provider: 'service_name'
       expires_after: 3600 #default

使用它

作为服务

捆绑包添加了一个全局的 ChainedFeatureManager 服务,您可以在您的 PHP 类中使用它。

use App\Controller;
// ...

class MyController extends Controller
{
    public function myAction(ChainedFeatureManager $featureManager): Response
    {
        if ($featureManager->isEnabled('my_feature_1')) {
            // my_feature_1 is enabled
        }

        if ($featureManager->isDisabled('my_feature_2')) {
            // my_feature_2 is not enabled
        }

        // ...
    }
}

在您的 Twig 模板中

您也可以在模板中检查一个标志

{% if isFeatureEnabled('my_feature_1') %}
    {% include 'feature1_template.html.twig' %}
{% endif %}

{% if isFeatureDisabled('my_feature_2') %}
    {% include 'feature2_template.html.twig' %}
{% endif %}

在路由配置中

此软件包允许您通过在您的路由定义中添加一些配置来限制控制器访问。

# app/config/routing.yml
my_first_route:
    path: /my/first/route
    defaults:
        _controller: AppBundle:Default:index
        _features:
            - { feature: my_feature_key, enabled: false } # The action is accessible if "my_feature_key" is disabled

my_second_route:
    path: /my/second-route
    defaults:
        _controller: AppBundle:Default:second
        _features:
            - { feature: foo } # The action is accessible if "foo" is enabled ...
            - { feature: bar, enabled: true } # ... and "bar" feature is also enabled

作为控制器属性

您还可以通过属性来限制控制器访问

#[Feature(name: "foo", enabled: true)]
class MyController extends Controller
{
    #[Feature(name: "foo")]
    public function annotationFooEnabledAction(): Response
    {
        return new Response('MyController::annotationFooEnabledAction');
    }

    #[Feature(name: "foo", enabled: true)]
    public function annotationFooEnabledBisAction(): Response
    {
        return new Response('MyController::annotationFooEnabledAction');
    }

    #[Feature(name: "foo", enabled: false)]
    public function annotationFooDisabledAction(): Response
    {
        return new Response('MyController::annotationFooDisabledAction');
    }
}

实现自己的存储提供者

  1. 首先,您需要创建一个实现 TwentytwoLabs\FeatureFlagBundle\Storage\StorageInterface 接口的自定义存储提供者类
  2. 然后,您需要创建一个实现 TwentytwoLabs\FeatureFlagBundle\Factory\StorageFactoryInterface 接口的工厂类,并创建您自定义的存储
  3. 在 Symfony 依赖注入容器中注册它
  4. 在管理器配置中指定您想要使用的存储
twentytwo_labs_feature_flag:
    manager:
        manager_name:
            storage: your.custom.service.name
            options:
                # arguments need to create the storage service

许可

此库在 MIT 许可证 下发布