byjg / featureflag
Allows you to enable or disable features in your application and dispatch the proper code based on the flags.
Fund package maintenance!
byjg
Requires
- php: >=8.1 <8.4
Requires (Dev)
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^5.9
This package is auto-updated.
Last update: 2024-11-29 20:01:31 UTC
README
A simple feature flag dispatcher.
It allows you to define a list of features and dispatch the request to the proper handler based on the enabled feature flag.
Basic Usage
// Initialize the enabled features FeatureFlags::addFlag('flag1', 'value1'); FeatureFlags::addFlag('flag2', 'value2'); FeatureFlags::addFlag('flag3'); // Create a Dispatcher $dispatcher = new FeatureFlagDispatcher(); // Add a feature flag handler $dispatcher->add(FeatureFlagSelector::whenFlagIs('flag2', 'value1', function () {/** function1 */})); $dispatcher->add(FeatureFlagSelector::whenFlagIs('flag2', 'value2', function () {/** function2 */})); $dispatcher->add(FeatureFlagSelector::whenFlagIs('flag2', 'value3', [Someclass::class, 'method1'])); // Dispatch the request $dispatcher->dispatch(); // Since there is a feature flag 'flag2' with value 'value2' the function2 will be executed
Note that if one or more feature flags matches the condition, all of them will be executed in the order they were added.
Adding Dispatchers
Advanced Usage
Install
composer require "byjg/featureflag"
Unit tests
vendor/bin/phpunit