|
| 1 | +# Featurevisor PHP SDK <!-- omit in toc --> |
| 2 | + |
| 3 | +This is a port of Featurevisor [Javascript SDK](https://featurevisor.com/docs/sdks/javascript/) v2.x to PHP, providing a way to evaluate feature flags, variations, and variables in your PHP applications. |
| 4 | + |
| 5 | +For more information, visit: [https://featurevisor.com](https://featurevisor.com) |
| 6 | + |
| 7 | +- [Installation](#installation) |
| 8 | +- [SDK usage](#sdk-usage) |
| 9 | + - [Create an instance](#create-an-instance) |
| 10 | + - [Set context](#set-context) |
| 11 | + - [Evaluate values](#evaluate-values) |
| 12 | + - [Evaluate a flag](#evaluate-a-flag) |
| 13 | + - [Evaluate a variation](#evaluate-a-variation) |
| 14 | + - [Evaluate a variable](#evaluate-a-variable) |
| 15 | + - [Evaluate all values](#evaluate-all-values) |
| 16 | + - [Passing additional context](#passing-additional-context) |
| 17 | + - [Set log level](#set-log-level) |
| 18 | + - [Set datafile](#set-datafile) |
| 19 | + - [Sticky features](#sticky-features) |
| 20 | + - [Child instances](#child-instances) |
| 21 | + - [Hooks](#hooks) |
| 22 | + - [Close](#close) |
| 23 | +- [CLI usage](#cli-usage) |
| 24 | + - [Test](#test) |
| 25 | + - [Benchmark](#benchmark) |
| 26 | + - [Assess distribution](#assess-distribution) |
| 27 | +- [License](#license) |
| 28 | + |
| 29 | +## Installation |
| 30 | + |
| 31 | +In your PHP application: |
| 32 | + |
| 33 | +```bash |
| 34 | +$ composer require featurevisor/featurevisor-php |
| 35 | +``` |
| 36 | + |
| 37 | +## SDK usage |
| 38 | + |
| 39 | +### Create an instance |
| 40 | + |
| 41 | +```php |
| 42 | +<?php |
| 43 | + |
| 44 | +use function Featurevisor\createInstance; |
| 45 | + |
| 46 | +$DATAFILE_CONTENT = "..."; |
| 47 | + |
| 48 | +$f = createInstance([ |
| 49 | + "datafile" => $DATAFILE_CONTENT, |
| 50 | +]); |
| 51 | +``` |
| 52 | + |
| 53 | +Learn more about Featurevisor datafiles [here](https://featurevisor.com/docs/building-datafiles/). |
| 54 | + |
| 55 | +### Set context |
| 56 | + |
| 57 | +```php |
| 58 | +$f->setContext([ |
| 59 | + "appVersion" => "1.0.0", |
| 60 | + "userId" => "123", |
| 61 | + "deviceId" => "456", |
| 62 | + "country" => "nl", |
| 63 | +]); |
| 64 | +``` |
| 65 | + |
| 66 | +Context keeps getting accumulated in the SDK instance as you set new ones. |
| 67 | + |
| 68 | +To replace existing context, you can pass the second argument as `true`: |
| 69 | + |
| 70 | +```php |
| 71 | +$f->setContext([], true) |
| 72 | +``` |
| 73 | + |
| 74 | +### Evaluate values |
| 75 | + |
| 76 | +#### Evaluate a flag |
| 77 | + |
| 78 | +```php |
| 79 | +$isEnabled = $f->isEnabled("myFeatureKey"); |
| 80 | +``` |
| 81 | + |
| 82 | +#### Evaluate a variation |
| 83 | + |
| 84 | +```php |
| 85 | +$variation = $f->getVariation("myFeatureKey"); |
| 86 | +``` |
| 87 | + |
| 88 | +#### Evaluate a variable |
| 89 | + |
| 90 | +```php |
| 91 | +$variable = $f->getVariable("myFeatureKey", "variableKey"); |
| 92 | + |
| 93 | +// type specific methods |
| 94 | +$variable = $f->getVariableBoolean("myFeatureKey", "variableKey"); |
| 95 | +$variable = $f->getVariableString("myFeatureKey", "variableKey"); |
| 96 | +$variable = $f->getVariableInteger("myFeatureKey", "variableKey"); |
| 97 | +$variable = $f->getVariableDouble("myFeatureKey", "variableKey"); |
| 98 | +$variable = $f->getVariableArray("myFeatureKey", "variableKey"); |
| 99 | +$variable = $f->getVariableObject("myFeatureKey", "variableKey"); |
| 100 | +$variable = $f->getVariableJSON("myFeatureKey", "variableKey"); |
| 101 | +``` |
| 102 | + |
| 103 | +#### Evaluate all values |
| 104 | + |
| 105 | +```php |
| 106 | +$allEvaluations = $f->getAllEvaluations(); |
| 107 | + |
| 108 | +// [ |
| 109 | +// "myFeatureKey" => [ |
| 110 | +// "enabled" => true, |
| 111 | +// "variation" => "variationA", |
| 112 | +// "variables" => [ |
| 113 | +// "variableKey" => "value", |
| 114 | +// // ... |
| 115 | +// ], |
| 116 | +// ], |
| 117 | +// |
| 118 | +// "anotherFeatureKey" => [ ... ], |
| 119 | +// |
| 120 | +// // ... |
| 121 | +// ] |
| 122 | +``` |
| 123 | + |
| 124 | +### Passing additional context |
| 125 | + |
| 126 | +Each evaluation method accepts an optional argument for passing additional context specific to that evaluation: |
| 127 | + |
| 128 | +```php |
| 129 | +$isEnabled = $f->isEnabled("myFeatureKey", [ |
| 130 | + "browser" => "chrome", |
| 131 | +]); |
| 132 | +``` |
| 133 | + |
| 134 | +### Set log level |
| 135 | + |
| 136 | +```php |
| 137 | +$f->setLogLevel("debug"); |
| 138 | +``` |
| 139 | + |
| 140 | +Accepted values: |
| 141 | + |
| 142 | +- `debug` |
| 143 | +- `info` |
| 144 | +- `warn` |
| 145 | +- `error` |
| 146 | + |
| 147 | +### Set datafile |
| 148 | + |
| 149 | +You may want to update the datafile at runtime after initializing the SDK instance: |
| 150 | + |
| 151 | +```php |
| 152 | +$NEW_DATAFILE_CONTENT = "..."; |
| 153 | + |
| 154 | +$f->setDatafile($NEW_DATAFILE_CONTENT); |
| 155 | +``` |
| 156 | + |
| 157 | +### Sticky features |
| 158 | + |
| 159 | +Setting sticky features allow you to override the configuration as present in the datafile: |
| 160 | + |
| 161 | +```php |
| 162 | +$f->setSticky([ |
| 163 | + "myFeatureKey" => [ |
| 164 | + "enabled" => true, |
| 165 | + "variation" => "variationA", |
| 166 | + "variables" => [ |
| 167 | + "variableKey" => "value", |
| 168 | + ], |
| 169 | + ], |
| 170 | +]); |
| 171 | +``` |
| 172 | + |
| 173 | +To clear sticky features, you can use: |
| 174 | + |
| 175 | +```php |
| 176 | +$f->setSticky([], true); |
| 177 | +``` |
| 178 | + |
| 179 | +The second argument `true` indicates to replace the previously set stick features with new empty array. |
| 180 | + |
| 181 | +### Child instances |
| 182 | + |
| 183 | +```php |
| 184 | +$childF = $f->spawn($optionalChildContext); |
| 185 | + |
| 186 | +$childF->setContext([ |
| 187 | + "userId" => "789", |
| 188 | +]); |
| 189 | + |
| 190 | +$isEnabled = $childF->isEnabled("myFeatureKey"); |
| 191 | +``` |
| 192 | + |
| 193 | +Child instances inherit the context and configuration from the primary instance, but you can set a different context for them. |
| 194 | + |
| 195 | +They share similar methods as the primary instance for evaluations: |
| 196 | + |
| 197 | +```php |
| 198 | +$childF->isEnabled("myFeatureKey"); |
| 199 | +$childF->getVariation("myFeatureKey"); |
| 200 | +$childF->getVariable("myFeatureKey", "variableKey"); |
| 201 | +$childF->getAllEvaluations(); |
| 202 | +``` |
| 203 | + |
| 204 | +### Hooks |
| 205 | + |
| 206 | +TODO |
| 207 | + |
| 208 | +### Close |
| 209 | + |
| 210 | +To remove any forgotten listeners, both the primary and child instances allow a `close` method: |
| 211 | + |
| 212 | +```php |
| 213 | +$f->close(); |
| 214 | +``` |
| 215 | + |
| 216 | +## CLI usage |
| 217 | + |
| 218 | +This package also provides a CLI tool for running your Featurevisor project's test specs and benchmarking against this PHP SDK: |
| 219 | + |
| 220 | +### Test |
| 221 | + |
| 222 | +See: https://featurevisor.com/docs/testing/ |
| 223 | + |
| 224 | +``` |
| 225 | +$ vendor/bin/featurevisor test --projectDirectoryPath="path/to/your/featurevisor/project" |
| 226 | +``` |
| 227 | + |
| 228 | +Additional options that are available: |
| 229 | + |
| 230 | +``` |
| 231 | +$ vendor/bin/featurevisor test \ |
| 232 | + --projectDirectoryPath="path/to/your/featurevisor/project" \ |
| 233 | + --quiet|verbose \ |
| 234 | + --onlyFailures \ |
| 235 | + --keyPattern="myFeatureKey" \ |
| 236 | + --assertionPattern="#1" |
| 237 | +``` |
| 238 | + |
| 239 | +### Benchmark |
| 240 | + |
| 241 | +TODO |
| 242 | + |
| 243 | +### Assess distribution |
| 244 | + |
| 245 | +TODO |
| 246 | + |
| 247 | +## License |
| 248 | + |
| 249 | +MIT © [Fahad Heylaal](https://fahad19.com) |
0 commit comments