1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LaravelSwivel; |
6
|
|
|
|
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @package LaravelSwivel |
11
|
|
|
* @author Pablo Molina <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class SwivelComponent |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var \LaravelSwivel\SwivelLoader |
17
|
|
|
*/ |
18
|
|
|
protected $loader; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* SwivelComponent constructor. |
22
|
|
|
* @param \Illuminate\Http\Request $request HTTP request to load the header from |
23
|
|
|
*/ |
24
|
2 |
|
public function __construct(Request $request) |
25
|
|
|
{ |
26
|
2 |
|
$config = (array)config('swivel'); |
27
|
|
|
|
28
|
|
|
$swivelOptions = [ |
29
|
2 |
|
'LoaderAlias' => $config['loader_class'], |
30
|
2 |
|
'ModelAlias' => $config['model_class'], |
31
|
2 |
|
'Logger' => !empty($config['logger_class']) ? app($config['logger_class']) : null, |
32
|
|
|
'Metrics' => null, |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
// always search for the header and fallback to default value configured |
36
|
2 |
|
$swivelOptions['BucketIndex'] = !empty($request->header('Bucket')) |
37
|
2 |
|
? (int)$request->header('Bucket') |
38
|
|
|
: (int)$config['bucket_index']; |
39
|
|
|
|
40
|
2 |
|
if (empty($swivelOptions['LoaderAlias'])) { |
41
|
|
|
throw new \InvalidArgumentException('Loader alias expected'); |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
if (!empty($config['cookie_enabled'])) { |
45
|
|
|
$cookie = [ |
46
|
2 |
|
'enabled' => true, |
47
|
2 |
|
'name' => $config['cookie_name'], |
48
|
2 |
|
'expire' => (int)$config['cookie_expire'], |
49
|
2 |
|
'path' => $config['cookie_path'], |
50
|
2 |
|
'domain' => $config['cookie_domain'], |
51
|
2 |
|
'secure' => (bool)$config['cookie_secure'], |
52
|
2 |
|
'httpOnly' => (bool)$config['cookie_http_only'], |
53
|
|
|
]; |
54
|
2 |
|
$swivelOptions['Cookie'] = $cookie; |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
$this->loader = new $swivelOptions['LoaderAlias']($swivelOptions); |
58
|
2 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the loader instance to access it easily |
62
|
|
|
* |
63
|
|
|
* @return \LaravelSwivel\SwivelLoader |
64
|
|
|
*/ |
65
|
|
|
public function getLoader() |
66
|
|
|
{ |
67
|
|
|
return $this->loader; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create a new Builder instance |
72
|
|
|
* |
73
|
|
|
* @param string $slug The feature domain slug |
74
|
|
|
* @return \Zumba\Swivel\Builder |
75
|
|
|
*/ |
76
|
|
|
public function forFeature($slug) |
77
|
|
|
{ |
78
|
|
|
return $this->loader->getManager()->forFeature($slug); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Syntactic sugar for creating simple feature toggles (ternary style) |
83
|
|
|
* |
84
|
|
|
* @param string $slug The feature slug to evaluate |
85
|
|
|
* @param mixed $a Clousure to execute if feature is enabled |
86
|
|
|
* @param mixed $b Clousure to execute if feature is disabled |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
|
|
public function invoke($slug, $a, $b = null) |
90
|
|
|
{ |
91
|
|
|
return $this->loader->getManager()->invoke($slug, $a, $b); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Shorthand syntactic sugar for invoking a simple feature behavior using Builder::addValue. |
96
|
|
|
* Useful for ternary style code. |
97
|
|
|
* |
98
|
|
|
* @param string $slug The feature slug to evaluate |
99
|
|
|
* @param mixed $a Value to return if feature is enabled |
100
|
|
|
* @param null|mixed $b Value to return if feature is disabled |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
2 |
|
public function returnValue($slug, $a, $b = null) |
104
|
|
|
{ |
105
|
2 |
|
return $this->loader->getManager()->returnValue($slug, $a, $b); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|