Feature Toggle
Installation
composer require sonsofphp/feature-toggleUsage
<?php
use SonsOfPHP\Component\FeatureToggle\Feature;
use SonsOfPHP\Component\FeatureToggle\Provider\InMemoryFeatureToggleProvider;
use SonsOfPHP\Component\FeatureToggle\Toggle\AlwaysEnabledToggle;
// Using a feature toggle provider
$provider = new InMemoryFeatureToggleProvider();
$provider->add(new Feature('feature.example', new AlwaysEnabledToggle()));
$feature = $provider->get('feature.example');
// Checking if the feature is enabled
$isEnabled = $feature->isEnabled();Advanced Usage
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.
Affirmative Toggle
Similar to the chain toggle, this will only return true when ALL toggles are true.
Date Range Toggle
The date range toggle will return true if it's within a given time range.
Create your own Toggle
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.
Once you make your custom toggle, you can use it just like all the rest of the toggles.
Last updated
Was this helpful?