Repository navigation
Expand file tree
/
Copy pathin-memory.php
More file actions
68 lines (56 loc) · 2.63 KB
/
Copy pathin-memory.php
File metadata and controls
68 lines (56 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* Flipster in thirty seconds — no infrastructure required.
*
* This is the snippet the README opens with, kept as a runnable file so the README cannot drift from working
* code. Run it with:
*
* ./dev example in-memory
*/
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use Epignosis\Flipster\Flipster;
use Epignosis\Flipster\Testing\ControllableCircuitBreaker;
use Epignosis\Flipster\Testing\InMemoryProvider;
// In production this is your real provider — flagd, LaunchDarkly, GrowthBook, anything conformant.
// InMemoryProvider ships with Flipster so examples and tests need no backend.
$provider = new InMemoryProvider([
'new-checkout' => true,
'checkout-variant' => 'blue',
'rate-limit-rpm' => 500,
]);
// Likewise, a real deployment uses GaneshaCircuitBreaker over Redis. This one lets the example
// demonstrate an outage on demand.
$breaker = new ControllableCircuitBreaker();
$flags = Flipster::for($provider)
// Declare what each flag means when there is no live answer. This is the single source of truth:
// call sites never repeat a default, so the two can never disagree.
->withDefaults([
'new-checkout' => false,
'checkout-variant' => 'control',
'rate-limit-rpm' => 100,
])
->withBreaker($breaker, 'flags')
->evaluator();
// Inject $flags as a FlagEvaluator. Domain code sees nothing else — no SDK, no globals, no defaults.
printf("provider healthy\n");
printf(" new-checkout : %s\n", var_export($flags->isEnabled('new-checkout'), true));
printf(" checkout-variant : %s\n", $flags->getString('checkout-variant'));
printf(" rate-limit-rpm : %d\n", $flags->getInteger('rate-limit-rpm'));
// Now the flag backend goes down. In production the breaker would trip itself after enough failures;
// here we simply open it.
$breaker->open('flags');
printf("\ncircuit open — the provider is not called at all\n");
printf(" new-checkout : %s\n", var_export($flags->isEnabled('new-checkout'), true));
printf(" checkout-variant : %s\n", $flags->getString('checkout-variant'));
printf(" rate-limit-rpm : %d\n", $flags->getInteger('rate-limit-rpm'));
printf(" (each value above is the declared default)\n");
$breaker->close('flags');
printf("\nrecovered\n");
printf(" new-checkout : %s\n", var_export($flags->isEnabled('new-checkout'), true));
// A flag nobody declared has no defined meaning, so reading it is an error rather than a silent `false`.
try {
$flags->isEnabled('never-declared');
} catch (\Epignosis\Flipster\Exception\UndeclaredFlagException $e) {
printf("\nundeclared flag fails loudly:\n %s\n", $e->getMessage());
}