1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fomvasss\Variable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Cache\Repository as CacheRepository; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class VariableManager |
9
|
|
|
* |
10
|
|
|
* @package \Fomvasss\Variable |
11
|
|
|
*/ |
12
|
|
|
class VariableManager implements VariableManagerContract |
13
|
|
|
{ |
14
|
|
|
protected $variableModel; |
15
|
|
|
|
16
|
|
|
protected $cacheRepo; |
17
|
|
|
|
18
|
|
|
protected $locale = null; |
19
|
|
|
|
20
|
|
|
private $cacheName; |
21
|
|
|
|
22
|
|
|
protected $cacheTime; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* VariableManager constructor. |
26
|
|
|
* |
27
|
|
|
* @param \Illuminate\Cache\Repository $cacheRepo |
28
|
|
|
*/ |
29
|
|
|
public function __construct(CacheRepository $cacheRepo) |
30
|
|
|
{ |
31
|
|
|
$this->cacheTime = config('variables.cache.time', 360); |
|
|
|
|
32
|
|
|
$this->cacheName = config('variables.cache.name', 'laravel.variables.cache'); |
33
|
|
|
$this->variableModel = $this->model(); |
34
|
|
|
$this->cacheRepo = $cacheRepo; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
protected function model() |
41
|
|
|
{ |
42
|
|
|
return app()->make(config('variables.model', \Fomvasss\Variable\Variable::class)); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function all(): array |
49
|
|
|
{ |
50
|
|
|
$settings = $this->getAll(); |
51
|
|
|
return $settings->pluck('value', 'name')->toArray(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $name |
56
|
|
|
* @param null $default |
|
|
|
|
57
|
|
|
* @return null |
58
|
|
|
*/ |
59
|
|
|
public function first($name, $default = null) |
60
|
|
|
{ |
61
|
|
|
if (!empty($variable = $this->firstByName($name))) { |
62
|
|
|
return $variable->value; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $default; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $name |
70
|
|
|
* @param bool $asoc |
71
|
|
|
* @return array|mixed |
72
|
|
|
*/ |
73
|
|
|
public function firstJsonDecode($name, $asoc = true) |
74
|
|
|
{ |
75
|
|
|
if (!empty($variable = $this->firstByName($name))) { |
76
|
|
|
return json_decode($variable->value, $asoc); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return []; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $name |
84
|
|
|
* @param null $value |
|
|
|
|
85
|
|
|
* @param null $description |
86
|
|
|
* @return int |
87
|
|
|
*/ |
88
|
|
|
public function set($name, $value = null, $description = null): int |
89
|
|
|
{ |
90
|
|
|
$this->cacheRepo->forget($this->cacheName); |
91
|
|
|
|
92
|
|
|
return $this->variableModel->updateOrCreate([ |
93
|
|
|
'name' => $name, 'locale' => $this->locale |
94
|
|
|
], [ |
95
|
|
|
'value' => $value, 'description' => $description, |
96
|
|
|
]) ? 1 : 0; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param $name |
101
|
|
|
* @return int |
102
|
|
|
*/ |
103
|
|
|
public function delete($name): int |
104
|
|
|
{ |
105
|
|
|
$res = $this->variableModel->where('name', $name)->where('locale', $this->locale)->delete(); |
106
|
|
|
$this->cacheRepo->forget($this->cacheName); |
107
|
|
|
|
108
|
|
|
return $res ? 0 : 1; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string|null $locale |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
public function locale(string $locale = null) |
116
|
|
|
{ |
117
|
|
|
$this->locale = $locale; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param array $attributes |
124
|
|
|
* @return int |
125
|
|
|
*/ |
126
|
|
|
public function setArray(array $attributes): int |
127
|
|
|
{ |
128
|
|
|
$r = 0; |
129
|
|
|
foreach ($attributes as $name => $value) { |
130
|
|
|
$this->variableModel->updateOrCreate([ |
131
|
|
|
'name' => $name, 'locale' => $this->locale |
132
|
|
|
], [ |
133
|
|
|
'value' => $value |
134
|
|
|
]); |
135
|
|
|
$r++; |
136
|
|
|
} |
137
|
|
|
$this->cacheRepo->forget($this->cacheName); |
138
|
|
|
return $r; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param $name |
143
|
|
|
* @return mixed |
144
|
|
|
*/ |
145
|
|
|
protected function firstByName($name) |
146
|
|
|
{ |
147
|
|
|
return $this->getAll()->where('name', $name)->where('locale', $this->locale)->first(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return mixed |
152
|
|
|
*/ |
153
|
|
|
protected function getAll() |
154
|
|
|
{ |
155
|
|
|
$settings = $this->cacheRepo->remember($this->cacheName, $this->cacheTime, function () { |
156
|
|
|
return $this->variableModel->all(); |
157
|
|
|
}); |
158
|
|
|
|
159
|
|
|
return $this->locale ? $settings->where('locale', $this->locale) : $settings; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|