fetzi / flipt-php
A PHP package for integrating the Flipt REST API
Fund package maintenance!
fetzi
Requires
- php: ^7.4 || ^8.0
- php-http/discovery: ^1.13
- php-http/httplug: ^2.2
- psr/http-client-implementation: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
- ergebnis/phpstan-rules: ^0.15.3
- friendsofphp/php-cs-fixer: ^3.0
- guzzlehttp/guzzle: ^7.3
- mockery/mockery: ^1.4
- pestphp/pest: ^1.2
- php-http/mock-client: ^1.4
- phpstan/phpstan: ^0.12.88
- phpstan/phpstan-strict-rules: ^0.12.9
- thecodingmachine/phpstan-strict-rules: ^0.12.1
README
flipt-php is a small wrapper package for the Flipt REST API to be able to easily integrate Flipt into your PHP applications.
Installation
composer require fetzi/flipt-php
Usage
The Flipt
class uses HTTPlug a HTTP Client abstraction to make the API requests. You have to pass a base URL to the static create
function. Everything else (HttpClient
, RequestFactory
& StreamFactory
) is automatically determined.
$flipt = Flipt::create('http://localhost:8080');
By calling the evaluate
method you can check if a certain user (entity) should get a certain feature or not. To perform a evaluation you need to create a EvaluateRequest
that contains the data to evaluate.
$evaluateRequest = new EvaluateRequest('sample-flag', 'user-id', ['foo' => 'bar']); $evaluateResponse = $flipt->evaluate($evaluateRequest); if ($evaluateResponse->isMatch()) { // awesome new feature } else { // old boring feature }
Variants
To be able to determine which variant of a feature-flag should be displayed you need to use the getVariant()
method on the response.
The EvaluateResponse
also provides a variant that can be accessed via the getValue()
or getVariant()
method:
$evaluateRequest = new EvaluateRequest('sample-flag', 'user-id', ['foo' => 'bar']); $evaluateResponse = $flipt->evaluate($evaluateRequest); if ($evaluateResponse->isMatch()) { switch ($evaluateResponse->getVariant()) { case 'a': // show A variant break; case 'b': // show B variant break; } }
flipt-php was created by Johannes Pichler under the MIT license.