承接 novaway/feature-flag-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

novaway/feature-flag-bundle

Composer 安装命令:

composer require novaway/feature-flag-bundle

包简介

Very KISS bundle to manage features flag

README 文档

README

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

The FeatureFlagBundle is a bundle to manage features flags in your Symfony applications.

Compatibility

This bundle is tested with at least all maintained Symfony version.

Documentation

Install it

Install extension using composer:

composer require novaway/feature-flag-bundle

If you don't use Flex, enable the bundle in your config/bundles.php file:

<?php

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

Configuration

To configure and register a feature manager you need a factory service. You may also need to change some options to the factory.

# ...
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)%'

The factories that come with this bundle can be found in the table below.

Factory service id Options
novaway_feature_flag.factory.array features

Example configuration

# ...
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

You can declare multiple managers. Multiple providers is useful if you want to use different storage providers or to isolate your features flags.

# ...
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

When several managers are defined, they are registered in the Symfony dependency injection container as services with the following naming convention: novaway_feature_flag.manager.<manager_name>.

For example, the manager_bar is accessible with the following service name: novaway_feature_flag.manager.manager_bar.

Manager storage are also registered in the Symfony dependency injection container as services with the following naming convention: novaway_feature_flag.storage.<manager_name>.

Use it as a service

The bundle adds a global novaway_feature_flag.manager (also bind to FeatureManager) service you can use in your PHP classes.

In the case you have defined several managers, the service use the ChainedFeatureManager class to chain all declared managers.

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
        }

        // ...
    }
}

In your Twig templates

You can also check a flag in your templates:

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

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

In the routing configuration

The package allows you to restrict a controller access by adding some configuration in your routing definition.

# 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

As a controller attribute

You can also restrict a controller access with attributes, two attributes are available:

  • 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');
    }
}

API Controller

The bundle provides an API controller that exposes feature flags via REST endpoints:

  • GET /features - Get all feature flags

To enable the API controller, you need to register its routes in your routing configuration:

# config/routes.yaml (or app/config/routing.yml for older Symfony versions)
feature_api:
    resource: '@NovawayFeatureFlagBundle/Resources/config/routing.yml'

You can also register endpoints manually:

features_all:
    path: /features
    defaults:
        _controller: Novaway\Bundle\FeatureFlagBundle\Controller\FeatureApiController::all
    methods: [GET]

The API returns JSON responses with feature flag data including key, description, and enabled status.

Implement your own storage provider

  1. First your need to create your storage provider class which implement the Novaway\Bundle\FeatureFlagBundle\Storage\StorageInterface interface
  2. Register it in the Symfony dependency injection container
  3. Specify the storage you want to use in a manager configuration
novaway_feature_flag:
    manager:
        manager_name:
            storage: your.custom.service.name
            options:
                # arguments need to create the storage service

When you create a storage, the static method create is called to create the storage instance.

License

This library is published under MIT license

novaway/feature-flag-bundle 适用场景与选型建议

novaway/feature-flag-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 290.25k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2016 年 10 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「flag」 「feature」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 novaway/feature-flag-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 novaway/feature-flag-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 novaway/feature-flag-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 290.25k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 24
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 23
  • Watchers: 6
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-10-12