playox / feature-tox
功能开关的强化版。
Requires
- php: ^7.4||^8.0
- psr/cache: ^1.0||^2.0||^3.0
- symfony/expression-language: >=2.7
Requires (Dev)
- php-coveralls/php-coveralls: ^2.4.3
- phpunit/phpunit: *
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2023-09-10 12:56:10 UTC
README
FeatureTox 是一个简单而强大的功能开关库。只需几行配置 - 仍然非常灵活和可扩展。此库基于 https://github.com/bestit/flagception-sdk 并在此基础上进一步开发。
下载库
打开命令行,进入您的项目目录,然后执行以下命令以下载此库的最新稳定版本
$ composer require playox/FeatureTox
快速示例
只需创建一个 FeatureManager
实例并将您的激活器传递以启动功能开关。
// MyClass.php class MyClass { public function doSomething() { // The activator decide if the feature is active or not // You can use your own activator if you implement the interface $activator = new ArrayActivator(); $manager = new FeatureManager($activator); if ($manager->isActive('your_feature_name')) { // do something } } }
激活器是最重要的类,它决定给定的功能是否激活。ArrayActivator
需要一个包含激活功能名称的数组作为构造函数参数。如果请求的功能在数组中,它将返回 true,否则返回 false。示例
// MyClass.php class MyClass { public function doSomething() { $activator = new ArrayActivator([ 'feature_abc', 'feature_def', 'feature_ghi' ]); $manager = new FeatureManager($activator); // Will return true if ($manager->isActive('feature_def')) { // do something } // Will return false if ($manager->isActive('feature_wxy')) { // do something } } }
此库提供 ArrayActivator、ConstraintActivator、EnvironmentActivator、CookieActivator 和 ChainActivator。
在大多数情况下,您将创建自己的激活器(例如,对于 doctrine)。只需实现 FeatureActivatorInterface
。
如果您想缓存某些耗时激活器的结果,可以使用 CacheActivator。
高级示例
有时您的激活器需要更多上下文来决定功能是否激活。您可以可选地向管理器添加上下文对象作为第二个参数,并在您的激活器中检查上下文数据。
示例
// MyClass.php class MyClass { public function doSomething(User $user) { $activator = new YourCustomDoctrineActivator(); $manager = new FeatureManager($activator); $context = new Context(); $context->add('user_id', $user->getId()); // Check the feature with context if ($manager->isActive('feature_def', $context)) { // do something } // Check the feature without context (result may differ from above) if ($manager->isActive('feature_def')) { // do something } } } // YourCustomDoctrineActivator.php class YourCustomDoctrineActivator implements FeatureActivatorInterface { public function isActive($name, Context $context) { return $context->get('user_id') === 12; } }
您还可以全局添加上下文数据,而不是将上下文添加到每个功能请求中。只需将实现 ContextDecoratorInterface
的类作为第二个参数传递给功能管理器构造函数。
// MyClass.php class MyClass { public function doSomething(User $user) { $activator = new YourCustomDoctrineActivator(); $decorator = new ArrayDecorator([ 'user_id' => $user->getId() ]); $manager = new FeatureManager($activator, $decorator); // Check the feature with the global defined context if ($manager->isActive('feature_def')) { // do something } } } //YourCustomDoctrineActivator.php class YourCustomDoctrineActivator implements FeatureActivatorInterface { public function isActive($name, Context $context) { return $context->get('user_id') === 12; } }
您还可以混合这两种变体
// MyClass.php class MyClass { public function doSomething(User $user) { $activator = new YourCustomDoctrineActivator(); $decorator = new ArrayDecorator([ 'user_id' => $user->getId() ]); $manager = new FeatureManager($activator, $decorator); $context = new Context(); $context->add('user_name', $user->getUsername()); // Check the feature with the global defined context if ($manager->isActive('feature_def', $context)) { // do something } } }
此库提供 ArrayDecorator 和 ChainDecorator。