Skip to content

Commit 88afb6b

Browse files
committed
initial commit
0 parents  commit 88afb6b

41 files changed

Lines changed: 8002 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.editorconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# EditorConfig helps maintain consistent coding styles between editors
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
insert_final_newline = true
8+
indent_style = space
9+
indent_size = 4
10+
trim_trailing_whitespace = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false
14+
15+
[*.php]
16+
insert_final_newline = true

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Dependency directories
2+
vendor/
3+
4+
# Composer files
5+
# composer.lock
6+
7+
# PHPUnit
8+
.phpunit.result.cache
9+
10+
# IDEs and editors
11+
/.idea/
12+
/.vscode/
13+
/*.sublime-project
14+
/*.sublime-workspace
15+
16+
# OS generated files
17+
.DS_Store
18+
Thumbs.db
19+
20+
# Logs and caches
21+
*.log
22+
cache/
23+
.phpunit*

.prettierrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"printWidth": 100,
3+
"tabWidth": 4,
4+
"useTabs": false,
5+
"semi": true,
6+
"singleQuote": true,
7+
"trailingComma": "es5",
8+
"bracketSpacing": true,
9+
"arrowParens": "always"
10+
}

CODE_OF_CONDUCT.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Code of Conduct
2+
3+
Please see https://github.com/featurevisor/featurevisor/blob/main/CODE_OF_CONDUCT.md

CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Contributing
2+
3+
Please see https://github.com/featurevisor/featurevisor/blob/main/CONTRIBUTING.md

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2025 Fahad Heylaal (https://fahad19.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
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)

composer.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "featurevisor/featurevisor-php",
3+
"description": "Featurevisor SDK for PHP",
4+
"type": "library",
5+
"homepage": "https://featurevisor.com",
6+
"keywords": [
7+
"featurevisor",
8+
"feature-flag",
9+
"feature-flags",
10+
"feature-toggles",
11+
"feature-management"
12+
],
13+
"authors": [
14+
{
15+
"name": "Fahad Heylaal",
16+
"homepage": "https://fahad19.com",
17+
"role": "lead"
18+
}
19+
],
20+
"support": {
21+
"issues": "https://github.com/featurevisor/featurevisor-php/issues",
22+
"source": "https://github.com/featurevisor/featurevisor-php",
23+
"docs": "https://featurevisor.com/docs/sdks/php/"
24+
},
25+
"version": "0.0.1",
26+
"minimum-stability": "dev",
27+
"prefer-stable": true,
28+
"autoload": {
29+
"psr-4": {
30+
"Featurevisor\\": "src/"
31+
}
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"Featurevisor\\Tests\\": "tests/"
36+
}
37+
},
38+
"require": {},
39+
"require-dev": {
40+
"php": "^8.0",
41+
"phpunit/phpunit": "^10"
42+
},
43+
"bin": [
44+
"featurevisor"
45+
],
46+
"scripts": {
47+
"test": "phpunit --bootstrap tests/bootstrap.php tests"
48+
},
49+
"license": [
50+
"MIT"
51+
]
52+
}

0 commit comments

Comments
 (0)