|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Opensoft\Rollout; |
|
7
|
|
|
|
|
8
|
|
|
use Opensoft\Rollout\Storage\StorageInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Richard Fullmer <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Rollout |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var StorageInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $storage; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private $groups; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param StorageInterface $storage |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(StorageInterface $storage) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->storage = $storage; |
|
31
|
|
|
$this->groups = array( |
|
32
|
|
|
'all' => function(RolloutUserInterface $user) { return $user !== null; } |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $feature |
|
38
|
|
|
*/ |
|
39
|
|
|
public function activate($feature) |
|
40
|
|
|
{ |
|
41
|
|
|
$feature = $this->get($feature); |
|
42
|
|
|
if ($feature) { |
|
43
|
|
|
$feature->setPercentage(100); |
|
44
|
|
|
$this->save($feature); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $feature |
|
50
|
|
|
*/ |
|
51
|
|
|
public function deactivate($feature) |
|
52
|
|
|
{ |
|
53
|
|
|
$feature = $this->get($feature); |
|
54
|
|
|
if ($feature) { |
|
55
|
|
|
$feature->clear(); |
|
56
|
|
|
$this->save($feature); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $feature |
|
62
|
|
|
* @param string $group |
|
63
|
|
|
*/ |
|
64
|
|
|
public function activateGroup($feature, $group) |
|
65
|
|
|
{ |
|
66
|
|
|
$feature = $this->get($feature); |
|
67
|
|
|
if ($feature) { |
|
68
|
|
|
$feature->addGroup($group); |
|
69
|
|
|
$this->save($feature); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $feature |
|
75
|
|
|
* @param string $group |
|
76
|
|
|
*/ |
|
77
|
|
|
public function deactivateGroup($feature, $group) |
|
78
|
|
|
{ |
|
79
|
|
|
$feature = $this->get($feature); |
|
80
|
|
|
if ($feature) { |
|
81
|
|
|
$feature->removeGroup($group); |
|
82
|
|
|
$this->save($feature); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $feature |
|
88
|
|
|
* @param RolloutUserInterface $user |
|
89
|
|
|
*/ |
|
90
|
|
|
public function activateUser($feature, RolloutUserInterface $user) |
|
91
|
|
|
{ |
|
92
|
|
|
$feature = $this->get($feature); |
|
93
|
|
|
if ($feature) { |
|
94
|
|
|
$feature->addUser($user); |
|
95
|
|
|
$this->save($feature); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $feature |
|
101
|
|
|
* @param RolloutUserInterface $user |
|
102
|
|
|
*/ |
|
103
|
|
|
public function deactivateUser($feature, RolloutUserInterface $user) |
|
104
|
|
|
{ |
|
105
|
|
|
$feature = $this->get($feature); |
|
106
|
|
|
if ($feature) { |
|
107
|
|
|
$feature->removeUser($user); |
|
108
|
|
|
$this->save($feature); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $group |
|
114
|
|
|
* @param \Closure $closure |
|
115
|
|
|
*/ |
|
116
|
|
|
public function defineGroup($group, \Closure $closure) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->groups[$group] = $closure; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $feature |
|
123
|
|
|
* @param RolloutUserInterface|null $user |
|
124
|
|
|
* @param array $requestParameters |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
|
|
public function isActive($feature, RolloutUserInterface $user = null, array $requestParameters = array()) |
|
128
|
|
|
{ |
|
129
|
|
|
$feature = $this->get($feature); |
|
130
|
|
|
|
|
131
|
|
|
return $feature ? $feature->isActive($this, $user, $requestParameters) : false; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param string $feature |
|
136
|
|
|
* @param integer $percentage |
|
137
|
|
|
*/ |
|
138
|
|
|
public function activatePercentage($feature, $percentage) |
|
139
|
|
|
{ |
|
140
|
|
|
$feature = $this->get($feature); |
|
141
|
|
|
if ($feature) { |
|
142
|
|
|
$feature->setPercentage($percentage); |
|
143
|
|
|
$this->save($feature); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param string $feature |
|
149
|
|
|
*/ |
|
150
|
|
|
public function deactivatePercentage($feature) |
|
151
|
|
|
{ |
|
152
|
|
|
$feature = $this->get($feature); |
|
153
|
|
|
if ($feature) { |
|
154
|
|
|
$feature->setPercentage(0); |
|
155
|
|
|
$this->save($feature); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param string $feature |
|
161
|
|
|
* @param string $requestParam |
|
162
|
|
|
*/ |
|
163
|
|
|
public function activateRequestParam($feature, $requestParam) |
|
164
|
|
|
{ |
|
165
|
|
|
$feature = $this->get($feature); |
|
166
|
|
|
if ($feature) { |
|
167
|
|
|
$feature->setRequestParam($requestParam); |
|
168
|
|
|
$this->save($feature); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param string $feature |
|
174
|
|
|
*/ |
|
175
|
|
|
public function deactivateRequestParam($feature) |
|
176
|
|
|
{ |
|
177
|
|
|
$feature = $this->get($feature); |
|
178
|
|
|
if ($feature) { |
|
179
|
|
|
$feature->setRequestParam(''); |
|
180
|
|
|
$this->save($feature); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param string $group |
|
186
|
|
|
* @param RolloutUserInterface|null $user |
|
187
|
|
|
* @return bool |
|
188
|
|
|
*/ |
|
189
|
|
|
public function isActiveInGroup($group, RolloutUserInterface $user = null) |
|
190
|
|
|
{ |
|
191
|
|
|
if (!isset($this->groups[$group])) { |
|
192
|
|
|
return false; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$g = $this->groups[$group]; |
|
196
|
|
|
|
|
197
|
|
|
return $g && $g($user); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param string $feature |
|
202
|
|
|
* @return Feature |
|
203
|
|
|
*/ |
|
204
|
|
|
public function get($feature) |
|
205
|
|
|
{ |
|
206
|
|
|
$settings = $this->storage->get($this->key($feature)); |
|
207
|
|
|
|
|
208
|
|
|
if (!empty($settings)) { |
|
209
|
|
|
$f = new Feature($feature, $settings); |
|
210
|
|
|
} else { |
|
211
|
|
|
$f = new Feature($feature); |
|
212
|
|
|
|
|
213
|
|
|
$this->save($f); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
return $f; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Remove a feature definition from rollout |
|
221
|
|
|
* |
|
222
|
|
|
* @param string $feature |
|
223
|
|
|
*/ |
|
224
|
|
|
public function remove($feature) |
|
225
|
|
|
{ |
|
226
|
|
|
$this->storage->remove($this->key($feature)); |
|
227
|
|
|
|
|
228
|
|
|
$features = $this->features(); |
|
229
|
|
|
if (in_array($feature, $features)) { |
|
230
|
|
|
$features = array_diff($features, array($feature)); |
|
231
|
|
|
} |
|
232
|
|
|
$this->storage->set($this->featuresKey(), implode(',', $features)); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Update feature specific data |
|
237
|
|
|
* |
|
238
|
|
|
* @example $rollout->setFeatureData('chat', array( |
|
239
|
|
|
* 'description' => 'foo', |
|
240
|
|
|
* 'release_date' => 'bar', |
|
241
|
|
|
* 'whatever' => 'baz' |
|
242
|
|
|
* )); |
|
243
|
|
|
* |
|
244
|
|
|
* @param string $feature |
|
245
|
|
|
* @param array $data |
|
246
|
|
|
*/ |
|
247
|
|
|
public function setFeatureData($feature, array $data) |
|
248
|
|
|
{ |
|
249
|
|
|
$feature = $this->get($feature); |
|
250
|
|
|
if ($feature) { |
|
251
|
|
|
$feature->setData(array_merge($feature->getData(), $data)); |
|
252
|
|
|
$this->save($feature); |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Clear all feature data |
|
258
|
|
|
* |
|
259
|
|
|
* @param string $feature |
|
260
|
|
|
*/ |
|
261
|
|
|
public function clearFeatureData($feature) |
|
262
|
|
|
{ |
|
263
|
|
|
$feature = $this->get($feature); |
|
264
|
|
|
if ($feature) { |
|
265
|
|
|
$feature->setData(array()); |
|
266
|
|
|
$this->save($feature); |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* @return array |
|
272
|
|
|
*/ |
|
273
|
|
|
public function features() |
|
274
|
|
|
{ |
|
275
|
|
|
$content = $this->storage->get($this->featuresKey()); |
|
276
|
|
|
|
|
277
|
|
|
if (!empty($content)) { |
|
278
|
|
|
return explode(',', $content); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
return array(); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* @param string $name |
|
286
|
|
|
* @return string |
|
287
|
|
|
*/ |
|
288
|
|
|
private function key($name) |
|
289
|
|
|
{ |
|
290
|
|
|
return 'feature:' . $name; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* @return string |
|
295
|
|
|
*/ |
|
296
|
|
|
private function featuresKey() |
|
297
|
|
|
{ |
|
298
|
|
|
return 'feature:__features__'; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* @param Feature $feature |
|
303
|
|
|
*/ |
|
304
|
|
|
protected function save(Feature $feature) |
|
305
|
|
|
{ |
|
306
|
|
|
$name = $feature->getName(); |
|
307
|
|
|
$this->storage->set($this->key($name), $feature->serialize()); |
|
308
|
|
|
|
|
309
|
|
|
$features = $this->features(); |
|
310
|
|
|
if (!in_array($name, $features)) { |
|
311
|
|
|
$features[] = $name; |
|
312
|
|
|
} |
|
313
|
|
|
$this->storage->set($this->featuresKey(), implode(',', $features)); |
|
314
|
|
|
} |
|
315
|
|
|
} |
|
316
|
|
|
|