1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fomvasss\Variable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class VariableManager |
10
|
|
|
* |
11
|
|
|
* @package \Fomvasss\Variable |
12
|
|
|
*/ |
13
|
|
|
class VariableManager implements VariableManagerContract |
14
|
|
|
{ |
15
|
|
|
protected $app; |
16
|
|
|
|
17
|
|
|
/** @var \Fomvasss\Variable\Models\Variable */ |
18
|
|
|
protected $variableModel; |
19
|
|
|
|
20
|
|
|
/** @var \Illuminate\Cache\Repository */ |
21
|
|
|
protected $cacheRepository; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
protected $langcode = null; |
25
|
|
|
|
26
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection */ |
27
|
|
|
protected $variables = null; |
28
|
|
|
|
29
|
|
|
/** @var bool */ |
30
|
|
|
protected $isUseCache; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* VariableManager constructor. |
34
|
|
|
* @param $variableModel |
35
|
|
|
* @param $cacheRepository |
36
|
|
|
* @param null $app |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
public function __construct($variableModel, $cacheRepository, $app = null) |
39
|
|
|
{ |
40
|
|
|
if (! $app) { |
|
|
|
|
41
|
|
|
$app = app(); //Fallback when $app is not given |
|
|
|
|
42
|
|
|
} |
43
|
|
|
$this->app = $app; |
44
|
|
|
|
45
|
|
|
$this->config = $this->app['config']->get('variables'); |
|
|
|
|
46
|
|
|
|
47
|
|
|
$this->isUseCache = $this->config['cache']['is_use_cache'] ?? true; |
48
|
|
|
|
49
|
|
|
$this->variableModel = $variableModel; |
50
|
|
|
|
51
|
|
|
$this->cacheRepository = $cacheRepository; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string|null $langcode |
56
|
|
|
* @param bool|null $isUseCache |
57
|
|
|
* @return array|\Illuminate\Database\Eloquent\Collection |
58
|
|
|
*/ |
59
|
|
|
public function all(?string $langcode = null, ?bool $isUseCache = null) |
60
|
|
|
{ |
61
|
|
|
$langcode = $langcode ?: $this->langcode; |
62
|
|
|
$isUseCache = $isUseCache === null |
63
|
|
|
? $this->isUseCache |
64
|
|
|
: $isUseCache; |
65
|
|
|
|
66
|
|
|
if ($langcode) { |
67
|
|
|
return $this->getCollection($isUseCache)->where('langcode', $langcode); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->getCollection($isUseCache); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $key |
75
|
|
|
* @param null $default |
|
|
|
|
76
|
|
|
* @param string|null $langcode |
77
|
|
|
* @param bool $useCache |
78
|
|
|
* @return mixed|null |
79
|
|
|
*/ |
80
|
|
|
public function get(string $key, $default = null, ?string $langcode = null, ?bool $isUseCache = null) |
81
|
|
|
{ |
82
|
|
|
$langcode = $langcode ?: $this->langcode; |
83
|
|
|
$isUseCache = $isUseCache === null |
84
|
|
|
? $this->isUseCache |
85
|
|
|
: $isUseCache; |
86
|
|
|
|
87
|
|
|
if ($var = $this->getCollection($isUseCache) |
88
|
|
|
->where('key', $key) |
89
|
|
|
->where('langcode', $langcode) |
90
|
|
|
->first()) { |
91
|
|
|
|
92
|
|
|
return $var->value ?: $default; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($var = $var = $this->getCollection($isUseCache) |
|
|
|
|
96
|
|
|
->where('key', $key) |
97
|
|
|
->where('langcode', null) |
98
|
|
|
->first()) { |
99
|
|
|
|
100
|
|
|
return $var->value ?: $default; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $default; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $key |
108
|
|
|
* @param null $value |
|
|
|
|
109
|
|
|
* @param string|null $langcode |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
public function save(string $key, $value = null, ?string $langcode = null) |
113
|
|
|
{ |
114
|
|
|
return $this->getVariableModel()->updateOrCreate([ |
115
|
|
|
'key' => $key, |
116
|
|
|
'langcode' => $langcode ?: $this->langcode, |
117
|
|
|
], [ |
118
|
|
|
'value' => $value, |
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string|null $langcode |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function setLang(?string $langcode = null): VariableManagerContract |
127
|
|
|
{ |
128
|
|
|
$this->langcode = $langcode; |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param bool $val |
135
|
|
|
* @return $this|mixed |
136
|
|
|
*/ |
137
|
|
|
public function useCache(bool $val = true): VariableManagerContract |
138
|
|
|
{ |
139
|
|
|
$this->isUseCache = $val; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function cacheClear() |
148
|
|
|
{ |
149
|
|
|
return $this->cacheRepository->forget($this->config['cache']['name']); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
154
|
|
|
*/ |
155
|
|
|
protected function getCollection(bool $isUseCache): \Illuminate\Database\Eloquent\Collection |
156
|
|
|
{ |
157
|
|
|
try { |
158
|
|
|
if ($isUseCache) { |
159
|
|
|
return $this->cacheRepository->remember( |
|
|
|
|
160
|
|
|
$this->config['cache']['name'], |
161
|
|
|
$this->config['cache']['time'], |
162
|
|
|
function () { |
163
|
|
|
return $this->getVariableModel()->all(); |
164
|
|
|
}); |
165
|
|
|
} else { |
166
|
|
|
return $this->getVariableModel()->all(); |
167
|
|
|
} |
168
|
|
|
} catch (\Exception $exception) { |
169
|
|
|
$this->app['log']->warning($exception->getMessage()); |
170
|
|
|
|
171
|
|
|
return new \Illuminate\Database\Eloquent\Collection; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return Model |
177
|
|
|
*/ |
178
|
|
|
protected function getVariableModel(): Model |
179
|
|
|
{ |
180
|
|
|
return $this->variableModel; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|