open-feature/go-feature-flag-provider

The GO Feature Flag provider package for open-feature

This package is auto-updated.

Last update: 2024-11-04 03:57:23 UTC


README

go-feature-flag logo

GO Feature Flag - OpenFeature PHP provider

Packagist Version Documentation Issues Join us on slack

This repository contains the official PHP OpenFeature provider for accessing your feature flags with GO Feature Flag.

In conjunction with the OpenFeature SDK you will be able to evaluate your feature flags in your Ruby applications.

For documentation related to flags management in GO Feature Flag, refer to the GO Feature Flag documentation website.

Functionalities:

  • Manage the integration of the OpenFeature PHP SDK and GO Feature Flag relay-proxy.

Dependency Setup

Composer

composer require open-feature/go-feature-flag-provider

Getting started

Initialize the provider

The GoFeatureFlagProvider takes a config object as parameter to be initialized.

The constructor of the config object has the following options:

The only required option to create a GoFeatureFlagProvider is the URL (endpoint) to your GO Feature Flag relay-proxy instance.

use OpenFeature\Providers\GoFeatureFlag\config\Config;
use OpenFeature\Providers\GoFeatureFlag\GoFeatureFlagProvider;
use OpenFeature\implementation\flags\MutableEvaluationContext;
use OpenFeature\implementation\flags\Attributes;
use OpenFeature\OpenFeatureAPI;

$config = new Config('http://gofeatureflag.org', 'my-api-key');
$provider = new GoFeatureFlagProvider($config);

$api = OpenFeatureAPI::getInstance();
$api->setProvider($provider);
$client = $api->getClient();
$evaluationContext = new MutableEvaluationContext(
      "214b796a-807b-4697-b3a3-42de0ec10a37", 
      new Attributes(["email" => 'contact@gofeatureflag.org'])
  );

$value = $client->getBooleanDetails('integer_key', false, $evaluationContext);
if ($value) {
    echo "The flag is enabled";
} else {
    echo "The flag is disabled";
}

The evaluation context is the way for the client to specify contextual data that GO Feature Flag uses to evaluate the feature flags, it allows to define rules on the flag.

The targeting_key is mandatory for GO Feature Flag to evaluate the feature flag, it could be the id of a user, a session ID or anything you find relevant to use as identifier during the evaluation.

Evaluate a feature flag

The client is used to retrieve values for the current EvaluationContext. For example, retrieving a boolean value for the flag "my-flag":

$value = $client->getBooleanDetails('integer_key', false, $evaluationContext);
if ($value) {
  echo "The flag is enabled";
} else {
  echo "The flag is disabled";
}

GO Feature Flag supports different all OpenFeature supported types of feature flags, it means that you can use all the accessor directly

// Bool
$client->getBooleanDetails('my-flag-key', false, new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));
$client->getBooleanValue('my-flag-key', false, new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));

// String
$client->getStringDetails('my-flag-key', "default", new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));
$client->getStringValue('my-flag-key', "default", new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));

// Integer
$client->getIntegerDetails('my-flag-key', 1, new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));
$client->getIntegerValue('my-flag-key', 1, new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));

// Float
$client->getFloatDetails('my-flag-key', 1.1, new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));
$client->getFloatValue('my-flag-key', 1.1, new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));

// Object
$client->getObjectDetails('my-flag-key', ["default" => true], new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));
$client->getObjectValue('my-flag-key', ["default" => true], new MutableEvaluationContext("214b796a-807b-4697-b3a3-42de0ec10a37"));

Features status

Implemented: ✅ | In-progress: ⚠️ | Not implemented yet: ❌

Contributing

This project welcomes contributions from the community. If you're interested in contributing, see the contributors' guide for some helpful tips.

PHP Versioning

This library targets PHP version 8.0 and newer. As long as you have any compatible version of PHP on your system you should be able to utilize the OpenFeature SDK.

This package also has a .tool-versions file for use with PHP version managers like asdf.

Installation and Dependencies

Install dependencies with composer install, it will update the composer.lock with the most recent compatible versions.

We value having as few runtime dependencies as possible. The addition of any dependencies requires careful consideration and review.