FeatureFlag
Simple feature flag checking for PHP >=5.5., (*1)
, (*2)
Installation
Install this package in your application using composer., (*3)
In the require section, add the following dependency:, (*4)
"fraserreed/feature-flag": "~0.2"
Usage
Any of these approaches can be extended and put in a view helper in most frameworks., (*5)
Static Configuration
For static, array based feature flags, ideally used in multi-environment deploys., (*6)
First create the static filter:, (*7)
$config = [
'feature-one' => true,
'feature-two' => false,
'feature-three' => true,
'feature-four' => true
];
$featureFlagFilter = new \FeatureFlag\Filter\Simple( $config );
Then assert that a feature is enabled or not:, (*8)
$featureFlag = new \FeatureFlag\FeatureFlag( $featureFlagFilter );
echo (int) $featureFlag->isEnabled( 'feature-one' ) . " should be true\n";
echo (int) $featureFlag->isEnabled( 'feature-two' ) . " should be false\n";
echo (int) $featureFlag->isEnabled( 'feature-three' ) . " should be true\n";
echo (int) $featureFlag->isEnabled( 'feature-four' ) . " should be true\n";
echo (int) $featureFlag->isEnabled( 'feature-five' ) . " should be false (doesn't exist)\n";
Distributed IP Address Weighting
For an expected distribution across the IPv4 spectrum, a weighted configuration can be used. A crc32 hash checksum is computed for the IP, modulo 100., (*9)
First create the filter with the expected distribution factor., (*10)
//feature flag will be enabled 75% of the time
$featureFlagFilter = new \FeatureFlag\Filter\DistributedIp( 75 );
Then assert that a feature is enabled or not:, (*11)
$featureFlag = new \FeatureFlag\FeatureFlag( $featureFlagFilter );
echo (int) $featureFlag->isEnabled( 'feature-one', '192.168.0.161' ) . " should be false\n";
echo (int) $featureFlag->isEnabled( 'feature-one', '31.12.127.255' ) . " should be true\n";
echo (int) $featureFlag->isEnabled( 'feature-one', '46.248.224.183' ) . " should be true\n";
echo (int) $featureFlag->isEnabled( 'feature-one', '58.136.218.102' ) . " should be true\n";
Tests
To run the tests you'll need to have phpunit installed., (*12)
Running the tests:, (*13)
$ phpunit
Contributing
For any issues, submit an issue via this repo. For contributing please fork and submit PRs., (*14)
License
MIT, see LICENSE., (*15)