Flip is a simple Feature Toggle implementation. Features are implemented as independent classes and are "mixed" into the class you want the feature to be exposed from.
Via Composer
$ composer require vehikl/flip
A Flip feature is just a regular PHP class with a few required methods.
enabled
- This method returns a boolean value indicating if the feature is enabled or not
toggles
- This method returns an array of available feature toggles. The array is keyed by the name of the method which
is called to run the feature. The value of each key is an associative array with keys on
and off
, each key
is mapped to the appropriate method to call depending on if the feature is "on" or "off".
class SomeFeature extends \Vehikl\Flip\Feature
{
/**
* Decides under which conditions this Feature is enabled
*/
public function enabled()
{
return random_int(0, 1) == 1;
}
/**
* Returns an array of available toggles for this feature
*/
public function toggles()
{
return [
'someToggle' => [
'on' => 'whenOn',
'off' => 'whenOff'
]
];
}
public function whenOn()
{
return "I'm on!";
}
public function whenOff()
{
return "I'm off!";
}
}
class SomeClass
{
use Vehikl\Flip\Featurable;
protected $features = [SomeFeature::class];
public function someBehaviour()
{
// no need for if/else blocks, just call the toggle using the
// `flip` helper
return $this->flip()->someToggle();
}
}
You can force a feature to be "on" or "off" by calling the alwaysOn
or alwaysOff
static methods respectively. This
will force all features of that class to be either "on" or "off" regardless of how their enabled
methods evaluate.
class SomeFeature extends \Vehikl\Flip\Feature
{
// include the $forcedState static variable if you want to enable forcing state
protected static $forcedState;
/**
* Decides under which conditions this Feature is enabled
*/
public function enabled()
{
return random_int(0, 1) == 1;
}
/**
* Returns an array of available toggles for this feature
*/
public function toggles()
{
return [
'someToggle' => [
'on' => 'whenOn',
'off' => 'whenOff'
]
];
}
public function whenOn()
{
return "I'm on!";
}
public function whenOff()
{
return "I'm off!";
}
}
class SomeClass
{
use Vehikl\Flip\Featurable;
protected $features = [SomeFeature::class];
public function someBehaviour()
{
// no need for if/else blocks, just call the toggle using the
// `flip` helper
return $this->flip()->someToggle();
}
}
// force the SomeFeature feature to be always on
SomeFeature::alwaysOn()
// anytime `someToggle` is called on instances of SomeClass,
// the `on` version of `someToggle` will be run
$someObject = new SomeClass;
$someObject->someBehaviour(); // always returns "I'm on!"
Please see CHANGELOG for more information on what has changed recently.
$ composer test
Please see CONTRIBUTING for details.
If you discover any security related issues, please email go@vehikl.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.