-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature-flags.php
More file actions
168 lines (135 loc) · 4.13 KB
/
feature-flags.php
File metadata and controls
168 lines (135 loc) · 4.13 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/* PHP Feature Flags
https://github.com/jayf/php-feature-flags
MIT License 2013
*/
/* USAGE
1. require this file in your own code:
require_once '/your/path/feature-flags.php';
2. in your code, create a feature flag variable
(name is your choice), and initialize:
either (one step):
$FF = new FeatureFlags(
array(
'cookie'=>'FF',
'uriParam'=>'ff',
'ip'=>'10.0.0.0'
)
);
OR (two steps):
$FF = new FeatureFlags();
$FF->setDetect(
array(
'cookie'=>'FF',
'uriParam'=>'ff',
'ip'=>'10.0.0.0')
);
(see the README for more info on the detect options)
3. in your code, test for any flag, like so:
$FF->isFlagged();
// returns true when any flag is set
4. in your code, test for specific flags, like so:
$FF->hasFlag('dog');
// returns true when the "dog" flag is set
5. you also can see if a flag method is active, like so:
$FF->usesMethod('cookie')
// returns true when flag cookie detected
*/
class FeatureFlags {
private $isFlagged = false;
private $methods = array();
private $flags = array();
function __construct($options) {
$this->setDetect($options);
}
function setDetect($options) {
$init = array(
'cookie' => null,
'ip' => null,
'uriParam' => null
);
$detect = array_merge($init, $options);
$this->detectCookie($detect['cookie']);
$this->detectIp($detect['ip']);
$this->detectUriParam($detect['uriParam']);
}
private function detectCookie($cookiename) {
if(isset($cookiename)) {
/*
detect cookie-based flag
detects when cookie exists
and comma-separated values in the cookie:
value1,value2,value3
*/
if (isset($_COOKIE[$cookiename])) {
$this->isFlagged = true;
$this->addMethod('cookie');
$vals = explode(',', $_COOKIE[$cookiename]);
foreach ($vals as $val) {
$this->addFlag($val);
}
}
}
}
private function detectIp($ip) {
if(isset($ip) && $ip === $_SERVER['REMOTE_ADDR']) {
/*
detect IP Address-based flag
detects when requestor's IP Address matches
*/
$this->isFlagged = true;
$this->addMethod('ip');
}
}
private function detectUriParam($param) {
if(isset($param)) {
/*
detect URI Query Parameter-based flag
detects when parameter exists
and comma-separated values in the parameter:
value1,value2,value3
*/
if (isset($_GET[$param])) {
$this->isFlagged = true;
$this->addMethod('uriParam');
$vals = explode(',', $_GET[$param]);
foreach ($vals as $val) {
$this->addFlag($val);
}
}
}
}
private function addFlag($value) {
/*
adds a specific flag value
based on cookie or URI query parameter
*/
$this->flags[] = $value;
}
function hasFlag($value) {
/*
returns true if a specific flag is set
*/
return in_array($value, $this->flags);
}
function isFlagged() {
/*
returns true if any flag is detected
*/
return $this->isFlagged;
}
private function addMethod($value) {
/*
sets specific flag values
based on cookie or URI query parameter
*/
$this->methods[] = $value;
}
function usesMethod($value) {
/*
returns true if any flag was set by this method
*/
return in_array($value, $this->methods);
}
}
?>