, (*1)
Feature Toggle
a.k.a. Feature Flip, Feature Flag, Feature Switch, (*2)
From Wikipedia:, (*3)
Feature Toggle is a technique in software development that attempts to provide an
alternative to maintaining multiple source code branches, called feature branches., (*4)
Continuous release and continuous deployment enables you to have quick feedback
about your coding. This requires you to integrate your changes as early as possible.
Feature branches introduce a by-pass to this process. Feature toggles brings you back
to the track, but the execution paths of your feature is still âdeadâ and âuntestedâ,
if a toggle is âoffâ. But the effort is low to enable the new execution paths just by
setting a toggle to âonâ., (*5)
Common use cases:
- Limited testing (i.e. employees only based on email address, subset of users, etc.)
- Gradual feature release (i.e. by location, by subscription, by browser, etc.)
Install
FeatureToggle can be installed using Composer (of course, you could always
clone it from GitHub)., (*6)
NOTE: For PHP5.x support, please check the 0.1.0 branch., (*7)
In composer.json
:, (*8)
{
"require": {
"jadb/feature_toggle": "^1.0"
}
}
To install, you may then run:, (*9)
$ php composer.phar install
Example
In your application's bootstrap:, (*10)
use FeatureToggle\FeatureRegistry;
use Predis\Client as Redis;
FeatureRegistry::setStorage(new Redis());
FeatureRegistry::init('Cool Feature', [
'description' => 'A cool new feature!',
'strategies' => [
'UserAgent' => [['/opera/', '/Mozilla\/5\.0/']],
function ($Feature) {
return !empty($_SESSION['isAdmin']);
},
function ($Feature) {
return !empty($_SESSION[$Feature->getName()]);
}
]
]);
FeatureRegistry::init('Another Cool Feature', [
'type' => 'threshold', // use the `ThresholdFeature`
'description' => 'Another cool new feature!',
'strategies' => [
'UserAgent' => [['/opera/', '/Mozilla\/5\.0/']],
function ($Feature) {
return !empty($_SESSION['isAdmin']);
},
function ($Feature) {
return !empty($_SESSION[$Feature->getName()]);
}
]
])->threshold(2); // Require at least 2 strategies to pass
and then, anywhere in your code, you can check this feature's status like so:, (*11)
if (\FeatureToggle\FeatureManager::isEnabled('Cool Feature')) {
// do something
}
What's included?
Features
-
BooleanFeature: Enabled if one ore more strategies pass.
-
StrictBooleanFeature: Enabled only if entire strategies' set passes.
-
ThresholdFeature: Enabled only if a minimum number of strategies pass.
-
EnabledFeature: Forces feature to always be enabled.
-
DisabledFeature: Forces feature to always be disabled.
Features MUST implement the FeatureInterface
., (*12)
Strategies
-
DateTimeStrategy: Compares today's time to set date and time.
-
DateTimeRangeStrategy: Checks if today's time is in set date time range.
-
UserAgentStrategy: Checks if browser's user agent matches any allowed agent.
Strategies MUST implement the StrategyInterface
., (*13)
Storage Adapters
-
HashStorage: Default. Basic associative array (a.k.a. in memory)
-
FileStorage: Filesystem used (only good if features stored in database).
-
MemcachedStorage: Memcached store, requires the
Memcached
extension.
-
RedisStorage: Redis store, requires the
predis/predis
package.
Storage adapaters MUST implement the StorageInterface
., (*14)
Todo
-
PercentageStrategy
enable feature to a percentage of users - requires RedisStorage
- Option to automatically disable a feature if error threshold reached - requires
RedisStorage
Contributing
- Fork
- Mod, fix, test
-
Optionally write some documentation (currently in
README.md
)
- Send pull request
All contributed code must be licensed under the [BSD 3-Clause License][bsd3clause]., (*15)
Bugs & Feedback
http://github.com/jadb/feature_toggle/issues, (*16)
License
Copyright (c) 2014, Jad Bitar, (*17)
Licensed under the MIT license., (*18)
Redistributions of files must retain the above copyright notice., (*19)