rescaled / simple-feature
This package aims to provide a fully-featured, yet simple feature flag configuration through environment variables for Laravel 7+
Fund package maintenance!
rescaled
Requires
- php: ^8.0
- illuminate/contracts: ^8.73|^9.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- nunomaduro/collision: ^5.10
- nunomaduro/larastan: ^1.0
- orchestra/testbench: ^6.22
- 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
This package is auto-updated.
Last update: 2024-11-09 13:24:16 UTC
README
Simple Feature allows you to define feature flags via environment variables and check for their state within your Laravel application. It also provides pre-defined middleware you can utilize for this use case.
Installation
You can install the package via composer:
composer require rescaled/simple-feature
Usage
Define feature flags
Feature flags are defined in your environment file. They must be defined in snake case as well as being prefixed with FEATURE_
.
FEATURE_FIRST_TEST=true FEATURE_SECOND_TEST=true FEATURE_THIRD_TEST=false FEATURE_FOURTH_TEST=false
Direct usage
You can directly access the package's methods as following.
SimpleFeature::enabled('firstTest') // true SimpleFeature::disabled('firstTest') // false SimpleFeature::allEnabled(['firstTest', 'secondTest']) // true SimpleFeature::allDisabled(['thirdTest', 'fourthTest']) // true SimpleFeature::allEnabled(['firstTest', 'thirdTest']) // false SimpleFeature::allDisabled(['firstTest', 'thirdTest']) // false
Middleware
The package comes with two middlewares that allow to check whether a given set of features is enabled or disabled.
// FEATURE_REGISTRATION=true // FEATURE_ON_PREMISE=true Route::get('/register', [RegistrationController::class, 'create'])->middleware('feature.enabled:registration'); Route::get('/billing', [BillingController, 'show'])->middleware('feature.disabled:onPremise');
If the feature hasn't the desired state the middleware will abort the request with a 404.
Blade
Furthermore, you can use conditional Blade directives to render content based on the state of a given feature flag.
@feature('registration') <a href="/register">Register</a> @endfeature
@unlessfeature('onPremise') ... @endunlessfeature
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
If you discover any security-related issues, please email security@rescaled.de instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.