1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Activators; |
4
|
|
|
|
5
|
|
|
use Illuminate\Cache\CacheManager; |
6
|
|
|
use Illuminate\Config\Repository as Config; |
7
|
|
|
use Illuminate\Container\Container; |
8
|
|
|
use Illuminate\Filesystem\Filesystem; |
9
|
|
|
use Nwidart\Modules\Contracts\ActivatorInterface; |
10
|
|
|
use Nwidart\Modules\Module; |
11
|
|
|
|
12
|
|
|
class FileActivator implements ActivatorInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Laravel cache instance |
16
|
|
|
* |
17
|
|
|
* @var CacheManager |
18
|
|
|
*/ |
19
|
|
|
private $cache; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Laravel Filesystem instance |
23
|
|
|
* |
24
|
|
|
* @var Filesystem |
25
|
|
|
*/ |
26
|
|
|
private $files; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Laravel config instance |
30
|
|
|
* |
31
|
|
|
* @var Config |
32
|
|
|
*/ |
33
|
|
|
private $config; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $cacheKey; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $cacheLifetime; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Array of modules activation statuses |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private $modulesStatuses; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* File used to store activation statuses |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $statusesFile; |
58
|
|
|
|
59
|
191 |
|
public function __construct(Container $app) |
60
|
|
|
{ |
61
|
191 |
|
$this->cache = $app['cache']; |
62
|
191 |
|
$this->files = $app['files']; |
63
|
191 |
|
$this->config = $app['config']; |
64
|
191 |
|
$this->statusesFile = $this->config('statuses-file'); |
65
|
191 |
|
$this->cacheKey = $this->config('cache-key'); |
66
|
191 |
|
$this->cacheLifetime = $this->config('cache-lifetime'); |
67
|
191 |
|
$this->modulesStatuses = $this->getModulesStatuses(); |
68
|
191 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get modules statuses, either from the cache or |
72
|
|
|
* from the json statuses file if the cache is disabled. |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
191 |
|
private function getModulesStatuses() |
77
|
|
|
{ |
78
|
191 |
|
if (!$this->config->get('modules.cache.enabled')) { |
79
|
191 |
|
return $this->readJson(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->cache->remember($this->cacheKey, $this->cacheLifetime, function () { |
83
|
|
|
return $this->readJson(); |
84
|
|
|
}); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Flushes the modules activation statuses cache |
89
|
|
|
*/ |
90
|
188 |
|
private function flushCache() |
91
|
|
|
{ |
92
|
188 |
|
$this->cache->forget($this->cacheKey); |
93
|
188 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Reads a config parameter |
97
|
|
|
* |
98
|
|
|
* @param string $key [description] |
99
|
|
|
* @param $default |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
191 |
|
private function config(string $key, $default = null) |
103
|
|
|
{ |
104
|
191 |
|
return $this->config->get('modules.activators.file.' . $key, $default); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Reads the json file that contains the activation statuses. |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
191 |
|
private function readJson() |
113
|
|
|
{ |
114
|
191 |
|
if (!$this->files->exists($this->statusesFile)) { |
115
|
101 |
|
return []; |
116
|
|
|
} |
117
|
|
|
|
118
|
90 |
|
return json_decode($this->files->get($this->statusesFile), true); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Writes the activation statuses in a file, as json |
123
|
|
|
*/ |
124
|
130 |
|
private function writeJson() |
125
|
|
|
{ |
126
|
130 |
|
$this->files->put($this->statusesFile, json_encode($this->modulesStatuses, JSON_PRETTY_PRINT)); |
127
|
130 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the path of the file where statuses are stored |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
2 |
|
public function getStatusesFilePath() |
135
|
|
|
{ |
136
|
2 |
|
return $this->statusesFile; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @inheritDoc |
141
|
|
|
*/ |
142
|
98 |
|
public function reset() |
143
|
|
|
{ |
144
|
98 |
|
if ($this->files->exists($this->statusesFile)) { |
145
|
41 |
|
$this->files->delete($this->statusesFile); |
146
|
|
|
} |
147
|
98 |
|
$this->modulesStatuses = []; |
148
|
98 |
|
$this->flushCache(); |
149
|
98 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @inheritDoc |
153
|
|
|
*/ |
154
|
6 |
|
public function enable(Module $module) |
155
|
|
|
{ |
156
|
6 |
|
$this->setActiveByName($module->getName(), true); |
|
|
|
|
157
|
6 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @inheritDoc |
161
|
|
|
*/ |
162
|
6 |
|
public function disable(Module $module) |
163
|
|
|
{ |
164
|
6 |
|
$this->setActiveByName($module->getName(), false); |
|
|
|
|
165
|
6 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @inheritDoc |
169
|
|
|
*/ |
170
|
16 |
|
public function isStatus(Module $module, bool $status): bool |
171
|
|
|
{ |
172
|
16 |
|
if (!isset($this->modulesStatuses[$module->getName()])) { |
|
|
|
|
173
|
8 |
|
return $status === false; |
174
|
|
|
} |
175
|
|
|
|
176
|
8 |
|
return $this->modulesStatuses[$module->getName()] === $status; |
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @inheritDoc |
181
|
|
|
*/ |
182
|
4 |
|
public function setActive(Module $module, bool $active) |
183
|
|
|
{ |
184
|
4 |
|
$this->setActiveByName($module->getName(), $active); |
|
|
|
|
185
|
4 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @inheritDoc |
189
|
|
|
*/ |
190
|
130 |
|
public function setActiveByName(string $name, bool $status) |
191
|
|
|
{ |
192
|
130 |
|
$this->modulesStatuses[$name] = $status; |
193
|
130 |
|
$this->writeJson(); |
194
|
130 |
|
$this->flushCache(); |
195
|
130 |
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @inheritDoc |
199
|
|
|
*/ |
200
|
2 |
|
public function delete(Module $module) |
201
|
|
|
{ |
202
|
2 |
|
if (!isset($this->modulesStatuses[$module->getName()])) { |
|
|
|
|
203
|
|
|
return; |
204
|
|
|
} |
205
|
2 |
|
unset($this->modulesStatuses[$module->getName()]); |
206
|
2 |
|
$this->writeJson(); |
207
|
2 |
|
$this->flushCache(); |
208
|
2 |
|
} |
209
|
|
|
} |
210
|
|
|
|