1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Humweb\Features; |
4
|
|
|
|
5
|
|
|
use Humweb\Features\Strategy\StrategyInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Feature registry. |
9
|
|
|
*/ |
10
|
|
|
class Features |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Features collections. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $features = []; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Create a new feature |
22
|
|
|
* |
23
|
|
|
* @param string $name Feature identifier. |
24
|
|
|
* @param string $description Feature description |
25
|
|
|
* @param array $strategies array of strategies |
26
|
|
|
* |
27
|
|
|
* @return StrategyCollection |
28
|
|
|
*/ |
29
|
|
|
public function create($name, $description = '', $strategies = []) |
30
|
|
|
{ |
31
|
|
|
if ($this->exists($name)) { |
32
|
|
|
throw new \InvalidArgumentException('Duplicate feature identifier.'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->features[$name] = [ |
36
|
|
|
'description' => $description, |
37
|
|
|
'collection' => new StrategyCollection($strategies), |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
return $this->features[$name]['collection']; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Adds strategy to feature |
46
|
|
|
* |
47
|
|
|
* @param string $feature Feature identifier |
48
|
|
|
* @param string $strategyName Strategy name |
49
|
|
|
* @param string|callable|StrategyInterface $strategy Strategy class or closure |
50
|
|
|
* @param array $args |
51
|
|
|
*/ |
52
|
|
|
public function pushStrategy($feature, $strategyName, $strategy = null, $args = []) |
53
|
|
|
{ |
54
|
|
|
if ( ! $this->exists($feature)) { |
55
|
|
|
throw new \InvalidArgumentException('Duplicate feature identifier.'); |
56
|
|
|
} |
57
|
|
|
$this->getCollection($feature)->add($strategyName, $strategy, $args); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get Collection by feature name |
63
|
|
|
* |
64
|
|
|
* @param string $name Feature identifier |
65
|
|
|
* |
66
|
|
|
* @return StrategyCollection |
67
|
|
|
*/ |
68
|
|
|
public function getCollection($name) |
69
|
|
|
{ |
70
|
|
|
return $this->get($name, 'collection'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Checks if a feature with given name exists. |
76
|
|
|
* |
77
|
|
|
* @param string $name Feature identifier. |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function exists($name) |
82
|
|
|
{ |
83
|
|
|
return isset($this->features[$name]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Flush all features. |
89
|
|
|
*/ |
90
|
|
|
public function flush() |
91
|
|
|
{ |
92
|
|
|
$this->features = []; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get Feature or item from feature array |
98
|
|
|
* |
99
|
|
|
* @param string $name |
100
|
|
|
* @param null|string $key |
101
|
|
|
* |
102
|
|
|
* @return |
103
|
|
|
*/ |
104
|
|
|
public function get($name, $key = null) |
105
|
|
|
{ |
106
|
|
|
if ( ! $this->exists($name)) { |
107
|
|
|
throw new \InvalidArgumentException('Unknown feature identifier.'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return ! is_null($key) ? $this->features[$name][$key] : $this->features[$name]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Check if feature is enabled |
116
|
|
|
* |
117
|
|
|
* @param string $feature Feature identifier |
118
|
|
|
* @param array $args arguments passed to strategies checker |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function isEnabled($feature, array $args = []) |
123
|
|
|
{ |
124
|
|
|
return $this->getCollection($feature)->check($args); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set the feature's threshold |
130
|
|
|
* |
131
|
|
|
* @param string $name Feature identifier |
132
|
|
|
* @param int $threshold |
133
|
|
|
* |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function setThreshold($name, $threshold = 1) |
137
|
|
|
{ |
138
|
|
|
$this->getCollection($name)->setThreshold($threshold); |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|