jadb/feature_toggle

Feature Flip, Feature Flag, Feature Switch

v0.1.0 2016-06-17 03:07 UTC

This package is auto-updated.

Last update: 2024-10-28 01:31:47 UTC


README

Build Status Total Downloads License

Feature Toggle

a.k.a. Feature Flip, Feature Flag, Feature Switch

From Wikipedia:

Feature Toggle is a technique in software development that attempts to provide an alternative to maintaining multiple source code branches, called feature branches.

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”.

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).

NOTE: For PHP5.x support, please check the 0.1.0 branch.

In composer.json:

{
    "require": {
        "jadb/feature_toggle": "^1.0"
    }
}

To install, you may then run:

$ php composer.phar install

Example

In your application's bootstrap:

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:

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.

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.

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.

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].

Bugs & Feedback

http://github.com/jadb/feature_toggle/issues

License

Copyright (c) 2014, Jad Bitar

Licensed under the MIT license.

Redistributions of files must retain the above copyright notice.