markstory/cakephp-feature-flags 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

markstory/cakephp-feature-flags

Composer 安装命令:

composer require markstory/cakephp-feature-flags

包简介

Feature flags plugin for CakePHP

README 文档

README

FeatureFlags is a CakePHP plugin that enables your to use feature flags in your application to enable functionality based on simple application config, or a more complex rule based system.

By using feature flags you can separate your code deployments from what features are enabled, or have different features enabled in different environments. For example, you could have features that are incomplete, enabled in staging environments but disabled in production.

Installation

You can install this plugin into your application using composer.

The recommended way to install composer packages is:

composer require markstory/cakephp-feature-flags

Next, load the plugin by running the following command:

bin/cake plugin load FeatureFlags

Usage

First you need to decide if you want simple boolean feature flags, or more complex rule based feature flags. For the examples below, we'll assume you have two features (calendar-v2, and checkout-v2) that you want to conditionally enable.

Simple Feature Flags

First create a configuration file config/features.php, with the following:

<?php
return [
    'Features' => [
        'calendar-v2' => true,
        'checkout-v2' => false,
    ],
];

In your Application::services() method add the following:

use FeatureFlags\FeatureManagerInterface;
use FeatureFlags\Simple\FeatureManager;

public function services(ContainerInterface $container): void
{
    $container->addShared(FeatureManagerInterface::class, function () {
        return new FeatureManager(Configure::read('Features'));
    });
}

With the DI container setup, you can have CakePHP inject the FeatureManager into your controllers, and commands as required.

public function view(FeatureManagerInterface $features, $id)
{
    if ($features->has('calendar-v2')) {
        // Logic for the new feature.
        return $this->render();
    }
    ...
}

Rule Based Feature Flags

First create a configuration file config/features.php, with the following:

<?php
return [
    'Features' => [
        // Each key is a feature name
        'calendar-v2' => [
            // Features are composed of many segments.
            // All conditions in a segment must match for a feature to be
            // granted
            'segments' => [
                // Segments can incrementally enable features
                'rollout' => 50,
                // Segments are composed of multiple conditions
                'conditions' => [
                    [
                        'property' => 'user_email',
                        'op' => 'equal',
                        'value' => 'winner@example.com',
                    ]
                ],
            ],
        ],
    ],
];

In your Application::services() method add the following:

use FeatureFlags\FeatureManagerInterface;
use FeatureFlags\RuleBased\FeatureManager;

public function services(ContainerInterface $container): void
{
    $container->addShared(FeatureManagerInterface::class, function () {
        return new FeatureManager(
            function (array $data) {
                $context = [];
                // Add properties to `$context` based on the data you use
                // to check features.
                return $context;
            }
            Configure::read('Features')
        );
    });
}

With the DI container setup, you can have CakePHP inject the FeatureManager into your controllers, and commands as required.

public function view(FeatureManagerInterface $features, $id)
{
    // Including application data in `features->has` calls allows
    // you to build custom feature logic that fits your application.
    $identity = $this->request->getAttribute('identity');
    if ($features->has('calendar-v2', ['user' => $identity])) {
        // Logic for the new feature.
        return $this->render();
    }
    ...
}

Writing conditions

Conditions will safely extract keys out of the context that your application prepares. Each condition compares a single attribute in your context to a known value.

[
    'property' => 'user_email',
    'op' => 'equal',
    'value' => 'winner@example.com',
]

The following op values are supported:

  • equal Match if the context value matches value
  • not_equal Match if the context value is not equal to value
  • in Match if the context value is within the array of value.
  • not_in Match if the context value is not contained in the array of value.

markstory/cakephp-feature-flags 适用场景与选型建议

markstory/cakephp-feature-flags 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 619 次下载、GitHub Stars 达 8, 最近一次更新时间为 2024 年 09 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 619
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-03