teracrafts/flagkit
Composer 安装命令:
composer require teracrafts/flagkit
包简介
Official PHP SDK for FlagKit feature flag management
README 文档
README
Official PHP SDK for FlagKit feature flag service.
Requirements
- PHP 8.1 or later
- Composer
Installation
composer require teracrafts/flagkit
Features
- Type-safe evaluation - Boolean, string, number, and JSON flag types
- Local caching - Fast evaluations with configurable TTL and optional encryption
- Background polling - Automatic flag updates
- Event tracking - Analytics with batching and crash-resilient persistence
- Resilient - Circuit breaker, retry with exponential backoff, offline support
- Security - PII detection, request signing, bootstrap verification, timing attack protection
Quick Start
<?php use FlagKit\FlagKit; use FlagKit\FlagKitOptions; // Initialize the SDK $options = new FlagKitOptions(apiKey: 'sdk_your_api_key'); $client = FlagKit::initializeAndStart($options); // Identify user FlagKit::identify('user-123', [ 'plan' => 'premium', 'beta' => true, ]); // Evaluate flags $darkMode = FlagKit::getBooleanValue('dark-mode', defaultValue: false); $theme = FlagKit::getStringValue('theme', defaultValue: 'light'); $maxItems = FlagKit::getIntValue('max-items', defaultValue: 10); // Track events FlagKit::track('button_clicked', ['button' => 'signup']); // Cleanup when done FlagKit::close();
Configuration
<?php use FlagKit\FlagKitOptions; $options = FlagKitOptions::builder('sdk_your_api_key') ->pollingInterval(60) ->cacheTtl(600) ->maxCacheSize(500) ->cacheEnabled(true) ->eventBatchSize(20) ->eventFlushInterval(60) ->eventsEnabled(true) ->timeout(30) ->retryAttempts(5) ->build(); $client = FlagKit::initialize($options); $client->initialize();
Or using direct construction:
$options = new FlagKitOptions( apiKey: 'sdk_your_api_key', pollingInterval: 60, cacheTtl: 600, maxCacheSize: 500, );
The SDK also supports security-related configuration options such as PII detection, request signing, cache encryption, bootstrap signature verification, evaluation jitter, and error sanitization. These can be enabled through their respective builder methods or constructor parameters.
Evaluation Context
Provide context for targeting rules:
<?php use FlagKit\EvaluationContext; use FlagKit\FlagKit; // Using builder pattern $context = EvaluationContext::builder() ->userId('user-123') ->attribute('plan', 'premium') ->attribute('beta', true) ->attribute('score', 95.5) ->build(); $result = FlagKit::evaluate('feature-flag', $context); // Using fluent methods $context = (new EvaluationContext()) ->withUserId('user-123') ->withAttribute('plan', 'premium') ->withAttributes([ 'region' => 'us-east', 'beta' => true, ]);
Flag Evaluation
Basic Evaluation
// Boolean flags $enabled = FlagKit::getBooleanValue('feature-enabled', defaultValue: false); // String flags $variant = FlagKit::getStringValue('experiment-variant', defaultValue: 'control'); // Number flags $limit = FlagKit::getNumberValue('rate-limit', defaultValue: 100.0); $count = FlagKit::getIntValue('max-count', defaultValue: 10); // JSON flags $config = FlagKit::getJsonValue('feature-config', defaultValue: null);
Detailed Evaluation
$result = FlagKit::evaluate('feature-flag'); echo "Flag: " . $result->flagKey . "\n"; echo "Value: " . print_r($result->value->getRaw(), true) . "\n"; echo "Enabled: " . ($result->enabled ? 'true' : 'false') . "\n"; echo "Reason: " . $result->reason->value . "\n"; echo "Version: " . $result->version . "\n";
User Identification
// Identify user with attributes FlagKit::identify('user-123', [ 'email' => 'user@example.com', 'plan' => 'enterprise', 'created_at' => date('c'), ]); // Update context FlagKit::setContext( (new EvaluationContext()) ->withUserId('user-456') ->withAttribute('admin', true) ); // Clear context FlagKit::clearContext();
Analytics
// Track custom events FlagKit::track('purchase_completed', [ 'amount' => 99.99, 'currency' => 'USD', 'product_id' => 'prod-123', ]); // Flush pending events FlagKit::flush();
Bootstrap Data
Initialize with local flag data for instant evaluation:
$options = FlagKitOptions::builder('sdk_your_api_key') ->bootstrap([ 'dark-mode' => true, 'theme' => 'dark', 'max-items' => 50, ]) ->build(); $client = FlagKit::initialize($options); // Flags available immediately from bootstrap
Error Handling
<?php use FlagKit\FlagKitException; use FlagKit\ErrorCode; try { $client->initialize(); } catch (FlagKitException $e) { if ($e->isConfigError()) { echo "Configuration error: " . $e->getMessage() . "\n"; } elseif ($e->isNetworkError()) { echo "Network error: " . $e->getMessage() . "\n"; } else { echo "Error [{$e->getErrorCode()->value}]: " . $e->getMessage() . "\n"; } }
API Reference
FlagKit (Static Factory)
| Method | Description |
|---|---|
initialize($options) |
Initialize SDK with options |
initializeAndStart($options) |
Initialize and start SDK |
close() |
Close SDK and release resources |
identify($userId, $attributes) |
Set user context |
setContext($context) |
Set evaluation context |
clearContext() |
Clear evaluation context |
evaluate($flagKey, $context) |
Evaluate a flag |
getBooleanValue(...) |
Get boolean flag value |
getStringValue(...) |
Get string flag value |
getNumberValue(...) |
Get number flag value |
getIntValue(...) |
Get integer flag value |
getJsonValue(...) |
Get JSON flag value |
getAllFlags() |
Get all cached flags |
track($eventType, $data) |
Track custom event |
flush() |
Flush pending events |
FlagKitOptions
| Property | Default | Description |
|---|---|---|
apiKey |
(required) | API key for authentication |
pollingInterval |
30 | Polling interval (seconds) |
cacheTtl |
300 | Cache time-to-live (seconds) |
maxCacheSize |
1000 | Maximum cache entries |
cacheEnabled |
true | Enable caching |
eventBatchSize |
10 | Events per batch |
eventFlushInterval |
30 | Event flush interval (seconds) |
eventsEnabled |
true | Enable event tracking |
timeout |
10 | HTTP timeout (seconds) |
retryAttempts |
3 | Max retry attempts |
bootstrap |
null | Initial flag data |
Local Development
$options = FlagKitOptions::builder('sdk_your_api_key') ->build(); $client = FlagKit::initialize($options);
License
MIT License - see LICENSE file for details.
teracrafts/flagkit 适用场景与选型建议
teracrafts/flagkit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「feature-flags」 「feature-toggles」 「flagkit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 teracrafts/flagkit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 teracrafts/flagkit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 teracrafts/flagkit 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Pheature flags Doctrine DBAL toggle implementation library.
Pheature flags toggle CRUD library.
Official PHP SDK for Zenmanage feature flags with local evaluation
A simple and flexible feature toggle (feature flag) package for Laravel
Simple feature toggles for Laravel PHP and Javascript
Laravel package for Turtle Militia feature flag evaluation
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-08