OpenFeature Provider
OpenFeature is an open standard that provides a vendor-agnostic, community-driven API for feature flagging that workswith DevCycle.
DevCycle provides a PHP implementation of theOpenFeature Provider interface, if you preferto use the OpenFeature API.
Usage
Installation
The OpenFeature Provider is included in the DevCycle SDK for PHP natively. It's compatible with both Cloud, and SDKproxy modes.
To install the bindings viaComposer, add the following tocomposer.json
:
{
"require":{
"devcycle/php-server-sdk":"*"
}
}
Then runcomposer install
Once the composer install is complete, avendor
folder should be generated at the root of your project. Include this at the start of your app index file:
require_once(__DIR__ . '/vendor/autoload.php');
Getting Started
Initialize the DevCycle SDK and set the DevCycleProvider as the provider for OpenFeature:
$options = new DevCycleOptions(true);
$devCycleClient = new DevCycleClient(
sdkKey: getenv("DEVCYCLE_SERVER_SDK_KEY"),
dvcOptions: $options);
$api = OpenFeatureAPI::getInstance();
$api->setProvider($devCycleClient->getOpenFeatureProvider());
$openFeatureClient = $api->getClient();
Evaluate a Variable
Use a Variable value by setting the EvaluationContext, then passing the Variable key and default value to one of the OpenFeature flag evaluation methods.
// Create a new user attribute object that can be used by OpenFeature as part of the flag evaluation process.
$user_attributes = new Attributes(array("user_id" => "my-user"));
// Create a new evaluation context for the feature flag evaluations. This context is used to provide user or environment details for flag evaluations in OpenFeature.
$openfeature_context = new EvaluationContext(attributes: $user_attributes);
// Use the OpenFeature client to get the string value of the "string-flag" feature flag.
$flag_value = $openfeature_client->getStringValue("string-flag", "default", $openfeature_context);
Required TargetingKey
For DevCycle SDK to work we require either atargetingKey
oruser_id
to be set on the OpenFeature context.This is used to identify the user as theuser_id
for aDevCycleUser
in DevCycle. Setting theuser_id
propertywill take priority overtargetingKey
.
Context properties to DevCycleUser
The provider will automatically translate knownDevCycleUser
properties from the OpenFeature context totheDevCycleUser
object.
For example all these properties will be set on theDevCycleUser
:
$attributes = new Attributes(
array(
"user_id" => "test",
"customData" => array("customkey" => "customValue"),
"privateCustomData" => array("privateCustomKey" => "privateCustomValue"),
"email" => "[email protected]",
"name" => "Name Name",
"language" => "EN",
"country" => "CA",
"appVersion" => "0.0.1",
"appBuild" => 1,
"nonSetValueBubbledCustomData" => true,
"nonSetValueBubbledCustomData2" => "true",
"nonSetValueBubbledCustomData3" => 1,
"nonSetValueBubbledCustomData4" => null)
);
$context = new EvaluationContext('user', $attributes);
Context properties that are not knownDevCycleUser
properties will be automaticallyadded to theCustomData
property of theDevCycleUser
.
Context Limitations
DevCycle only supports flat JSON Object properties used in the Context. Non-flat properties will throw an exception.
For examplenested
will be ignored:
$attributes = new Attributes(array("key"=>"value", "number"=>1, "bool"=>true, "nested"=>array("key"=>"value")));
self::$context = new EvaluationContext('user', $attributes);