jkbennemann / laravel-features
The package provides a convenient way to introduce new functionalities into your application by simply switching them on or off or granting access only to specific groups of users.
Fund package maintenance!
jkbennemann
Requires
- php: ^8.0|^8.1
- illuminate/contracts: ^9.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- nunomaduro/collision: ^6.1
- nunomaduro/larastan: ^1.0
- orchestra/testbench: ^7
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-12-09 09:27:37 UTC
README
This packages provides a simple to use possibility to introduce functionality (features
)
to specific users or groups of users (parties
).
This concept is also known as FeatureFlags or FeatureToggles.
Installation
You can install the package via composer:
composer require jkbennemann/laravel-features
After you have installed the package run the following command start the installation routine
#Interactive installation php artisan feature:install #Command if you are using uuids for your model php artisan feature:install --uuid #Command if you are using unsigned big integer (id) php artisan feature:install --id
Last steps
Migrate database files
php artisan migrate
Adding HasFeatures
trait to your User
model
//.. use Jkbennemann\Features\Models\Traits\HasFeatures; class User extends Model { use HasFeatures; //.. }
After these steps you're good to go.
Common Use-Cases
1. A/B testing
- Add you
features
which should be tested - Create two
parties
and add users to them - Assign features to the parties or single users
2. A group of Beta-Testers
- Add you
features
which should be beta-tested - Create a new
party
fpr you beta-testers - Assign the users to that party
3. Preparation for an upcoming feature
- Add a new
feature
which should beINACTIVE
for now - Complete development and optionally create a special group for users e.g.
developers that should still be able to access that new feature - Extend that feature to beta-testers or remove feature-switch
4. Functionality for specific users like administrators
- Create a new
party
for administrators - Assign all
features
which should be explicitly available for them to that party - Add all relevant users to that party.
Usage
use \Jkbennemann\Features\Models\Enums\FeatureStatus; use \Jkbennemann\Features\Models\Feature; $feature = Features::create([ 'name' => 'Functionality A', 'description' => 'This is a new feature', //optional 'status' => FeatureStatus::ACTIVE //optional, defaults to INACTIVE ]); $party = Party::create([ 'name' => 'Beta Testers', 'description' => 'This is a new party', //optional 'status' => FeatureStatus::ACTIVE //optional, defaults to INACTIVE ]); $feature->activate(); //activates a feature $feature->deactivate(); //deactivates a feature $party->addFeature($feature); //assigns a feature to a party $party->removeFeature($feature); //removes a feature from a party $active = $user->hasFeature($feature); //user has the feature which is active $active = $user->hasFeature('feature-slug'); //you may provide the slug of a feature $active = $user->hasFeature('feature-slug', false); //provide false ignore the check for active features $active = $user->hasFeatureThroughParty('feature-slug'); //checks if a feature is granted through a party $features = $user->allFeatures(); //returns all features $features = $user->allFeatures(false); //returns all features without checking the status $user->giveFeature('feature-slug'); //add specific feature to a user $user->removeFeature('feature-slug'); //remove specific feature $user->joinParty('party-slug'); //add a user to a party $user->addToParty('party-slug'); //add a user to a party $user->leaveParty('party-slug'); //remove a user from a party $user->belongsToParty('party-slug'); //checks if the user belongs to the party $user->inParty('party-slug'); //checks if the user belongs to the party
Commands
# Create a new feature/party php artisan feature:add {name} {description?} {--status} php artisan party:add {name} {description?} {--status} # List features/parties php artisan feature:list php artisan party:list # Activate a feature/party php artisan feature:activate {id|slug} php artisan party:activate {id|slug} # Deactivate a feature/party php artisan feature:deactivate {id|slug} php artisan party:deactivate {id|slug}
Gates
# Check feature $user->can('feature-slug'); //allows to check using laravel gates $user->can('feature-slug', true); //validates if feature is ACTIVE $user->can('feature-slug', false); //ignores status of feature
Ideas
- Blade directives for
@feature
,@party
- Command to update feature status
- Gate support
- Support for UUIDs for
User
model - Support
can()
method to use accept aFeature
model - Middleware to secure requests for features/parties
- Feature expiration to tackle carying costs
- Management for Parties/Features using Livewire
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Feel free to make suggestions of features or contribute by creating a Pull Request.
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.