Download the PHP package ericdowell/feature-toggle without Composer
On this page you can find all versions of the php package ericdowell/feature-toggle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ericdowell/feature-toggle
More information about ericdowell/feature-toggle
Files in ericdowell/feature-toggle
Package feature-toggle
Short Description Simple feature toggle api for Laravel applications.
License MIT
Informations about the package feature-toggle
Feature Toggles
A simple feature toggle api for Laravel applications.
Table of Contents
- Installation
- Testing
- Usage
- Toggle Booting
- Helper Functions
- Use with Laravel Blade Custom Directive
- Use with Laravel Middleware
- Use with Laravel Task Scheduling
- Use with Laravel Validation
- Simple String
- Via Illuminate\Validation\Rule
- requiredIfRule Method on FeatureToggleApi
- Toggle Providers
- Add Additional Toggle Providers
- Local Feature Toggles
- Toggle Parsing
- Conditional Feature Toggles
- Eloquent Feature Toggles
- Database Migration
- Eloquent Model
- QueryString Toggle Provider
- Configure Query String Keys
- Add Api Key Authorization
- Redis Toggle Provider
- Session Toggle Provider
- Frontend Feature Toggle Api
- Road Map
Installation
Install using composer by running:
Publish the feature-toggle.php config file by running:
Testing
Run composer test.
Usage
If a feature toggle is not defined then isActive will return false.
Toggle Booting
The Feature Toggle Api will pull all possible toggles at the boot of the application. This design allows there to be one database/cache/redis query instead of possibly many calls. This only becomes a problem if there're 100s of feature toggles.
Be mindful of how many database toggles are setup at given time, instead setup or move toggles to the local provider
in config/feature-toggle.php.
Helper Functions
feature_toggle_api:
Or a shorter function that does the same as above called feature_toggle:
The feature_toggle function also allows a second parameter to be passed to allow for checking if the toggle is
active (true) or if it is inactive (false):
The second parameter will parse as the local toggle does, read more in the Toggle Parsing section to learn more.
Use with Laravel Blade Custom Directive
This custom directive uses the feature_toggle helper function directly, you can expect the same behavior:
Or if you'd like to check if the Example is inactive then you may pass a falsy value as the second parameter:
Or you can use the normal @if blade directive and call feature_toggle function directly:
Use with Laravel Middleware
The middleware signature is as follows:
Where status and abort are optional parameters. status will default to true (truthy) and abort will default to
404 status code. name is required.
Examples:
Use with Laravel Task Scheduling
You can use the built-in when function in combination with the feature_toggle helper function in the app/Console/Kernel.php
schedule method.
Use with Laravel Validation
There are three ways you can use the validation logic:
- Simple String
- Via
Illuminate\Validation\Rule requiredIfRuleMethod onFeatureToggleApi
Simple String
Use the normal simple string signature via required_if_feature:
Where status is an optional parameter. status will default to true (truthy). name parameter is required.
Via Illuminate\Validation\Rule
A macro method has been added to the Rule class called requiredIfFeature:
requiredIfRule Method on FeatureToggleApi
You may also use the requiredIfRule method on the FeatureToggleApi/feature_toggle_api Facade or helper function:
Toggle Providers
The default feature toggle providers are as follows:
conditionaleloquentlocal(config)querystringredissession
You can pass the feature_toggle_api helper function one of the above strings to get the toggle provider:
Or you can access each toggle provider via:
feature_toggle_api()->getConditionalProvider()feature_toggle_api()->getEloquentProvider()feature_toggle_api()->getLocalProvider()feature_toggle_api()->getQueryStringProvider()feature_toggle_api()->getRedisProvider()feature_toggle_api()->getSessionProvider()
E.g.
If you would like to set the providers in code you may call the following in the boot method of your
AppServiceProvider:
Add Additional Toggle Providers
You may add additional custom toggle providers or override the default toggle providers by adding them to the drivers
key within config/feature-toggle.php:
Then just add them in the order you'd like them to be checked within providers as you would the defaults:
Local Feature Toggles
To add new toggle(s) you will need to update the published config/feature-toggles.php file:
Toggle Parsing
The value passed from the .env file or set directly within config file can be:
- A
boolean:true/false - An
intversion ofboolean:1/0 - Finally all supported values of filter_var($value, FILTER_VALIDATE_BOOLEAN)
Conditional Feature Toggles
To add new conditional toggle(s) you will need to call feature_toggle_api()->setConditional method:
NOTE: The function passed to setConditional does not get called right away by default, it is deferred to allow
the Laravel app to bootstrap User/Session information. The conditional function is only called once and the value
is cached to help prevent expensive operations from being recalculated when adding additional conditional toggles.
Because of this design it is best to define these in AppServiceProvider@boot or in a
FeatureToggleServiceProvider@boot that you create.
Eloquent Feature Toggles
To use the eloquent driver you will need to update the feature-toggle config or setProviders method call,
place the following within the providers key:
OR
NOTE: Be sure to place this value in the order you would like it to be prioritized by the feature toggle api.
Database Migration
By default the migration for feature_toggles is not loaded, to load this you can update the options key
within feature-toggle config setting the useMigrations value to true:
If you would like to set the useMigrations in code you may call the following in the register method of your
AppServiceProvider:
You may also publish the migrations to your application by running the following:
Once you've used one of the methods above, you can run the following command to update your database with
the feature_toggles migration(s):
Eloquent Model
If you would like to use a different eloquent model you may do so by adding model to the config file:
QueryString Toggle Provider
To use the querystring driver you will need to update the feature-toggle config or setProviders method call,
place the following within the providers key:
When making a request to your application you may now use the following query strings to make feature toggles active/inactive:
featurefeature_off
e.g. http://localhost/?feature=Example&feature_off[]=Example%20Off&feature_off[]=Example%20Query%20String
The following example will result in Example as active and Example Off/Example Query String as inactive. NOTE:
This will only be true if the querystring provider is placed above other toggle providers that haven't already defined
these feature toggles.
Configure Query String Keys
If you'd like to configure what the active/inactive feature toggle input keys are you may add activeKey
and inactiveKey to config file.
Below is an example of configuring the query string keys as active and inactive:
Add Api Key Authorization
To keep users or bad actors from enabling/disabling feature toggles via the querystring toggle provider you
may configure the driver with a token/api key. By default the query string input is configured as
feature_token, but this can be also be configured to any value.
Redis Toggle Provider
To use the redis driver you will need to update the feature-toggle config or setProviders method call,
place the following within the providers key:
There are three options that can be configured:
key, defaults tofeature_togglesprefix, defaults tonullconnection, defaults todefault
Current implementation requires the array of toggles to be serialized in redis, you can use
Illuminate\Cache\RedisStore forever method to persist toggle values.
Session Toggle Provider
To use the session driver you will need to update the feature-toggle config or setProviders method call,
place the following within the providers key:
Frontend Feature Toggle Api
Place the following in your main layout blade template in the <head> tag.
Then create a new js file within resources/js called featureToggle.js:
Expose on the window within app.js:
and/or simply use featureToggle within your other js files:
and/or create a Feature component that uses featureToggle.js:
Road Map
v1.x
- [x] Local Feature Toggles via Config.
- [x] Conditionally Enable/Disable Feature Toggles e.g. Authorization.
- [x] Eloquent Feature Toggles.
- [x] Query String Feature Toggles.
- [x] Integrate toggles into:
- [x] Blade
- [x] Middleware
- [x] Validation
v2.x
- [ ] Create/update toggles via common contract interface.
- [ ] Create Command to create/update toggles to be active/inactive.
- [ ] Classmap Feature Toggles (FeatureToggleServiceProvider similar to AuthServiceProvider $policies).
All versions of feature-toggle with dependencies
ext-json Version *
illuminate/support Version ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0