toggly/feature-management-php
Composer 安装命令:
composer require toggly/feature-management-php
包简介
Toggly Feature Management library for PHP with Laravel and WordPress support
README 文档
README
A comprehensive PHP library for Toggly feature management with native Laravel and WordPress support.
Features
- Full Feature Parity: Matches the functionality of the .NET Toggly.FeatureManagement library
- Signed Definitions: ECDSA signature verification for secure feature definitions
- Real-time Updates: WebSocket support for instant feature updates (with polling fallback)
- Usage Statistics: Automatic tracking of feature usage and user analytics
- Metrics Collection: Support for measurements, observations, and counters
- Snapshot Providers: Cache, database, and file-based snapshot storage
- Laravel Integration: Native Laravel service provider, facade, and middleware
- WordPress Plugin: Full WordPress plugin with admin interface and hooks
- PSR Standards: Built on PSR-4, PSR-11, PSR-16, PSR-18, and PSR-17
Installation
Composer
composer require toggly/feature-management-php
Quick Start
Laravel
- Register the service provider in
config/app.php:
'providers' => [ // ... Toggly\Laravel\ServiceProvider::class, ],
- Publish the configuration:
php artisan vendor:publish --tag=toggly-config
- Configure in
.env:
TOGGLY_APP_KEY=your-app-key TOGGLY_ENVIRONMENT=Production TOGGLY_USE_SIGNED_DEFINITIONS=false
- Use in your code:
use Toggly\Laravel\Facades\Toggly; // Check if feature is enabled if (Toggly::isEnabled('new-checkout')) { return view('checkout.v2'); } // With context $enabled = Toggly::isEnabledFor('premium-feature', [ 'userId' => $user->id, 'plan' => $user->plan ]); // State change handler Toggly::whenFeatureTurnsOn('new-api', function() { // Initialize new API }); // Record usage Toggly::recordUsage('feature-key'); // Record metrics Toggly::measure('checkout-completed', 125.50); Toggly::observe('active-users', 1500); Toggly::incrementCounter('api-calls', 1);
- Use middleware in routes:
Route::get('/new-feature', function () { return view('new-feature'); })->middleware('feature:new-feature');
WordPress
-
Install the plugin by copying to
wp-content/plugins/toggly/ -
Activate the plugin in WordPress admin
-
Configure in Settings > Toggly:
- App Key
- Environment
- Base URL (optional)
- Use Signed Definitions (optional)
-
Use in templates:
<?php if (toggly_is_enabled('new-header')): ?> <?php get_template_part('header', 'new'); ?> <?php endif; ?>
- Use shortcode:
[toggly_feature name="premium-content"]
<!-- Premium content here -->
[/toggly_feature]
- Use hooks in
functions.php:
add_action('toggly_feature_turns_on', function($featureKey) { if ($featureKey === 'new-theme') { // Activate new theme } });
Core Library Usage
Basic Usage
use Toggly\FeatureManagement\Config\TogglySettings; use Toggly\FeatureManagement\Core\FeatureProvider; use Toggly\FeatureManagement\Core\FeatureManager; use Toggly\FeatureManagement\Http\TogglyHttpClient; $settings = new TogglySettings([ 'app_key' => 'your-app-key', 'environment' => 'Production', ]); $httpClient = new TogglyHttpClient(/* PSR-18 client */, /* PSR-17 factory */, $settings->getBaseUrl()); $featureProvider = new FeatureProvider($settings, $httpClient, /* state service */); $featureManager = new FeatureManager($featureProvider, /* usage stats */, /* secure provider */); // Check feature if ($featureManager->isEnabled('my-feature')) { // Feature is enabled }
Snapshot Providers
Cache Provider (PSR-16)
use Toggly\FeatureManagement\Storage\SnapshotProviders\CacheSnapshotProvider; use Toggly\FeatureManagement\Storage\SnapshotSettings; $snapshotProvider = new CacheSnapshotProvider( $cache, // PSR-16 cache implementation new SnapshotSettings(['document_name' => 'toggly_features']), 86400 // TTL in seconds );
Database Provider (PDO)
use Toggly\FeatureManagement\Storage\SnapshotProviders\DatabaseSnapshotProvider; $snapshotProvider = new DatabaseSnapshotProvider( $pdo, // PDO instance new SnapshotSettings(['document_name' => 'toggly_features']) );
File Provider
use Toggly\FeatureManagement\Storage\SnapshotProviders\FileSnapshotProvider; $snapshotProvider = new FileSnapshotProvider( '/path/to/snapshots', new SnapshotSettings(['document_name' => 'toggly_features.json']) );
Configuration
TogglySettings
$settings = new TogglySettings([ 'app_key' => 'your-app-key', 'environment' => 'Production', 'base_url' => 'https://app.toggly.io/', 'use_signed_definitions' => true, 'allowed_key_ids' => ['key-id-1', 'key-id-2'], 'refresh_interval' => 300, // 5 minutes 'app_version' => '1.0.0', 'instance_name' => 'server-1', 'undefined_enabled_on_development' => false, ]);
Advanced Features
Feature State Change Handlers
$stateService = $container->get(FeatureStateServiceInterface::class); // Register callback $id = $stateService->whenFeatureTurnsOn('new-feature', function() { // Initialize feature }); // Unregister $stateService->unregisterFeatureStateChange('new-feature', $id);
Custom Metrics
$metricsService = $container->get(MetricsServiceInterface::class); // Record measurement (aggregated over time) $metricsService->measure('revenue', 1250.50); // Record observation (point-in-time) $metricsService->observe('active-users', 1500); // Increment counter $metricsService->incrementCounter('api-calls', 1);
Custom Context Provider
class MyContextProvider implements FeatureContextProviderInterface { public function getContextIdentifier(): ?string { // Return unique user identifier return $this->getCurrentUserId(); } // ... implement other methods }
Requirements
- PHP 7.4 or higher (8.1+ recommended)
- PSR-18 HTTP client (e.g., Guzzle, Symfony HTTP Client)
- PSR-16 cache (optional, for snapshot provider)
- PSR-11 container (optional, for dependency injection)
Laravel Requirements
- Laravel 8.0 or higher
illuminate/supportilluminate/http
WordPress Requirements
- WordPress 5.0 or higher
- No external dependencies (uses WordPress APIs)
License
MIT
Architecture
The library follows a modular architecture:
- Core Library: Framework-agnostic core functionality
- Laravel Integration: Service provider, facade, middleware, and filters
- WordPress Plugin: Full plugin with admin interface
Core Components
FeatureProvider: Fetches and manages feature definitionsFeatureManager: Evaluates features with stats trackingFeatureStateService: Manages state change notificationsUsageStatsProvider: Collects and sends usage statisticsMetricsService: Collects custom metrics for experimentsEcdsaSignatureVerifier: Verifies signed definitionsJwkManager: Manages JSON Web Keys for signature verification
Snapshot Providers
Three snapshot provider implementations are available:
- CacheSnapshotProvider: Uses PSR-16 cache (Redis, Memcached, etc.)
- DatabaseSnapshotProvider: Uses PDO (MySQL, PostgreSQL, SQLite)
- FileSnapshotProvider: Uses file system storage
Development
Running Tests
composer test
Code Style
The project follows PSR-12 coding standards.
Contributing
Contributions are welcome! Please read our contributing guidelines first.
License
MIT License - see LICENSE file for details.
Support
For support, visit https://toggly.io or open an issue.
toggly/feature-management-php 适用场景与选型建议
toggly/feature-management-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「wordpress」 「laravel」 「feature-flags」 「feature-toggles」 「ab-testing」 「feature-management」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 toggly/feature-management-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 toggly/feature-management-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 toggly/feature-management-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Pheature flags Doctrine DBAL toggle implementation library.
Pheature flags toggle CRUD library.
Alfabank REST API integration
Pheature flags Laminas Mezzio toggle.
Must-use plugin integrating WordPress with the Upsun platform: environment awareness, router-cache friendliness, safe preview clones, deploy migrations, Site Health checks, and a wp upsun CLI command.
Runtime feature flags for Craft CMS with targeting rules, percentage rollouts, and audit logging.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-04