vehikl / flip
功能开关实现
0.2.0
2018-09-13 03:31 UTC
Requires
- php: ^7.1
Requires (Dev)
- phpunit/phpunit: ^7.0
- squizlabs/php_codesniffer: ^3.3
This package is auto-updated.
Last update: 2024-09-17 05:56:03 UTC
README
Flip 是一个简单的功能开关实现。功能作为独立的类实现,并被“混合”到您希望公开功能的类中。
安装
通过 Composer
$ composer require vehikl/flip
用法
Flip 功能只是一个带有几个必需方法的常规 PHP 类。
enabled
- 此方法返回一个布尔值,表示功能是否启用
toggles
- 此方法返回一个包含可用功能开关的数组。数组以调用运行功能的方法的名称为键。每个键的值是一个关联数组,具有 on
和 off
键,每个键映射到在功能“开启”或“关闭”时调用的相应方法。
class SomeFeature extends \Vehikl\Flip\Feature { /** * Decides under which conditions this Feature is enabled */ public function enabled() { return random_int(0, 1) == 1; } /** * Returns an array of available toggles for this feature */ public function toggles() { return [ 'someToggle' => [ 'on' => 'whenOn', 'off' => 'whenOff' ] ]; } public function whenOn() { return "I'm on!"; } public function whenOff() { return "I'm off!"; } } class SomeClass { use Vehikl\Flip\Featurable; protected $features = [SomeFeature::class]; public function someBehaviour() { // no need for if/else blocks, just call the toggle using the // `flip` helper return $this->flip()->someToggle(); } }
强制功能开启或关闭
您可以通过分别调用 alwaysOn
或 alwaysOff
静态方法来强制功能为“开启”或“关闭”。这将强制该类的所有功能要么“开启”,要么“关闭”,而不管它们的 enabled
方法如何评估。
class SomeFeature extends \Vehikl\Flip\Feature { // include the $forcedState static variable if you want to enable forcing state protected static $forcedState; /** * Decides under which conditions this Feature is enabled */ public function enabled() { return random_int(0, 1) == 1; } /** * Returns an array of available toggles for this feature */ public function toggles() { return [ 'someToggle' => [ 'on' => 'whenOn', 'off' => 'whenOff' ] ]; } public function whenOn() { return "I'm on!"; } public function whenOff() { return "I'm off!"; } } class SomeClass { use Vehikl\Flip\Featurable; protected $features = [SomeFeature::class]; public function someBehaviour() { // no need for if/else blocks, just call the toggle using the // `flip` helper return $this->flip()->someToggle(); } } // force the SomeFeature feature to be always on SomeFeature::alwaysOn() // anytime `someToggle` is called on instances of SomeClass, // the `on` version of `someToggle` will be run $someObject = new SomeClass; $someObject->someBehaviour(); // always returns "I'm on!"
变更日志
有关最近更改的更多信息,请参阅 变更日志。
测试
$ composer test
贡献
有关详细信息,请参阅 贡献指南。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 go@vehikl.com 联系,而不是使用问题跟踪器。
鸣谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。