1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rawilk\LaravelModules\Activators; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Container\Container; |
6
|
|
|
use Rawilk\LaravelModules\Contracts\Activator; |
7
|
|
|
use Rawilk\LaravelModules\Contracts\ModuleModel; |
8
|
|
|
use Rawilk\LaravelModules\Module; |
9
|
|
|
|
10
|
|
|
class DatabaseActivator implements Activator |
11
|
|
|
{ |
12
|
|
|
/** @var \Illuminate\Cache\CacheManager */ |
13
|
|
|
private $cache; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
private $cacheKey; |
17
|
|
|
|
18
|
|
|
/** @var int */ |
19
|
|
|
private $cacheLifetime; |
20
|
|
|
|
21
|
|
|
/** @var \Illuminate\Contracts\Config\Repository */ |
22
|
|
|
private $config; |
23
|
|
|
|
24
|
|
|
/** @var \Rawilk\LaravelModules\Contracts\ModuleModel */ |
25
|
|
|
private $model; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \Illuminate\Contracts\Container\Container $app |
29
|
|
|
*/ |
30
|
|
|
public function __construct(Container $app) |
31
|
|
|
{ |
32
|
|
|
$this->cache = $app['cache']; |
33
|
|
|
$this->config = $app['config']; |
34
|
|
|
$this->model = $app[ModuleModel::class]; |
35
|
|
|
$this->cacheKey = $this->config('cache-key'); |
36
|
|
|
$this->cacheLifetime = $this->config('cache-lifetime'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function delete(Module $module): void |
40
|
|
|
{ |
41
|
|
|
$this->flushCache($module->getName()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function disable(Module $module): void |
45
|
|
|
{ |
46
|
|
|
$this->setActiveByName($module->getName(), false); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function enable(Module $module): void |
50
|
|
|
{ |
51
|
|
|
$this->setActiveByName($module->getName(), true); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function hasStatus(Module $module, bool $status): bool |
55
|
|
|
{ |
56
|
|
|
return $this->cache->remember( |
57
|
|
|
$this->moduleCacheKey($module->getName()), |
58
|
|
|
$this->cacheLifetime, |
59
|
|
|
function () use ($module, $status) { |
60
|
|
|
$databaseModule = $this->model::findModule($module->getName()); |
61
|
|
|
|
62
|
|
|
if (! $databaseModule) { |
63
|
|
|
return $status === false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $databaseModule->hasStatus($status); |
67
|
|
|
} |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function reset(): void |
72
|
|
|
{ |
73
|
|
|
$this->model::allModules() |
74
|
|
|
->each(function (ModuleModel $module) { |
75
|
|
|
$this->flushCache($module->getName()); |
76
|
|
|
}); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function setActive(Module $module, bool $active): void |
80
|
|
|
{ |
81
|
|
|
$this->setActiveByName($module->getName(), $active); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function setActiveByName(string $name, bool $active): void |
85
|
|
|
{ |
86
|
|
|
$method = $active === true ? 'enable' : 'disable'; |
87
|
|
|
|
88
|
|
|
$this->model::$method($name); |
89
|
|
|
|
90
|
|
|
$this->flushCache($name); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function config(string $key, $default = null) |
94
|
|
|
{ |
95
|
|
|
return $this->config->get("modules.activators.database.{$key}", $default); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function flushCache(string $name): void |
99
|
|
|
{ |
100
|
|
|
$this->cache->forget($this->moduleCacheKey($name)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function moduleCacheKey(string $name): string |
104
|
|
|
{ |
105
|
|
|
return sprintf($this->cacheKey, $name); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|