jadb/feature_toggle

Installs: 2 457

Dependents: 0

Stars: 2

Watchers: 2

Forks: 1

Language: PHP

0.0.2 2014-09-04 13:40 UTC

README

Build Status Total Downloads License

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

In composer.json:

{
    "require": {
        "jadb/feature_toggle": "*"
    }
}

To install, you may then run:

$ php composer.phar install

Example

In your application's bootstrap:

use FeatureToggle\FeatureRegistry;

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.

Todo

  • Enable feature only when {n} (or all) strategies pass
  • RedisStorage to keep track of features
  • 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.

Bugs & Feedback

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

License

Copyright (c) 2014, Jad Bitar

Licensed under the BSD 3-Clause License.

Redistributions of files must retain the above copyright notice.