systream/feature-switch

This package provides feature switch functionality. A/B test support and time based switcher.

1.2.6 2016-11-24 21:12 UTC

This package is not auto-updated.

Last update: 2024-04-23 19:44:34 UTC


README

Installation

You can install this package via packagist.org with composer.

composer require systream/feature-switch

composer.json:

"require": {
    "systream/feature-switch": "1.*"
}

This library requires php 5.6 or higher.

Usage examples

By default the feature is not enabled.

$feature = new Feature('foo_bar_feature_key');
$feature->isEnabled(); // will return: false

If you want to change the feature state, you have to set up one or more switcher.

Switchers (/ toggles / flippers)

Simple

You can easily switch on a feature:

$feature = new Feature('foo_bar_feature_key');
$feature->addSwitcher(Simple::on());
$feature->isEnabled(); // will return: true

A/B testing

This will enable the feature approximately 50% of the visitors. The feature status is tracking by cookie, so if the visitor returns, the same state of the feature will be shown.

$feature = new Feature('foo_bar_feature_key');
$feature->addSwitcher(new AB());
$feature->isEnabled();

Time based switching

With this library you can set up a time based feature toggle too. For example you cool new feature will be available for every visitor after a point in time.

$feature = new Feature('foo_bar_feature_key');
$feature->addSwitcher(new Until(\DateTime::createFromFormat('Y-m-d H:i:s', '2017-08-12 10:00:00')));
$feature->isEnabled(); // brefore 2017-08-12 10:00:00 it's return false, after will return true

If you want to disable a feature after a date:

$feature = new Feature('foo_bar_feature_key');
$feature->addSwitcher(new Until(\DateTime::createFromFormat('Y-m-d H:i:s', '2017-08-12 10:00:00'), false));
$feature->isEnabled(); // brefore 2017-08-12 10:00:00 it's return true, after will return false

Callback

For custom cases:

$feature = new Feature('foo_bar_feature_key');
$feature->addSwitcher(
	new Callback(function() {
		/* do custom logic */
		return true;
	})
);
$feature->isEnabled();

Writing custom switcher

The only thing you should do that your class need to implement FeatureSwitcherInterface interface.

Add multiple switcher/toggle

$feature = new Feature('foo_bar_feature_key');
$feature->addSwitcher(new AB());
$feature->addSwitcher(new Until(\DateTime::createFromFormat('Y-m-d H:i:s', '2017-08-12 10:00:00')));
$feature->isEnabled();

The feature will passed to all of the switcher until one of them return true; In this case the feature will tested first with AB switcher and if it returns false then it passes to the next time based switcher.

FeatureSwitch

Simple feature builder

$feature1 = FeatureSwitch::buildFeature('foo_feature', true); // enabled
$feature2 = FeatureSwitch::buildFeature('another_bar_feature', false); // disabled

Container

$featureSwitch = new FeatureSwitch();
$featureSwitch->addFeature(FeatureSwitch::buildFeature('foo', true));
 
$feature = new Feature('bar2');
$feature->addSwitcher(new AB());

$featureSwitch->addFeature($feature);
$featureSwitch->isEnabled('foo'); // true
$featureSwitch->isEnabled('bar2');

FeatureSwitchArray

This class decorates the FeatureSwitch with array access support.

You can user FeatureSwitchArray class as Array:

$featureSwitch = new FeatureSwitchArray();

$featureSwitch[] = FeatureSwitch::buildFeature('foo', true);
$featureSwitch->isEnabled('foo'); // returns true
$featureSwitch['foo']->isEnabled();

Test

Build Status