Skip to content

Commit ca3dfde

Browse files
committed
docs: add code examples for context and hooks
Signed-off-by: Tom Carrio <[email protected]>
1 parent 1a338b7 commit ca3dfde

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,20 @@ Sometimes, the value of a flag must consider some dynamic criteria about the app
170170
In OpenFeature, we refer to this as [targeting](https://openfeature.dev/specification/glossary#targeting).
171171
If the flag management system you're using supports targeting, you can provide the input data using the [evaluation context](https://openfeature.dev/docs/reference/concepts/evaluation-context).
172172

173-
<!-- TODO: code examples using context and different levels -->
173+
```php
174+
// add a value to the global context
175+
$api = OpenFeatureAPI.getInstance();
176+
$api->setEvaluationContext(new EvaluationContext('targetingKey', ['myGlobalKey' => 'myGlobalValue']));
177+
178+
// add a value to the client context
179+
$client = $api->getClient();
180+
$client->setEvaluationContext(new EvaluationContext('targetingKey', ['myClientKey' => 'myClientValue']));
181+
182+
// add a value to the invocation context
183+
$context = new EvaluationContext('targetingKey', ['myInvocationKey' => 'myInvocationValue']);
184+
185+
$boolValue = $client->getBooleanValue('boolFlag', false, $context);
186+
```
174187

175188
### Hooks
176189

@@ -180,7 +193,18 @@ If the hook you're looking for hasn't been created yet, see the [develop a hook]
180193

181194
Once you've added a hook as a dependency, it can be registered at the global, client, or flag invocation level.
182195

183-
<!-- TODO: code example of setting hooks at all levels -->
196+
```php
197+
// add a hook globally, to run on all evaluations
198+
$api = OpenFeatureAPI.getInstance();
199+
$api->addHook(new ExampleGlobalHook());
200+
201+
// add a hook on this client, to run on all evaluations made by this client
202+
$client = $api->getClient();
203+
$client->addHook(new ExampleClientHook());
204+
205+
// add a hook for this evaluation only
206+
$value = $client->getBooleanValue("boolFlag", false, $context, new EvaluationOptions([new ExampleInvocationHook()]));
207+
```
184208

185209
### Logging
186210

0 commit comments

Comments
 (0)