<?phpuseSonsOfPHP\Component\FeatureToggle\Feature;useSonsOfPHP\Component\FeatureToggle\Provider\InMemoryFeatureToggleProvider;useSonsOfPHP\Component\FeatureToggle\Toggle\AlwaysEnabledToggle;// Using a feature toggle provider$provider =newInMemoryFeatureToggleProvider();$provider->addFeature(newFeature('feature.example',newAlwaysEnabledToggle()));$feature = $provider->getFeatureToggleByKey('feature.example');// Checking if the feature is enabled$isEnabled = $feature->isEnabled();
Advanced Usage
<?php// ...useSonsOfPHP\Component\FeatureToggle\Context;// Different ways to build the context, the context// is used by the Toggle$context =newContext(['user'=> $user,]);$context->set('user', $user);$context['user'] = $user;$isEnabled = $feature->isEnabled($context);
When you create your own toggles, you may need to introduce additional context to the toggle to check if everything should be enabled or disabled. This is where this comes into play at.
Chain Toggle
The chain toggle allows you to use many toggles together. If ANY toggle returns true, the feature is considered enabled.
<?phpuseSonsOfPHP\Component\FeatureToggle\Toggle\AlwaysDisabledToggle;useSonsOfPHP\Component\FeatureToggle\Toggle\AlwaysEnabledToggle;useSonsOfPHP\Component\FeatureToggle\Toggle\ChainToggle;$toggle =newChainToggle([newAlwaysEnabledToggle(),newAlwaysDisabledToggle(),]);// true because at least ONE is enabled$isEnabled = $toggle->isEnabled();
Affirmative Toggle
Similar to the chain toggle, this will only return true when ALL toggles are true.
<?phpuseSonsOfPHP\Component\FeatureToggle\Toggle\AlwaysDisabledToggle;useSonsOfPHP\Component\FeatureToggle\Toggle\AlwaysEnabledToggle;useSonsOfPHP\Component\FeatureToggle\Toggle\ChainToggle;$toggle =newChainToggle([newAlwaysDisabledToggle(),newAlwaysEnabledToggle(),]);// false because at least ONE is disabled$isEnabled = $toggle->isEnabled();
Date Range Toggle
The date range toggle will return true if it's within a given time range.
Take a look at how some of the other toggles are implemented. Creating your own toggles are very easy. You just need to make sure they implement the interface ToggleInterface.