Skip to content

Commit e279219

Browse files
authored
chore: upgrade to PHP SDK v3 (#3)
1 parent 70d30cd commit e279219

4 files changed

Lines changed: 86 additions & 31 deletions

File tree

‎README.md‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,44 @@
11
# featurevisor-example-php
22

3-
Example PHP application using Featurevisor PHP SDK: https://github.com/featurevisor/featurevisor-php
3+
A small PHP application showing how to use the [Featurevisor PHP SDK](https://github.com/featurevisor/featurevisor-php).
44

5-
Learn more about Featurevisor: https://featurevisor.com
5+
Learn more about Featurevisor [here](https://featurevisor.com).
66

7-
## Installation
7+
It fetches a production datafile, creates a Featurevisor instance with customer context, then evaluates a flag, a variation, feature variables, and global variables.
88

9-
Clone the repository, and run this with Composer:
9+
## Installation
1010

11+
```sh
12+
composer install
1113
```
12-
$ composer install
14+
15+
## Run the example
16+
17+
```sh
18+
php src/index.php
1319
```
1420

15-
## Usage
21+
The application evaluates `commerce_platform`, `checkout_experience`, `max_items`, `payment_methods`, `serviceEndpoints`, and `supportContact` from this datafile:
1622

23+
```text
24+
https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-sdk-v3.json
1725
```
18-
$ php src/index.php
26+
27+
Expected output:
28+
29+
```text
30+
Commerce platform enabled: true
31+
Checkout variation: express
32+
Maximum checkout items: 25
33+
Payment methods: [card, wallet]
34+
Service endpoint: https://api.eu.example.com (timeout: 1200 ms, retries: 4)
35+
Support contact: support-nl@example.com
1936
```
2037

38+
Change the context in `src/index.php` to see how Featurevisor selects different rules, variations, and global variable overrides.
39+
2140
## Featurevisor project
2241

23-
Uses this Featurevisor project to fetch the configuration from: https://github.com/featurevisor/featurevisor-example-cloudflare
42+
The datafile is generated by the [Featurevisor Cloudflare example](https://github.com/featurevisor/featurevisor-example-cloudflare).
43+
44+
Learn more in the [Featurevisor PHP SDK documentation](https://featurevisor.com/docs/sdks/php/).

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"type": "project",
55
"license": "MIT",
66
"require": {
7-
"featurevisor/featurevisor-php": "^2.0"
7+
"featurevisor/featurevisor-php": "^3.0"
88
}
99
}

‎composer.lock‎

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/index.php‎

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,62 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
require_once __DIR__ . '/../vendor/autoload.php';
46

57
use Featurevisor\Featurevisor;
68

7-
// fetch datafile
8-
$DATAFILE_URL = "https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-tag-all.json";
9-
$datafileContent = json_decode(file_get_contents($DATAFILE_URL), true);
9+
const DATAFILE_URL = 'https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-sdk-v3.json';
1010

11-
// create instance
12-
$f = Featurevisor::createFeaturevisor([
13-
"datafile" => $datafileContent,
11+
$requestContext = stream_context_create([
12+
'http' => [
13+
'header' => "Accept: application/json\r\n",
14+
'timeout' => 10,
15+
'ignore_errors' => true,
16+
],
1417
]);
18+
$datafileJson = file_get_contents(DATAFILE_URL, false, $requestContext);
19+
20+
if ($datafileJson === false) {
21+
throw new RuntimeException('Could not fetch the Featurevisor datafile.');
22+
}
1523

16-
// set context
17-
$f->setContext([
18-
"userId" => "12345",
19-
"deviceId" => "device-23456",
20-
"country" => "nl",
24+
$statusLine = $http_response_header[0] ?? '';
25+
if (!preg_match('/\s2\d\d\s/', $statusLine)) {
26+
throw new RuntimeException("Datafile request failed: {$statusLine}");
27+
}
28+
29+
$datafile = json_decode($datafileJson, true, 512, JSON_THROW_ON_ERROR);
30+
$f = Featurevisor::createFeaturevisor([
31+
'datafile' => $datafile,
32+
'logLevel' => 'error',
33+
'context' => [
34+
'userId' => 'customer-123',
35+
'country' => 'nl',
36+
'locale' => 'nl-NL',
37+
'accountPlan' => 'pro',
38+
],
2139
]);
2240

23-
// evaluate values
24-
if ($f->isEnabled("my_feature")) {
25-
echo "Feature is enabled!";
26-
} else {
27-
echo "Feature is disabled.";
41+
try {
42+
$commerceEnabled = $f->isEnabled('commerce_platform');
43+
$checkoutVariation = $f->getVariation('checkout_experience');
44+
$maxItems = $f->getVariableInteger('checkout_experience', 'max_items');
45+
$paymentMethods = $f->getVariableArray('checkout_experience', 'payment_methods');
46+
$endpoints = $f->getVariableObject('serviceEndpoints');
47+
$supportContact = $f->getVariableString('supportContact');
48+
49+
echo 'Commerce platform enabled: ' . ($commerceEnabled ? 'true' : 'false') . PHP_EOL;
50+
echo 'Checkout variation: ' . ($checkoutVariation ?? 'unavailable') . PHP_EOL;
51+
echo 'Maximum checkout items: ' . ($maxItems ?? 'unavailable') . PHP_EOL;
52+
echo 'Payment methods: [' . implode(', ', $paymentMethods ?? []) . ']' . PHP_EOL;
53+
printf(
54+
"Service endpoint: %s (timeout: %d ms, retries: %d)\n",
55+
$endpoints['baseUrl'],
56+
$endpoints['timeoutMs'],
57+
$endpoints['retries'],
58+
);
59+
echo 'Support contact: ' . ($supportContact ?? 'unavailable') . PHP_EOL;
60+
} finally {
61+
$f->close();
2862
}

0 commit comments

Comments
 (0)