vladab/feature-flags-bundle

Symfony2 Bundle 用于在您的应用程序中实现功能标志

2.1.0 2016-09-05 08:56 UTC

This package is not auto-updated.

Last update: 2024-09-23 13:31:42 UTC


README

SensioLabsInsight TravisCI

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

如果需要 Symfony 3.0 支持,请使用版本 >= 2.0!

文档

设置

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

php composer.phar require dzunke/feature-flags-bundle:dev-master
"require" :  {
    "dzunke/feature-flags-bundle": "dev-master"
}

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 许可证下授权。