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
|
|
|
protected $cache; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Laravel Filesystem instance |
23
|
|
|
* |
24
|
|
|
* @var Filesystem |
25
|
|
|
*/ |
26
|
|
|
protected $files; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Laravel config instance |
30
|
|
|
* |
31
|
|
|
* @var Config |
32
|
|
|
*/ |
33
|
|
|
protected $config; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $cacheKey; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $cacheLifetime; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Array of modules activation statuses |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $modulesStatuses; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* File used to store activation statuses |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $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')) return $this->readJson(); |
79
|
|
|
|
80
|
|
|
return $this->cache->remember($this->cacheKey, $this->cacheLifetime, function () { |
81
|
|
|
return $this->readJson(); |
82
|
|
|
}); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Flushes the modules activation statuses cache |
87
|
|
|
*/ |
88
|
188 |
|
private function flushCache() |
89
|
|
|
{ |
90
|
188 |
|
$this->cache->forget($this->cacheKey); |
91
|
188 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Reads a config parameter |
95
|
|
|
* |
96
|
|
|
* @param string $key [description] |
97
|
|
|
* @param $default |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
191 |
|
private function config(string $key, $default = null) |
101
|
|
|
{ |
102
|
191 |
|
return $this->config->get('modules.activators.file.' . $key, $default); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Reads the json file that contains the activation statuses. |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
191 |
|
private function readJson() |
111
|
|
|
{ |
112
|
191 |
|
if(!$this->files->exists($this->statusesFile)) return []; |
113
|
90 |
|
return json_decode($this->files->get($this->statusesFile), true); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Writes the activation statuses in a file, as json |
118
|
|
|
*/ |
119
|
130 |
|
private function writeJson() |
120
|
|
|
{ |
121
|
130 |
|
$this->files->put($this->statusesFile, json_encode($this->modulesStatuses, JSON_PRETTY_PRINT)); |
122
|
130 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the path of the file where statuses are stored |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
2 |
|
public function getStatusesFilePath() |
130
|
|
|
{ |
131
|
2 |
|
return $this->statusesFile; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritDoc |
136
|
|
|
*/ |
137
|
98 |
|
public function reset() |
138
|
|
|
{ |
139
|
98 |
|
if($this->files->exists($this->statusesFile)){ |
140
|
41 |
|
$this->files->delete($this->statusesFile); |
141
|
|
|
} |
142
|
98 |
|
$this->modulesStatuses = []; |
143
|
98 |
|
$this->flushCache(); |
144
|
98 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @inheritDoc |
148
|
|
|
*/ |
149
|
6 |
|
public function enable(Module $module) |
150
|
|
|
{ |
151
|
6 |
|
$this->setActiveByName($module->getName(), true); |
|
|
|
|
152
|
6 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @inheritDoc |
156
|
|
|
*/ |
157
|
6 |
|
public function disable(Module $module) |
158
|
|
|
{ |
159
|
6 |
|
$this->setActiveByName($module->getName(), false); |
|
|
|
|
160
|
6 |
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritDoc |
164
|
|
|
*/ |
165
|
16 |
|
public function isStatus(Module $module, bool $status): bool |
166
|
|
|
{ |
167
|
16 |
|
if(!isset($this->modulesStatuses[$module->getName()])){ |
|
|
|
|
168
|
8 |
|
return $status === false; |
169
|
|
|
} |
170
|
8 |
|
return $this->modulesStatuses[$module->getName()] === $status; |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @inheritDoc |
175
|
|
|
*/ |
176
|
4 |
|
public function setActive(Module $module, bool $active) |
177
|
|
|
{ |
178
|
4 |
|
$this->setActiveByName($module->getName(), $active); |
|
|
|
|
179
|
4 |
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @inheritDoc |
183
|
|
|
*/ |
184
|
130 |
|
public function setActiveByName(string $name, bool $status) |
185
|
|
|
{ |
186
|
130 |
|
$this->modulesStatuses[$name] = $status; |
187
|
130 |
|
$this->writeJson(); |
188
|
130 |
|
$this->flushCache(); |
189
|
130 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @inheritDoc |
193
|
|
|
*/ |
194
|
2 |
|
public function delete(Module $module) |
195
|
|
|
{ |
196
|
2 |
|
if(!isset($this->modulesStatuses[$module->getName()])) return; |
|
|
|
|
197
|
2 |
|
unset($this->modulesStatuses[$module->getName()]); |
198
|
2 |
|
$this->writeJson(); |
199
|
2 |
|
$this->flushCache(); |
200
|
2 |
|
} |
201
|
|
|
} |
202
|
|
|
|