Flagception is a simple and powerful feature toggle library. Only a few lines of configuration necessary - and still very flexible and expandable.
Open a command console, enter your project directory and execute the following command to download the latest stable version of this library:
$ composer require flagception/flagceptionJust create a FeatureManager instance and pass your activator to start with feature toggling.
// MyClass.phpclass MyClass
{
publicfunctiondoSomething()
{
// The activator decide if the feature is active or not// You can use your own activator if you implement the interface$activator = newArrayActivator();
$manager = newFeatureManager($activator);
if ($manager->isActive('your_feature_name')) {
// do something
}
}
}The activator is the most important class and decide if the given feature is active or not. The ArrayActivator needs
an array with active feature names as constructor argument. If the requested feature is in array, it will return true
otherwise false. Example:
// MyClass.phpclass MyClass
{
publicfunctiondoSomething()
{
$activator = newArrayActivator([
'feature_abc',
'feature_def',
'feature_ghi'
]);
$manager = newFeatureManager($activator);
// Will return trueif ($manager->isActive('feature_def')) {
// do something
}
// Will return falseif ($manager->isActive('feature_wxy')) {
// do something
}
}
}This library ships an ArrayActivator, a ConstraintActivator, a EnvironmentActivator, CookieActivator and a ChainActivator.
In most cases you will create your own activator (eg. for doctrine). Just implement the FeatureActivatorInterface.
You can use a CacheActivator if you want to cache the result from some time intensive activators.
Sometimes your activator needs more context for deciding if a feature is active or not. You can optionally add a context object as second argument to the manager and check the context data in your activator.
Example:
// MyClass.phpclass MyClass
{
publicfunctiondoSomething(User$user)
{
$activator = newYourCustomDoctrineActivator();
$manager = newFeatureManager($activator);
$context = newContext();
$context->add('user_id', $user->getId());
// Check the feature with contextif ($manager->isActive('feature_def', $context)) {
// do something
}
// Check the feature without context (result may differ from above)if ($manager->isActive('feature_def')) {
// do something
}
}
}
// YourCustomDoctrineActivator.phpclass YourCustomDoctrineActivator implements FeatureActivatorInterface
{
publicfunctionisActive($name, Context$context)
{
return$context->get('user_id') === 12;
}
}You can also add the context data globally instead of adding the context to each feature request. Just pass a class
which implement the ContextDecoratorInterface as second argument for the feature manager constructor:
// MyClass.phpclass MyClass
{
publicfunctiondoSomething(User$user)
{
$activator = newYourCustomDoctrineActivator();
$decorator = newArrayDecorator([
'user_id' => $user->getId()
]);
$manager = newFeatureManager($activator, $decorator);
// Check the feature with the global defined context if ($manager->isActive('feature_def')) {
// do something
}
}
}
//YourCustomDoctrineActivator.phpclass YourCustomDoctrineActivator implements FeatureActivatorInterface
{
publicfunctionisActive($name, Context$context)
{
return$context->get('user_id') === 12;
}
}You can also mix both variants:
// MyClass.phpclass MyClass
{
publicfunctiondoSomething(User$user)
{
$activator = newYourCustomDoctrineActivator();
$decorator = newArrayDecorator([
'user_id' => $user->getId()
]);
$manager = newFeatureManager($activator, $decorator);
$context = newContext();
$context->add('user_name', $user->getUsername());
// Check the feature with the global defined context if ($manager->isActive('feature_def', $context)) {
// do something
}
}
}This library ships an ArrayDecorator and a ChainDecorator.