1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fomvasss\Variable; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class VariableManager |
7
|
|
|
* |
8
|
|
|
* @package \Fomvasss\Variable |
9
|
|
|
*/ |
10
|
|
|
class VariableManager implements VariableManagerContract |
11
|
|
|
{ |
12
|
|
|
protected $app; |
13
|
|
|
|
14
|
|
|
protected $variableModel; |
15
|
|
|
|
16
|
|
|
protected $cacheRepository; |
17
|
|
|
|
18
|
|
|
protected $locale = null; |
19
|
|
|
|
20
|
|
|
protected $variables = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* VariableManager constructor. |
24
|
|
|
* @param $cacheRepository |
25
|
|
|
* @param null $app |
|
|
|
|
26
|
|
|
*/ |
27
|
|
|
public function __construct($variableModel, $cacheRepository, $app = null) |
28
|
|
|
{ |
29
|
|
|
if (! $app) { |
|
|
|
|
30
|
|
|
$app = app(); //Fallback when $app is not given |
|
|
|
|
31
|
|
|
} |
32
|
|
|
$this->app = $app; |
33
|
|
|
|
34
|
|
|
$this->config = $this->app['config']->get('variables'); |
|
|
|
|
35
|
|
|
|
36
|
|
|
$this->variableModel = $variableModel; |
37
|
|
|
|
38
|
|
|
$this->cacheRepository = $cacheRepository; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $key |
43
|
|
|
* @param null $default |
|
|
|
|
44
|
|
|
* @return mixed|null |
45
|
|
|
*/ |
46
|
|
|
public function get(string $key, $default = null, bool $useCache = true) |
47
|
|
|
{ |
48
|
|
|
if ($useCache === false) { |
49
|
|
|
$var = $this->variableModel->where('key', $key) |
50
|
|
|
->when($this->locale, function ($q) { |
51
|
|
|
$q->where('locale', $this->locale); |
52
|
|
|
}) |
53
|
|
|
->first(); |
54
|
|
|
return $var ? $var->value : $default; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($collection = $this->getCollection()) { |
58
|
|
|
if ($var = $collection |
59
|
|
|
->where('key', $key) |
60
|
|
|
->where('locale', $this->locale) |
61
|
|
|
->first()) { |
62
|
|
|
|
63
|
|
|
return $var->value ?: $default; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($var = $collection |
67
|
|
|
->where('key', $key) |
68
|
|
|
->where('locale', null) |
69
|
|
|
->first()) { |
70
|
|
|
|
71
|
|
|
return $var->value ?: $default; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $default; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function set(string $key, $value = null, $locale = null) |
79
|
|
|
{ |
80
|
|
|
return $this->variableModel->updateOrCreate([ |
81
|
|
|
'key' => $key, |
82
|
|
|
'locale' => $locale ?: $this->locale, |
83
|
|
|
], [ |
84
|
|
|
'value' => $value, |
85
|
|
|
]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function all(): array |
92
|
|
|
{ |
93
|
|
|
if ($all = $this->getCollection()) { |
94
|
|
|
$allByLocale = $this->locale ? $all->where('locale', $this->locale) : $all; |
95
|
|
|
$this->variables = $allByLocale->toArray(); |
96
|
|
|
} else { |
97
|
|
|
$this->variables = []; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->variables; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $locale |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function locale(string $locale = null) |
108
|
|
|
{ |
109
|
|
|
$this->locale = $locale; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function cacheOff() |
115
|
|
|
{ |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function cacheClear() |
120
|
|
|
{ |
121
|
|
|
return $this->cacheRepository->forget($this->config['cache']['name']); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return |null |
|
|
|
|
126
|
|
|
*/ |
127
|
|
|
protected function getCollection() |
128
|
|
|
{ |
129
|
|
|
try { |
130
|
|
|
return $this->cacheRepository->remember($this->config['cache']['name'], $this->config['cache']['time'], function () { |
131
|
|
|
return $this->variableModel->select('key', 'value', 'locale')->get(); |
132
|
|
|
}); |
133
|
|
|
} catch (\Exception $exception) { |
134
|
|
|
$this->app['log']->info(__CLASS__ . ': ' . $exception->getMessage()); |
135
|
|
|
|
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|