dzunke/feature-flags-bundle

Symfony Bundle 用于在应用程序中实现功能标志

v6.1.1 2023-05-12 10:17 UTC

README

Build Status Latest Stable Version Total Downloads License

该Bundle允许您在应用程序中实现功能标志。请注意,没有接口可用,因此标志必须在Symfony-Configs中直接配置。

  • 如果需要支持Symfony 3.0,请使用版本 ^2.0!
  • 如果需要支持Symfony 4.2,请使用版本 ^4.0!
  • 如果需要支持Symfony 4.3或5.x,请使用版本 ^5.0!
  • 如果需要支持Symfony 6.0,请使用版本 ^6.0!
  • 如果需要支持Symfony >6.1,请使用版本 ^6.1!

文档

设置

您可以通过在shell上运行Composer或将它直接添加到您的composer.json中,来添加Bundle。

php composer.phar require dzunke/feature-flags-bundle:"^6.1"
"require" :  {
    "dzunke/feature-flags-bundle": "^6.1"
}

命名空间将通过Composer的自动加载进行注册,但为了使用symfony的集成功能,您必须注册Bundle。

# app/AppKernel.php
public function registerBundles()
{
    $bundles = [
        // [..]
        new DZunke\FeatureFlagsBundle\DZunkeFeatureFlagsBundle(),
    ];
}

在不进行任何配置的情况下,所有功能都将启用!但在此阶段,您可以开始开发了。

用法

服务容器

使用Bundle的最简单方法是获取容器并请求功能的状态。注意:未配置的功能默认启用。

# src/AcmeBundle/Controller/IndexController.php
<?php

namespace AcmeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
    public function indexAction()
    {
        if ($this->get('dz.feature_flags.toggle')->isActive('FooFeature')) {
           // [...]
        }
        // [...]
    }
}

Twig

# src/AcmeBundle/Resources/views/Index/index.html.twig
{% if has_feature('FooFeature') %}
    <p>Lorem Ipsum Dolor ...</p>
{% endif %}

参数使用

在每次检查时,如果您想指定检查,可以给检查提供参数。标志的参数可以通过验证方法上的数组定义。键必须与条件本身同名。请注意,如果条件不支持参数,则将忽略它们。

# src/AcmeBundle/Resources/views/Index/index.html.twig
{% if has_feature('FooBarFeature', {'device': 'tablet'}) %}
    <p>Lorem Ipsum Dolor ...</p>
{% endif %}

创建条件

首先必须创建条件。条件必须实现ConditionInterface。有一个通用的上下文可用。

<?php
# src/AcmeBundle/FeatureFlags/Condition/Foo.php
namespace AcmeBundle\FeatureFlags\Condition;

use DZunke\FeatureFlagsBundle\Toggle\Conditions\AbstractCondition;
use DZunke\FeatureFlagsBundle\Toggle\Conditions\ConditionInterface;

class Foo extends AbstractCondition implements ConditionInterface
{
    public function validate($config, $argument = null)
    {
        // [..] Implement your Methods to Validate the Feature

        return true;
    }

    public function __toString()
    {
        return 'Foo';
    }
}

类创建后,必须将其定义为标记化服务。通过这个标记和别名,条件将被加载。此时有许多空间可以通过添加调用或参数来扩展条件。

# src/AcmeBundle/Resources/config/services.yml
services:
    acme.feature_flags.condition.fo:
        class: DZunke\FeatureFlagsBundle\Toggle\Conditions\Foo
        calls:
            - [setContext, [@dz.feature_flags.context]]
        tags:
            -  { name: dz.feature_flags.toggle.condition, alias: foo }

配置

示例

d_zunke_feature_flags:
    flags:
        FooFeature: # feature will always be disabled
            default: false
        BarFeature: # feature will only be enabled for a list of special ClientIps
            conditions_config:
                ip_address: [192.168.0.1]
        BazFeature: # the feature will be enabled for the half of the users
            conditions_config:
                percentage:
                    percentage: 50
                    cookie: ExampleCookieForFeature
                    lifetime: 3600
        FooBarFeature:
            conditions_config:
                device:
                    tablet: "/ipad|playbook|android|kindle|opera mobi|arm|(^.*android(?:(?!mobile).)*$)/i"
                    mobile: "/iphone|ipod|bb10|meego|blackberry|windows\\sce|palm|windows phone|((android.*mobile))|mobile/i"

可用条件

hostname: [example.local, www.example.local]
ip_address: [192.168.0.1, 192.168.0.2]
percentage:
  cookie: NameThisCookieForTheUser # Default: 84a0b3f187a1d3bfefbb51d4b93074b1e5d9102a
  percentage: 29 # Default: 100
  lifetime: 3600 # Default: 86400 - 1 day
device:
  name: regex # give regex for each valid device
# See php.net/datetime
date:
  start_date: "2016-09-01" # Start date, accepts DateTime constructor values. Defaults to "now".
  end_date: "2016-09-03" # End date, accepts DateTime constructor values. Defaults to "now".

参考

d_zunke_feature_flags:
    # the default state to return for non-existent features
    default:              true
    # feature flags for the built system
    flags:
        # Prototype
        feature:
            # general active state for the flag - if conditions used it would be irrelevant
            default:              false
            # list of configured conditions which must be true to set this flag active
            conditions_config:    []

资源

许可证

FeatureFlagsBundle遵从MIT许可证。