phputil/flags
Composer 安装命令:
composer require phputil/flags
包简介
A lightweight, extensible feature flags framework for PHP
README 文档
README
🚩 A lightweight, customizable feature flags framework for PHP
You can customize:
- 🧠 How flags are evaluated, through Strategies.
- 💾 How flags are stored, through Storages.
- 📢 Who is notified about flag changes or removal, through Listeners.
Installation
PHP 7.4 or later. No external dependencies.
composer require phputil/flags
👉 You may also like to install some of the official extensions.
Extensions
Official extensions:
phputil/flags-pdo- a PDO-based storage.- ⏳ SOON -
phputil/flags-firebase- a Firebase-based storage. phputil/flags-webhooksa listener that works like a webhook, by notifying external APIs about flags' changes.
Third-party extensions:
- Create yours and open an Issue to be evaluated. It may appear here.
Usage
Basic flag checking:
require_once 'vendor/autoload.php'; use phputil\flags\FlagManager; // By default, it uses a storage-based strategy with an in-memory storage $flag = new FlagManager(); if ( $flag->isEnabled( 'my-cool-feature' ) ) { echo 'Cool feature available!', PHP_EOL; } else { echo 'Not-so-cool feature here', PHP_EOL; }
Customizing a certain verification:
// ... use phputil\flags\FlagVerificationStrategy; $flag = new FlagManager(); $myLuckBasedStrategy = new class implements FlagVerificationStrategy { function isEnabled( string $flag ): bool { return rand( 1, 100 ) >= 50; // 50% chance } }; if ( $flag->isEnabled( 'my-cool-feature', [ $myLuckBasedStrategy ] ) ) { echo 'Cool feature available!', PHP_EOL; } else { echo 'Not-so-cool feature here', PHP_EOL; }
Customizing all the verifications:
$flag = new FlagManager( null, [ $myLuckBasedStrategy ] ); if ( $flag->isEnabled( 'my-cool-feature' ) ) { ...
Setting a flag:
$flag->enable( 'my-cool-feature' ); $flag->disable( 'my-cool-feature' ); $flag->setEnable( 'my-cool-feature', true /* or false */ );
Removing a flag:
$flag->remove( 'my-cool-feature' );
Retrieving flag data:
$flagData = $flag->getStorage()->get( 'my-cool-feature' ); // null if not found
Adding a listener:
// ... use phputil\flags\FlagListener; use phputil\flags\FlagData; $myListener = new class implements FlagListener { public function notify( string $event, FlagData $flagData ): void { if ( $event === 'change' ) { echo 'Flag ', $flagData->key, ' is now ', $flagData->enabled ? 'enabled': 'disabled', PHP_EOL; } else if ( $event === 'removal' ) { echo 'Flag ', $flagData->key, ' was removed.', PHP_EOL; } } }; $flag->addListener( $myListener ); // v0.5.0+ // or $flag->getListeners()->add( $myListener ); $flag->enable( 'my-cool-feature' ); // Notify the listener
Customization
Storages
Use a different flag storage by:
- Creating your own, extending
FlagStorage; OR - Using an external storage extension.
How to configure it:
$storage = /* Create your storage here, e.g. new InMemoryStorage() */; $flag = new FlagManager( $storage );
Storages available in the framework:
InMemoryStorage, that store flags in memory.
Strategies
Use a flag verification strategy by:
- Creating your own, extending
FlagVerificationStrategy; OR - Using an external strategy extension.
How to configure it globally:
$strategies = [ /* pass your strategies here */ ]; $flag = new FlagManager( null, $strategies );
Strategies available in the framework:
StorageBasedVerificationStrategy, that checks flags in a storage.EnvBasedVerificationStrategy, that checks flags based on environment variables.
👉 A flag is considered enabled when all the strategies considered it enabled.
Listeners
Define a listener by:
- Creating your own, extending
FlagListener; OR - Using an external listener extension.
How to configure it:
$flag->addListener( $myListener ); // v0.5.0+ // or $flag->getListeners()->add( $myListener );
Roadmap
- Extensible library
- Official extensions:
- PDO-based storage
- Firebase-based storage
- Webhook-like listener
- REST API (external repository)
- Web-based control panel (external repository)
License
phputil/flags 适用场景与选型建议
phputil/flags 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 60 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 05 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「php」 「library」 「flag」 「feature」 「toggle」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phputil/flags 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phputil/flags 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phputil/flags 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Inbox pattern process implementation for your Laravel Applications
Core library that defines common interfaces used by the rest of the intahwebz..
Amqp classes
Provides command to manage a web library directory
Alfabank REST API integration
统计信息
- 总下载量: 60
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 7
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-10