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