mileschou / toggle
PHP 的特性开关库
v2.0.0-beta.1
2022-02-14 10:38 UTC
Requires
- php: ^7.2 | ^8.0
- ext-json: *
- mileschou/toggle-simplify: ^1.1
Requires (Dev)
- hassankhan/config: ^0.11.2
- laravel/framework: ^6.20 | ^7 | ^8
- phpunit/phpunit: ^8.5 | ^9.5
- squizlabs/php_codesniffer: ^3.6
- symfony/yaml: ^4 | ^5
README
PHP 的特性开关库
概念
即将推出...
用法
Toggle
类是核心类。所有特性配置都将设置在此对象上。
特性开关
使用固定结果
<?php use MilesChou\Toggle\Toggle; $toggle = new Toggle(); $toggle->create('f1', true); // Will return true $toggle->isActive('f1');
使用带有固定结果的对象
<?php use MilesChou\Toggle\Feature; use MilesChou\Toggle\Toggle; $toggle = new Toggle(); $toggle->add('f1', Feature::create(true)); // Will return true $toggle->isActive('f1');
使用可调用函数动态决定返回值
<?php use MilesChou\Toggle\Toggle; $toggle = new Toggle(); $toggle->create('f1', function() { return true; }); // Will return true $toggle->isActive('f1');
使用带有上下文的可调用函数
<?php use MilesChou\Toggle\Toggle; $toggle = new Toggle(); $toggle->create('f1', function($context) { return $context['return']; }); // Will return true $toggle->isActive('f1', ['return' => true]);
参数
Feature
实例可以存储一些参数。例如
<?php use MilesChou\Toggle\Toggle; $toggle = new Toggle(); $toggle->create('f1', true, ['name' => 'Miles']); $toggle->create('f2', false, ['name' => 'Chou']); // Will return 'Chou' $toggle->feature('f1')->params('name'); // Also using in callback $toggle->create('f3', function($context, array $params) { return $params['key'] === $context['key']; }, ['key' => 'foo']);
控制结构
此代码段类似于 if
/ switch
结构
<?php use MilesChou\Toggle\Toggle; $toggle = new Toggle(); $toggle->create('f1'); $toggle->create('f2'); $toggle->create('f3'); $toggle ->when('f1', function ($context, $params) { // Something when f1 is on }) ->when('f2', function ($context, $params) { // Something when f2 is on }) ->when('f3', function ($context, $params) { // Something when f3 is on });
处理器
请参考 Toggle Processor 项目。