|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace M1guelpf\Feature; |
|
4
|
|
|
|
|
5
|
|
|
class FeatureManager |
|
6
|
|
|
{ |
|
7
|
|
|
/** @var bool|null */ |
|
8
|
|
|
protected static $masterSwitch = null; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Create a new FeatureManager Instance. |
|
12
|
|
|
* |
|
13
|
|
|
* @param array $features Features to load |
|
14
|
|
|
* |
|
15
|
|
|
* @return void |
|
|
|
|
|
|
16
|
|
|
*/ |
|
17
|
|
|
public function __construct(array $features = []) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->features = $features; |
|
|
|
|
|
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Check if a feature is enabled. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $name Name of the feature to check |
|
26
|
|
|
* @param string $function Check for a specific function |
|
27
|
|
|
* |
|
28
|
|
|
* @return bool |
|
29
|
|
|
*/ |
|
30
|
|
|
public function enabled(string $name, string $function = '') : bool |
|
31
|
|
|
{ |
|
32
|
|
|
if (! is_null(static::$masterSwitch)) { |
|
33
|
|
|
return static::$masterSwitch; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
if ($this->runningTests()) { |
|
37
|
|
|
return true; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$feature = $this->feature($name); |
|
41
|
|
|
|
|
42
|
|
|
if (! empty(trim($function))) { |
|
43
|
|
|
return is_array($feature) ? value(array_get($feature, $function, false)) : (bool) value($feature); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return (bool) value($feature); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get feature options. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $name Name of the feature to check |
|
53
|
|
|
* |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
|
|
public function feature(string $name) |
|
57
|
|
|
{ |
|
58
|
|
|
return array_get($this->features, $name, false); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Set the master switch. |
|
63
|
|
|
* |
|
64
|
|
|
* @param bool $value Value to set the master switch to |
|
65
|
|
|
* |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function setMasterSwitch(bool $value) |
|
69
|
|
|
{ |
|
70
|
|
|
static::$masterSwitch = $value; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Check if the current app instance is running unit tests. |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
public function runningTests() |
|
79
|
|
|
{ |
|
80
|
|
|
return function_exists('app') && method_exists($app = app(), 'runningUnitTests') && $app->runningUnitTests(); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.