novaway/feature-flag-bundle

非常简单的功能标志管理包

3.0.0-beta1 2023-10-27 06:29 UTC

This package is auto-updated.

Last update: 2024-09-07 07:36:44 UTC


README

Build Status Coverage License Latest Stable Version Latest Unstable Version Total Downloads

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

⚠️ 你目前正在阅读下一个主要版本的文档。要阅读稳定版本的文档,请参阅 2.x 文档

兼容性

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

文档

安装它

使用 composer 安装扩展

composer require novaway/feature-flag-bundle

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

<?php

return [
    // ...
    Novaway\Bundle\FeatureFlagBundle\NovawayFeatureFlagBundle::class => ['all' => true],
];

配置

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

# ...
novaway_feature_flag:
    default_manager: default
    managers:
        default:
            factory: 'novaway_feature_flag.factory.array'
            options:
                features:
                    my_feature_1: false
                    my_feature_2: true
                    my_feature3: '%env(bool:FEATURE_ENVVAR)%'

此包附带的工厂可以在下表中找到。

示例配置

# ...
novaway_feature_flag:
    default_manager: default
    managers:
        default:
            factory: novaway_feature_flag.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

你可以声明多个管理器。多个提供者是如果你想要使用不同的存储提供者或隔离你的功能标志时的有用。

# ...
novaway_feature_flag:
    default_manager: manager_foo
    managers:
        manager_foo:
            factory: novaway_feature_flag.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: novaway_feature_flag.factory.array
            options:
                features:
                    my_feature_4:
                        enabled: false
                        description: MyFeature4 description text
                    my_feature_5: []
                    my_feature_6: ~
                    my_feature_7: false

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

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

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

作为服务使用

此包添加了一个全局的 novaway_feature_flag.manager(也绑定到 FeatureManager)服务,你可以在你的 PHP 类中使用它。

如果你定义了多个管理器,该服务使用 ChainedFeatureManager 类来连接所有声明的管理器。

use Novaway\Bundle\FeatureFlagBundle\Manager\FeatureManager;
// ...

class MyController extends Controller
{
    public function myAction(FeatureManager $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: feature-42, enabled: true, exceptionClass: Symfony\Component\HttpKernel\Exception\BadRequestHttpException } # will throw a BadRequestHttpException if "feature-42" is disabled
            - { feature: feature-44, enabled: true, exceptionFactory: Symfony\Component\HttpKernel\Exception\BadRequestHttpExceptionFactory } # will use the BadRequestHttpExceptionFactory registered factory class to create the exception to be thrown

作为控制器属性

你还可以使用属性来限制控制器访问,有两个属性可用

  • Novaway\Bundle\FeatureFlagBundle\Attribute\FeatureEnabled
  • Novaway\Bundle\FeatureFlagBundle\Attribute\FeatureDisabled
#[FeatureEnabled(name: "foo")]
class MyController extends Controller
{
    #[FeatureEnabled(name: "foo", exceptionClass: BadRequestHttpException::class)]
    public function annotationFooEnabledAction(): Response
    {
        return new Response('MyController::annotationFooEnabledAction');
    }

    #[FeatureDisabled(name: "foo", exceptionFactory: MyExceptionFactory::class)]
    public function annotationFooDisabledAction(): Response
    {
        return new Response('MyController::annotationFooDisabledAction');
    }
}

实现你自己的存储提供者

  1. 首先你需要创建一个实现 Novaway\Bundle\FeatureFlagBundle\Storage\StorageInterface 接口的自定义存储提供者类
  2. 将其注册到 Symfony 依赖注入容器中
  3. 在管理器配置中指定你想要使用的存储
novaway_feature_flag:
    manager:
        manager_name:
            storage: your.custom.service.name
            options:
                # arguments need to create the storage service

当你创建一个存储时,会调用静态方法 create 来创建存储实例。

许可证

此库根据 MIT 许可证 发布