Skip to content

Commit

Permalink
PHP Openfeature provider (#554)
Browse files Browse the repository at this point in the history
* PHP Openfeature provider

* Update PHP usage docs

* Fix bucketing api hostname for socket path
  • Loading branch information
JamieSinn authored Feb 7, 2024
1 parent 9723a2f commit c6ef19e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 32 deletions.
20 changes: 11 additions & 9 deletions docs/sdk/sdk-proxy/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,24 @@ Sample configurations for each SDK verified to work with the proxy are below.

### HTTP Socket Configuration
```php
use DevCycle\DevCycleConfiguration;
use DevCycle\Model\DevCycleOptions;

$config = DevCycleConfiguration::getDefaultConfiguration()
->setApiKey("Authorization", getenv("DEVCYCLE_SERVER_SDK_KEY"))
->setHost("http://localhost:8080");
$options = new DevCycleOptions(
enableEdgeDB: false,
bucketingApiHostname = "hostname for sdk proxy here"
);
```

### Unix Socket Configuration

```php
use DevCycle\DevCycleConfiguration;
use DevCycle\Model\DevCycleOptions;

$config = DevCycleConfiguration::getDefaultConfiguration()
->setApiKey("Authorization", getenv("DEVCYCLE_SERVER_SDK_KEY"))
->setHost("http:/v1")
->setUDSPath("/path/to/socket/file.sock");
$options = new DevCycleOptions(
enableEdgeDB: false,
bucketingApiHostname: "http:/localhost",
unixSocketPath: "/path/to/unix/socket"
);
```


Expand Down
17 changes: 4 additions & 13 deletions docs/sdk/server-side-sdks/php/php-gettingstarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,14 @@ Please follow the [installation procedure](/sdk/server-side-sdks/php/php-install
```php
require_once(__DIR__ . '/vendor/autoload.php');

use DevCycle\DevCycleConfiguration;
use DevCycle\Api\DevCycleClient;
use DevCycle\Model\DevCycleOptions;
use DevCycle\Model\DevCycleUser;

// Configure API key authorization: bearerAuth
$config = DevCycleConfiguration::getDefaultConfiguration()->setApiKey(
"Authorization",
getenv("DEVCYCLE_SERVER_SDK_KEY")
);

$devcycleClient = new DevCycleClient(
$config,
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new GuzzleHttp\Client(),
);
$options = new DevCycleOptions();
$devCycleClient = new DevCycleClient(
sdkKey: getenv("DEVCYCLE_SERVER_SDK_KEY"),
dvcOptions: $options);
$user_data = new DevCycleUser(array(
"user_id"=>"my-user"
));
Expand Down
86 changes: 86 additions & 0 deletions docs/sdk/server-side-sdks/php/php-openfeature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
title: PHP OpenFeature Provider
sidebar_label: OpenFeature
sidebar_position: 4
description: How to implement the OpenFeature Provider
sidebar_custom_props: { icon: material-symbols:toggle-off }
---

# OpenFeature Provider

OpenFeature is an open standard that provides a vendor-agnostic, community-driven API for feature flagging that works
with DevCycle.

DevCycle provides a PHP implementation of the [OpenFeature](https://openfeature.dev/) Provider interface, if you prefer
to 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 SDK
proxy modes.

### Getting Started

Initialize the DevCycle SDK and set the DevCycleProvider as the provider for OpenFeature:

```csharp
$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();
```

### Required TargetingKey

For DevCycle SDK to work we require either a `targetingKey` or `user_id` to be set on the OpenFeature context.
This is used to identify the user as the `user_id` for a `DevCycleUser` in DevCycle. Setting the `user_id` property
will take priority over `targetingKey`.

### Context properties to DevCycleUser

The provider will automatically translate known `DevCycleUser` properties from the OpenFeature context to
the `DevCycleUser` object.

For example all these properties will be set on the `DevCycleUser`:

```php

$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 known `DevCycleUser` properties will be automatically
added to the `CustomData` property of the `DevCycleUser`.

### Context Limitations

DevCycle only supports flat JSON Object properties used in the Context. Non-flat properties will throw an exception.

For example `nested` will be ignored:

```php
$attributes = new Attributes(array("key"=>"value", "number"=>1, "bool"=>true, "nested"=>array("key"=>"value")));
self::$context = new EvaluationContext('user', $attributes);
```
16 changes: 6 additions & 10 deletions docs/sdk/server-side-sdks/php/php-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,15 @@ To get started, contact us at [email protected] to enable EdgeDB for your pro
Once you have EdgeDB enabled in your project, pass in the enableEdgeDB option to turn on EdgeDB mode for the SDK:

```php
use DevCycle\DevCycleConfiguration;
use DevCycle\DevCycleOptions;

use DevCycle\Api\DevCycleClient;
use DevCycle\Model\DevCycleOptions;
use DevCycle\Model\DevCycleUser;

$config = DevCycleConfiguration::getDefaultConfiguration()->setApiKey(
"Authorization",
getenv("DEVCYCLE_SERVER_SDK_KEY")
);
$options = new DevCycleOptions(true);
$devcycleClient = new DevCycleClient(
$config,
dvcOptions:$options
);
$devCycleClient = new DevCycleClient(
sdkKey: getenv("DEVCYCLE_SERVER_SDK_KEY"),
dvcOptions: $options);
```

## Async Methods
Expand Down

0 comments on commit c6ef19e

Please sign in to comment.