This client is a wrapper around the Flipt.io REST API to easily evaluate flags with a given context on a remote Flipt instance.
Unlike some of our other SDKs, this client currently does not support the ability to create or update Flipt data. This client is intended to be used in applications that only needs to evaluate flags.
This SDK status is beta
, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning your installation of this package wherever necessary.
- PHP 8.0 or higher
- Composer
API documentation is available at https://www.flipt.io/docs/reference/overview.
composer install flipt-io/flipt
Instantiate a client with the corresponding settings.
The API token can be generated under Settings -> API Tokens
within your Flipt instance. See the documentation for more information.
$flipt = new \Flipt\Client('https://my-flipt.io', '<apiToken>', '<default namespace>', [ 'default' => 'context' ]);
// test on a boolean flag
$resp = $flipt->boolean('my-boolean');
if ($resp->getEnabled()) {
// do somthing
}
// test on variant flag
$resp = $flipt->variant('my-variant');
if ($resp->getVariantKey() == 'demo') {
// do something
}
// get a variant attachment
$array = $resp->getVariantAttachment();
// the returned value is an array and you can access properties like:
if ($array['key'] == 'demo') {
// do something
}
You can setup the context in the constructor as shown in the example above.
You can also overwrite context values when accessing a flag as the following example shows:
$flipt = new \Flipt\Client('https://my-flipt.io', 'token', 'namespace', [ 'environment' => 'test', 'user' => '23' ]);
// will send the context [ 'environment' => 'test', 'user' => '23' ] as defined in the client
$test = $flipt->boolean('flag');
// will send the context [ 'environment' => 'test', 'user' => '50' ] as it will merge the client context with the current from the call
$test2 = $flipt->boolean('flag', [ 'user' => '50' ]);
See our documentation for more information about namespaces.
If you need to query another namespace you can switch the namespace as follows:
// this client will query against the 'test' namespace
$fliptTest = new \Flipt\Client('https://my-flipt.io', 'token', 'test');
// this will create a new client with all the settings from $fliptTest client except the namespace will changed to 'production'
$fliptProd = $fliptTest->withNamespace('production'),
// this will use namespace 'production'
$fliptProd->boolean('flag')
# install composer dependencies
composer install
# execute phpunit
composer run-script test
Tests can be run in a Docker container like this:
# install composer dependencies
docker run -v $PWD:/app -w /app composer install
# execute phpunit
docker run -v $PWD:/app -w /app --entrypoint vendor/bin/phpunit php:8-cli
Thanks to legoheld for the initial implementation of this client.