1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Feature; |
4
|
|
|
|
5
|
|
|
use Feature\Contracts\User; |
6
|
|
|
use Feature\Contracts\World; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Feature |
10
|
|
|
* @package Feature |
11
|
|
|
*/ |
12
|
|
|
final class Feature |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var self |
16
|
|
|
*/ |
17
|
|
|
private static $instance; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var World |
21
|
|
|
*/ |
22
|
|
|
private $world; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $stanza; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private static $config = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param World $world |
36
|
|
|
* @param array $stanza |
37
|
|
|
*/ |
38
|
|
|
private function __construct(World $world, array $stanza) |
39
|
|
|
{ |
40
|
|
|
$this->world = $world; |
41
|
|
|
$this->stanza = $stanza; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param World $world |
46
|
|
|
* @param array $stanza |
47
|
|
|
* @return Feature |
48
|
|
|
*/ |
49
|
|
|
public static function create(World $world, array $stanza) |
50
|
|
|
{ |
51
|
|
|
if (static::$instance) { |
|
|
|
|
52
|
|
|
return static::$instance; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
static::$instance = new static($world, $stanza); |
|
|
|
|
56
|
|
|
|
57
|
|
|
return static::$instance; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $feature |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public static function isEnabled($feature) |
65
|
|
|
{ |
66
|
|
|
return static::createConfig($feature)->isEnabled(); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $feature |
71
|
|
|
* @param User $user |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public static function isEnabledFor($feature, User $user) |
75
|
|
|
{ |
76
|
|
|
return static::createConfig($feature)->isEnabledFor($user); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $feature |
81
|
|
|
* @param $bucketId |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public static function isEnabledBucketingBy($feature, $bucketId) |
85
|
|
|
{ |
86
|
|
|
return static::createConfig($feature)->isEnabledBucketingBy($bucketId); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $feature |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public static function variant($feature) |
94
|
|
|
{ |
95
|
|
|
return static::createConfig($feature)->variant(); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $feature |
100
|
|
|
* @param User $user |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public static function variantFor($feature, User $user) |
104
|
|
|
{ |
105
|
|
|
return static::createConfig($feature)->variantFor($user); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $feature |
110
|
|
|
* @param $bucketId |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public static function variantBucketingBy($feature, $bucketId) |
114
|
|
|
{ |
115
|
|
|
return static::createConfig($feature)->variantBucketingBy($bucketId); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $feature |
120
|
|
|
* @return mixed|null |
121
|
|
|
*/ |
122
|
|
|
public static function description($feature) |
123
|
|
|
{ |
124
|
|
|
return static::createConfig($feature)->description(); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* clear config |
129
|
|
|
*/ |
130
|
|
|
public static function clear() |
131
|
|
|
{ |
132
|
|
|
static::$config = []; |
|
|
|
|
133
|
|
|
static::$instance = null; |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param $feature |
138
|
|
|
* @return Config |
139
|
|
|
*/ |
140
|
|
|
private static function createConfig($feature) |
141
|
|
|
{ |
142
|
|
|
if (array_key_exists($feature, static::$config)) { |
|
|
|
|
143
|
|
|
return static::$config[$feature]; |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
static::$config[$feature] = new Config( |
|
|
|
|
147
|
|
|
$feature, |
148
|
|
|
static::$instance->stanza[$feature], |
|
|
|
|
149
|
|
|
static::$instance->world |
|
|
|
|
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
return static::$config[$feature]; |
|
|
|
|
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
Late static binding only has effect in subclasses. A
final
class cannot be extended anymore so late static binding cannot occurr. Consider replacingstatic::
withself::
.To learn more about late static binding, please refer to the PHP core documentation.