定制 xepozz/feature-flag 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

xepozz/feature-flag

Composer 安装命令:

composer require xepozz/feature-flag

包简介

Feature-flag management system for Yii 3

README 文档

README

This is a simple library to enable/disable features based on a set of rules.

Latest Stable Version Total Downloads phpunit codecov type-coverage

Installation

composer require xepozz/feature-flag

Configuration

Choose the driver you want to use. Currently, the library supports the following drivers:

InMemory

Configure the driver in the dependency injection container configuration file:

di.php

\Xepozz\FeatureFlag\Driver\InMemoryDriver::class => [
    '__construct()' => [
        'flags' => [
            155 => false,
            'feature_name' => true,
            FeaturesEnum::FEATURE_NAME => true,
        ],
    ],
],

Or with params.php:

'xepozz/feature-flag' => [
    'flags' => [
        155 => false,
        'feature_name' => true,
        FeaturesEnum::FEATURE_NAME => true,
    ],
],

Configuring the driver with params.php is only available for the InMemoryDriver.

Redis

Configure the driver in the dependency injection container configuration file:

di.php

\Xepozz\FeatureFlag\Driver\RedisDriver::class => function () {
    $redis = new Redis();
    $redis->pconnect(
        host: '127.0.0.1',
        port: 6379,
        timeout: 2.5,
    );

    return new \Xepozz\FeatureFlag\Driver\RedisDriver(redis: $redis, hashTableKey: 'ab');
},

The driver uses a hash table functions to store and retrieve data. Read more about the hash table functions here.

Choose a driver

After you have chosen a driver, you need to configure the dependency injection container:

di.php

use Xepozz\FeatureFlag\FlagStorageInterface;
use \Xepozz\FeatureFlag\Driver\RedisDriver;

return [
    // ...
    FlagStorageInterface::class => RedisDriver::class,
    // ...
]

Usage

Pull \Xepozz\FeatureFlag\FlagStorageInterface from the dependency injection container and use it:

isActive(string|int|BackedEnum $flag): bool

use Xepozz\FeatureFlag\FlagStorageInterface;

class Controller
{
    public function index(FlagStorageInterface $flagStorage)
    {
        if ($flagStorage->isActive('feature_name')) {
            // feature is enabled
        } else {
            // feature is disabled
        }
    }
}

setFlag(string|int|BackedEnum $flag, bool $active): void

Be careful, in case of using not the InMemoryDriver, the flag will be stored permanently.

In case of using the InMemoryDriver, the flag will be stored only for the current request. So you can switch the flag depending on the conditions in your code. For instance, you can enable the feature only for trusted IP addresses.

use Xepozz\FeatureFlag\FlagStorageInterface;

class Controller
{
    public function index(FlagStorageInterface $flagStorage)
    {
        if ($condition) {
            $flagStorage->setFlag('feature_name', true);
        }
    }
}

getAll(): array

Returns all flags as an associative array array<string, bool>.

The only InMemoryDriver supports returning BackendEnum as a key, because it does not need to serialize the key.

The key is the flag name, the value is the flag state.

use Xepozz\FeatureFlag\FlagStorageInterface;

class Controller
{
    public function index(FlagStorageInterface $flagStorage)
    {
        $flags = $flagStorage->getAll();
        // ...
    }
}

Testing

Redis

Redis driver requires phpredis extension and a running Redis server.

You can use the following command to start a Redis server in a Docker container:

docker run --rm -p 6379:6379 redis

Or use docker-compose:

docker-compose up -d

Run tests:

composer test

Or

./vendor/bin/phpunit

Looking for more modules?

  • Unique ID - Allows you to track the unique user in the application.
  • Request ID - A simple library to generate both unique request and response IDs for tracing purposes.
  • AB - A simple library to enable A/B testing based on a set of rules.
  • Shortcut - Sets of helper functions for rapid development of Yii 3 applications.

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

xepozz/feature-flag 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 04 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2023-04-07