|
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 = 'fomvasss.variables.cache'; |
|
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->variableModel = $this->model(); |
|
33
|
|
|
$this->cacheRepo = $cacheRepo; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return mixed |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function model() |
|
40
|
|
|
{ |
|
41
|
|
|
return app()->make(config('variables.model', \Fomvasss\Variable\Variable::class)); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function all(): array |
|
48
|
|
|
{ |
|
49
|
|
|
$settings = $this->getAll(); |
|
50
|
|
|
return $settings->pluck('value', 'name')->toArray(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param $name |
|
55
|
|
|
* @param null $default |
|
|
|
|
|
|
56
|
|
|
* @return null |
|
57
|
|
|
*/ |
|
58
|
|
|
public function first($name, $default = null) |
|
59
|
|
|
{ |
|
60
|
|
|
if (!empty($setting = $this->firstByName($name))) { |
|
61
|
|
|
return $setting->value; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $default; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param $name |
|
69
|
|
|
* @param null $value |
|
|
|
|
|
|
70
|
|
|
* @param null $description |
|
71
|
|
|
* @return int |
|
72
|
|
|
*/ |
|
73
|
|
|
public function set($name, $value = null, $description = null): int |
|
74
|
|
|
{ |
|
75
|
|
|
$this->cacheRepo->forget($this->cacheName); |
|
76
|
|
|
|
|
77
|
|
|
return $this->variableModel->updateOrCreate([ |
|
78
|
|
|
'name' => $name, 'locale' => $this->locale |
|
79
|
|
|
], [ |
|
80
|
|
|
'value' => $value, 'description' => $description, |
|
81
|
|
|
]) ? 1 : 0; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param $name |
|
86
|
|
|
* @return int |
|
87
|
|
|
*/ |
|
88
|
|
|
public function delete($name): int |
|
89
|
|
|
{ |
|
90
|
|
|
$this->cacheRepo->forget($this->cacheName); |
|
91
|
|
|
$res = $this->variableModel->where('name', $name)->where('locale', $this->locale)->delete(); |
|
92
|
|
|
|
|
93
|
|
|
return $res ? 0 : 1; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string|null $locale |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
|
|
public function locale(string $locale = null) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->locale = $locale; |
|
103
|
|
|
|
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param array $attributes |
|
109
|
|
|
* @return int |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setArray(array $attributes): int |
|
112
|
|
|
{ |
|
113
|
|
|
$r = 0; |
|
114
|
|
|
foreach ($attributes as $name => $value) { |
|
115
|
|
|
$this->variableModel->updateOrCreate([ |
|
116
|
|
|
'name' => $name, 'locale' => $this->locale |
|
117
|
|
|
], [ |
|
118
|
|
|
'value' => $value |
|
119
|
|
|
]); |
|
120
|
|
|
$r++; |
|
121
|
|
|
} |
|
122
|
|
|
$this->cacheRepo->forget($this->cacheName); |
|
123
|
|
|
return $r; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param $name |
|
128
|
|
|
* @return mixed |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function firstByName($name) |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->getAll()->where('name', $name)->where('locale', $this->locale)->first(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return mixed |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function getAll() |
|
139
|
|
|
{ |
|
140
|
|
|
$settings = $this->cacheRepo->remember($this->cacheName, $this->cacheTime, function () { |
|
141
|
|
|
return $this->variableModel->all(); |
|
142
|
|
|
}); |
|
143
|
|
|
|
|
144
|
|
|
return $this->locale ? $settings->where('locale', $this->locale) : $settings; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|