Giter Club home page Giter Club logo

flipt-php's Introduction

Flipt PHP Library

Packagist Version beta

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.

Status

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.

Requirements

Documentation

API documentation is available at https://www.flipt.io/docs/reference/overview.

Install

Composer

composer install flipt-io/flipt

Usage

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
}

Context

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' ]);

Namespaces

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')

Testing

Local

# install composer dependencies
composer install

# execute phpunit
composer run-script test

Docker

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 ๐ŸŽ‰

Thanks to legoheld for the initial implementation of this client.

flipt-php's People

Contributors

markphelps avatar flipt-release-bot[bot] avatar legoheld avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.