mohamedahmed01 / feature-flag
easily switch on and off features using a simple command
v1.0.2
2023-04-22 21:56 UTC
Requires
- php: ^7.4|^8.0
Requires (Dev)
- orchestra/testbench: ^5.0|^6.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2025-03-31 17:58:38 UTC
README
Introduction
Feature Flag is the easiest way to enable and disable features on your different Laravel enviroments it also enable you to do A/B testing and supporting your users with different features sets.
Installtion
- Adding the package using composer
composer require mohamedahmed01/feature-flag
- publishing configuration
php artisan vendor:publish --provider="Mohamedahmed01\FeatureFlag\FeatureFlagServiceProvider" --tag="config"
- Facade is automatically loaded using composer unless you have laravel version < 5.4 you will need to add the facade manually in config/app.php
'FeatureFlag'=> Mohamedahmed01\FeatureFlag\FeatureFlagFacade::class
Usage
//Feature Flagging can be simple done by creating the flag $featureFlag = new FeatureFlag([ 'name' => 'test', 'description' => 'Test feature flag', ]); $featureFlag->save(); //using the flag to scope your your code using if condition or any other way you like if ($featureFlag->isEnabled()) { // Implement the feature for the user } //or you can use the method Targted and checking the audience to match to specific audience if($featureFlag->isTargeted() && in_array($user->id, $featureFlag->getAudience())) { // Implement the feature for the user } //Feature Flagging can be also used to target users based on percentage $featureFlag = new FeatureFlag([ 'name' => 'test', 'description' => 'Test feature flag', 'percentage'=>50 ]); $featureFlag->save(); // you can use the method isEnabledForUser and checking the audience to match to specific audience i.e 50% if($featureFlag->isEnabledForUser($user)) { // Implement the feature for the user } //Feature Flagging can be also used to flag based on datetime $featureFlag = new FeatureFlag([ 'name' => 'test flag', 'finish_date' => '01/04/2023', 'enabled' => true, ]); $featureFlag->save(); // you can use the env either to throw an exception or send a notification when flag expires if($featureFlag->isEnabled()) { // Implement the feature for the user } //then simply call feature-flag:manage followed by the name of your flag to enable Or disable it php artisan feature-flag:manage {flag : The name of the feature flag} {--enable : Enable the feature flag} {--disable : Disable the feature flag}
Configuration :
Config Name | Description |
---|---|
FEATURE_FLAG_DRIVER | Allow you to change the backend driver currently support eleqount |
FEATURE_FLAG_FINISH_DATE_ACTION | Choose What to do when a date based feature flag expires options "exception,notification" |
FEATURE_FLAG_NOTIFICATION_EMAIL | set the email to receive on notifications in case you used notification option |
Methods :
Method | Description |
---|---|
getName() | getthe name of the flag |
getDescription() | get the description of the flag |
setEnabled(bool) | enable the flag or disable it depending on the value |
isEnabled() | check if the flag is enabled or not |
Targted() | check if the flag has specific audience |
setAudience(array) | set the flag audience by sending array of user's id's i.e [1,2,3,4] |
getAudience() | get the audience id's of the users for this flag |
isEnabledForUser($user) | check random user for falling within the percentage |
Contributing
Thank you for considering contributing to Feature-Flag! You can read the contribution guide here.
Testing
composer test
Security
If you discover any security related issues, please email mohamedabdelmenem01@gmail.com instead of using the issue tracker.
License
Feature-Flag is open-sourced software licensed under the MIT license.