-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathLocalhostClient.php
More file actions
284 lines (244 loc) · 8.62 KB
/
LocalhostClient.php
File metadata and controls
284 lines (244 loc) · 8.62 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
namespace SplitIO\Sdk;
use Symfony\Component\Yaml\Parser;
use SplitIO\Grammar\Condition\Partition\TreatmentEnum;
use SplitIO\Sdk\Validator\InputValidator;
use SplitIO\Split as SplitApp;
class LocalhostClient implements ClientInterface
{
private $splits = null;
/**
* Try to get the user home directory
* @return string|null
* @codeCoverageIgnore
*/
private function getUserHome()
{
$userData = posix_getpwuid(posix_getuid());
return (isset($userData['dir'])) ? $userData['dir'] : null;
}
private function getExistingFile($path)
{
if (!is_null($path) && file_exists($path)) {
return $path;
}
if (file_exists($this->getUserHome().'/split.yaml')) {
return $this->getUserHome().'/split.yaml';
}
if (file_exists($this->getUserHome().'/split.yml')) {
return $this->getUserHome().'/split.yml';
}
if (file_exists($this->getUserHome().'/.split')) {
SplitApp::logger()->warning("Localhost mode: .split mocks will be deprecated soon in favor of YAML "
. "files, which provide more targeting power. Take a look in our documentation.");
return $this->getUserHome().'/.split';
}
return null;
}
/**
* Constructor of the FakeClient for development purpose
* @param null $splitFilePath
* @throws \Exception
*/
public function __construct($splitFilePath = null)
{
$filePath = $this->getExistingFile($splitFilePath);
// @codeCoverageIgnoreStart
if (!is_null($filePath)) {
if (preg_match('/(\.yml$|\.yaml$)/i', $filePath)) {
$this->loadSplitsFromYAML($filePath);
} else {
SplitApp::logger()->warning("Localhost mode: .split mocks will be deprecated soon in favor of YAML "
. "files, which provide more targeting power. Take a look in our documentation.");
$this->loadSplits($filePath);
}
}
if (is_null($filePath) || $this->splits === null) {
throw new \Exception("Splits file could not be found");
}
// @codeCoverageIgnoreEnd
}
/**
* @param $splitFilePath
*/
private function loadSplits($splitFilePath)
{
$fileContent = file_get_contents($splitFilePath);
$this->splits = \SplitIO\parseSplitsFile($fileContent);
}
/**
* @param $splitFilePath
*/
private function loadSplitsFromYAML($splitFilePath)
{
$yaml = new Parser();
$parsed = $yaml->parse(file_get_contents($splitFilePath));
$splits = array();
foreach ($parsed as $split) {
$featureName = key($split);
$treatment = $split[$featureName]["treatment"];
if (isset($split[$featureName]["keys"])) {
$keys = $split[$featureName]["keys"];
if (is_array($keys)) {
foreach ($keys as $key) {
$splits[$featureName . ":" . $key]["treatment"] = $treatment;
if (isset($split[$featureName]["config"])) {
$splits[$featureName . ":" . $key]["config"] = $split[$featureName]["config"];
}
}
} else {
$splits[$featureName . ":" . $keys]["treatment"] = $treatment;
if (isset($split[$featureName]["config"])) {
$splits[$featureName . ":" . $keys]["config"] = $split[$featureName]["config"];
}
}
} else {
$splits[$featureName]["treatment"] = $treatment;
if (isset($split[$featureName]["config"])) {
$splits[$featureName]["config"] = $split[$featureName]["config"];
}
}
}
$this->splits = $splits;
}
public function getSplits()
{
return $this->splits;
}
public function doValidation($key, $featureFlagName, $operation)
{
$key = InputValidator::validateKey($key, $operation);
if (is_null($key)) {
return null;
}
$featureFlagName = InputValidator::validateFeatureFlagName($featureFlagName, $operation);
if (is_null($featureFlagName)) {
return null;
}
return is_null($key) ? $featureFlagName : ($featureFlagName . ":" . $key["matchingKey"]);
}
/**
* @inheritdoc
*/
public function getTreatment($key, $featureFlagName, ?array $attributes = null)
{
$key = $this->doValidation($key, $featureFlagName, "getTreatment");
if (is_null($key)) {
return TreatmentEnum::CONTROL;
}
if (isset($this->splits[$key])) {
return $this->splits[$key]["treatment"];
} else {
if (isset($this->splits[$featureFlagName])) {
return $this->splits[$featureFlagName]["treatment"];
}
}
return TreatmentEnum::CONTROL;
}
/**
* @inheritdoc
*/
public function getTreatmentWithConfig($key, $featureFlagName, ?array $attributes = null)
{
$treatmentResult = array(
"treatment" => TreatmentEnum::CONTROL,
"config" => null,
);
$key = $this->doValidation($key, $featureFlagName, "getTreatmentWithConfig");
if (is_null($key)) {
return $treatmentResult;
}
if (isset($this->splits[$key])) {
$treatmentResult["treatment"] = $this->splits[$key]["treatment"];
if (isset($this->splits[$key]["config"])) {
$treatmentResult["config"] = $this->splits[$key]["config"];
}
} else {
if (isset($this->splits[$featureFlagName])) {
$treatmentResult["treatment"] = $this->splits[$featureFlagName]["treatment"];
if (isset($this->splits[$featureFlagName]["config"])) {
$treatmentResult["config"] = $this->splits[$featureFlagName]["config"];
}
}
}
return $treatmentResult;
}
/**
* @inheritdoc
*/
public function getTreatments($key, $featureFlagNames, ?array $attributes = null)
{
$result = array();
$featureFlags = InputValidator::validateFeatureFlagNames($featureFlagNames, "getTreatments");
if (is_null($featureFlags)) {
return $result;
}
$key = InputValidator::validateKey($key, "getTreatments");
if (is_null($key)) {
return array_fill_keys($featureFlags, TreatmentEnum::CONTROL);
}
foreach ($featureFlags as $split) {
$result[$split] = $this->getTreatment($key["matchingKey"], $split, $attributes);
};
return $result;
}
/**
* @inheritdoc
*/
public function getTreatmentsWithConfig($key, $featureFlagNames, ?array $attributes = null)
{
$result = array();
$featureFlags = InputValidator::validateFeatureFlagNames($featureFlagNames, "getTreatmentsWithConfig");
if (is_null($featureFlags)) {
return $result;
}
$key = InputValidator::validateKey($key, "getTreatmentsWithConfig");
if (is_null($key)) {
return array_fill_keys($featureFlags, array('treatment' => TreatmentEnum::CONTROL, 'config' => null));
}
foreach ($featureFlags as $split) {
$result[$split] = $this->getTreatmentWithConfig($key["matchingKey"], $split, $attributes);
};
return $result;
}
/**
* @inheritdoc
*/
public function isTreatment($key, $featureFlagName, $treatment)
{
$calculatedTreatment = $this->getTreatment($key, $featureFlagName);
if ($calculatedTreatment !== TreatmentEnum::CONTROL) {
if ($treatment == $calculatedTreatment) {
return true;
}
}
return false;
}
/**
* @inheritdoc
*/
public function track($key, $trafficType, $eventType, $value = null)
{
return true;
}
public function getTreatmentsWithConfigByFlagSets($key, $flagSets, ?array $attributes = null)
{
// no-op
return array();
}
public function getTreatmentsByFlagSets($key, $flagSets, ?array $attributes = null)
{
// no-op
return array();
}
public function getTreatmentsWithConfigByFlagSet($key, $flagSet, ?array $attributes = null)
{
// no-op
return array();
}
public function getTreatmentsByFlagSet($key, $flagSet, ?array $attributes = null)
{
// no-op
return array();
}
}