featurit / featurit-sdk-php
PHP SDK for FeaturIT
Requires
- php: ^8.0.2
- ext-json: *
- laminas/laminas-serializer: ^2.14
- lastguest/murmurhash: ^2.1
- php-http/client-common: ^2.3
- php-http/discovery: ^1.12
- psr/http-client: ^1.0
- psr/http-client-implementation: ^1.0
- psr/http-factory: ^1.0
- psr/http-factory-implementation: ^1.0
- psr/simple-cache: ^1.0 || ^2.0 || ^3.0
- psr/simple-cache-implementation: ^1.0 || ^2.0 || ^3.0
- symfony/cache: ^5.0 | ^6.0 | ^7.0
Requires (Dev)
- fakerphp/faker: ^1.21
- laminas/laminas-diactoros: ^2.4
- php-http/curl-client: ^2.1
- php-http/mock-client: ^1.5
- phpunit/phpunit: ^9
- symfony/http-client: ^5.0 | ^6.0 | ^7.0
- symfony/var-dumper: ^5.1
README
PHP client for the FeaturIT Feature Flag management platform.
Description
This package aims to simplify the integration of the FeaturIT API in a PHP project.
Getting started
Dependencies
- PHP >= 8.0
- psr/http-client-implementation
- psr/simple-cache-implementation
Installing
composer require featurit/featurit-sdk-php
If there's no package providing psr/http-client-implementation, visit https://packagist.org/providers/psr/http-client-implementation and choose the package that better suits your project.
If there's no package providing psr/simple-cache-implementation, visit https://packagist.org/providers/psr/simple-cache-implementation and choose the package that better suits your project.
Basic Usage
$featurit = new \Featurit\Client\Featurit(
'my-tenant-subdomain',
'my-environment-key'
);
$isFeatureFlagActive = $featurit->isActive('MY_FEATURE_NAME');
if ($isFeatureFlagActive) {
my_feature_code();
}
Segmentation Usage
This is useful when you want to show different versions of your features to your users depending on certain attributes.
$featurit = new \Featurit\Client\Featurit(
'my-tenant-subdomain',
'my-environment-key'
);
$userContext = new \Featurit\Client\Modules\Segmentation\DefaultFeaturitUserContext(
"1234",
"123af503",
"192.168.1.1",
[
"role" => "ADMIN",
"email" => "featurit.tech@gmail.com",
]
);
$featurit->setUserContext($userContext);
$isFeatureFlagActive = $featurit->isActive('MY_FEATURE_NAME');
if ($isFeatureFlagActive) {
$featureVersion = $featurit->version('MY_FEATURE_NAME');
if ($featureVersion == 'v1') {
my_feature_v1_code();
} else if ($featureVersion == 'v2') {
my_feature_v2_code();
}
}
Creating a UserContextProvider
In some cases you want to fill the UserContext data once and forget about it when checking for feature flags.
If that is your case you can implement your own UserContextProvider and pass it to the Featurit client constructor (we recommend using our builder in order to create a new Featurit client).
class MyFeaturitUserContextProvider implements FeaturitUserContextProvider
public function getContext(): FeaturitUserContext
{
$contextData = get_my_context_data();
return new DefaultFeaturitUserContext(
$contextData['userId'],
$contextData['sessionId'],
$contextData['ipAddress'],
[
"role" => $contextData['role'],
...
]
);
}
}
Authors
FeaturIT