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:
equalMatch if the context value matchesvaluenot_equalMatch if the context value is not equal tovalueinMatch if the context value is within the array ofvalue.not_inMatch if the context value is not contained in the array ofvalue.
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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 619
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-03