togglebox/sdk
Composer 安装命令:
composer require togglebox/sdk
包简介
PHP SDK for ToggleBox - Remote Config, Feature Flags, and A/B Experiments
关键字:
README 文档
README
Official PHP SDK for ToggleBox - Remote Config, Feature Flags, and A/B Experiments.
Installation
composer require togglebox/sdk
Quick Start
use ToggleBox\ToggleBoxClient; use ToggleBox\Types\ClientOptions; use ToggleBox\Types\FlagContext; use ToggleBox\Types\ExperimentContext; // Initialize the client $client = new ToggleBoxClient(new ClientOptions( platform: 'web', environment: 'production', apiUrl: 'https://api.yourdomain.com', // Self-hosted // OR for cloud: // tenantSubdomain: 'your-tenant', apiKey: 'your-api-key', // Optional for self-hosted ));
Tier 1: Remote Configs
Remote configs provide the same value to everyone - perfect for feature toggles, API URLs, and app settings.
// Get a single config value with type-safe default $apiUrl = $client->getConfigValue('api_url', 'https://default.api.com'); $maxRetries = $client->getConfigValue('max_retries', 3); // Get all active config parameters as key-value array $allConfigs = $client->getAllConfigs(); // Returns: ['api_url' => 'https://...', 'max_retries' => 5, ...]
Tier 2: Feature Flags (2-Value Model)
Feature flags support targeting by country and language with a simple 2-value model (A/B).
$context = new FlagContext( userId: 'user-123', country: 'US', language: 'en', ); // Get flag result with full details $result = $client->getFlag('new-checkout-ui', $context); echo $result->value; // The actual value (valueA or valueB) echo $result->servedValue; // 'A' or 'B' echo $result->reason; // 'targeting_match', 'rollout', etc. // Simple boolean check if ($client->isFlagEnabled('dark-mode', $context)) { enableDarkMode(); } // Get flag metadata without evaluation $flagInfo = $client->getFlagInfo('dark-mode'); if ($flagInfo !== null) { echo $flagInfo->flagKey; // 'dark-mode' echo $flagInfo->name; // 'Dark Mode' echo $flagInfo->enabled; // true/false echo $flagInfo->valueA; // Value A echo $flagInfo->valueB; // Value B }
Tier 3: A/B Experiments
Full multi-variant experiments with traffic allocation and conversion tracking.
$context = new ExperimentContext( userId: 'user-123', country: 'US', language: 'en', ); // Get the user's assigned variation $variant = $client->getVariant('checkout-redesign', $context); if ($variant !== null) { echo $variant->variationKey; // 'control', 'variant_1', etc. echo $variant->variationName; // 'Control', 'Variant 1', etc. echo $variant->value; // The variation's value (JSON or string) echo $variant->isControl; // true/false } // Track a conversion use ToggleBox\Types\ConversionData; $client->trackConversion('checkout-redesign', $context, new ConversionData( metricName: 'purchase', value: 99.99, // Optional: for sum/average metrics )); // Get experiment metadata without assignment $experimentInfo = $client->getExperimentInfo('checkout-redesign'); if ($experimentInfo !== null) { echo $experimentInfo->experimentKey; // 'checkout-redesign' echo $experimentInfo->name; // 'Checkout Redesign' echo $experimentInfo->status; // 'running', 'draft', 'completed' print_r($experimentInfo->variations); // Array of variations } // Flush stats to server (call at end of request) $client->flushStats();
Caching
By default, the SDK uses an in-memory array cache that persists for the lifetime of the request. For better performance across requests, provide a PSR-16 compatible cache:
use ToggleBox\Cache\PsrCacheAdapter; // Using Symfony Cache $symfonyCache = new \Symfony\Component\Cache\Psr16Cache( new \Symfony\Component\Cache\Adapter\RedisAdapter($redis) ); $client = new ToggleBoxClient( new ClientOptions( platform: 'web', environment: 'production', apiUrl: 'https://api.yourdomain.com', cache: new CacheOptions( enabled: true, ttl: 300, // 5 minutes ), ), new PsrCacheAdapter($symfonyCache), );
Manual Cache Refresh
// Force refresh all cached data $client->refresh(); // Clear all caches $client->clearCache();
Health Check
// Check API connectivity try { $health = $client->checkConnection(); echo $health['status']; // 'ok' } catch (\ToggleBox\Exceptions\ToggleBoxException $e) { // API is unreachable log_error('ToggleBox API is down: ' . $e->getMessage()); }
Error Handling
use ToggleBox\Exceptions\ToggleBoxException; use ToggleBox\Exceptions\NetworkException; use ToggleBox\Exceptions\ConfigurationException; try { $variant = $client->getVariant('my-experiment', $context); } catch (NetworkException $e) { // API request failed log_error('ToggleBox API error: ' . $e->getMessage()); } catch (ToggleBoxException $e) { // Other SDK errors (e.g., experiment not found) log_error('ToggleBox error: ' . $e->getMessage()); }
Configuration Options
| Option | Type | Required | Description |
|---|---|---|---|
platform |
string | Yes | Platform name (e.g., 'web', 'mobile') |
environment |
string | Yes | Environment name (e.g., 'production', 'staging') |
apiUrl |
string | * | API base URL (for self-hosted) |
tenantSubdomain |
string | * | Tenant subdomain (for cloud) |
apiKey |
string | No | API key for authentication |
cache |
CacheOptions | No | Cache configuration |
configVersion |
string | No | Default config version ('stable', 'latest', or specific) |
* Either apiUrl or tenantSubdomain is required, but not both.
Requirements
- PHP 8.1 or higher
- Guzzle HTTP client
License
MIT
togglebox/sdk 适用场景与选型建议
togglebox/sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「analytics」 「feature-flags」 「ab-testing」 「experiments」 「configuration-management」 「experimentation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 togglebox/sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 togglebox/sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 togglebox/sdk 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Pheature flags Doctrine DBAL toggle implementation library.
🙋♀️ Minimal A/B Testing Library
Pheature flags toggle CRUD library.
Deck is a Laravel and PHP package for versioned, file-based AI prompt management with variable interpolation, performance tracking, A/B testing, and optional Laravel AI SDK integration.
Deck is a Laravel and PHP package for versioned, file-based AI prompt management with variable interpolation, performance tracking, A/B testing, and optional Laravel AI SDK integration.
Lightweight PHP micro-library for probabilistic and deterministic code execution — feature flags, A/B testing, gradual rollouts and sampling.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 31
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-29