|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Akaunting\Module\Activators; |
| 4 | + |
| 5 | +use Akaunting\Module\Contracts\ActivatorInterface; |
| 6 | +use Akaunting\Module\Module; |
| 7 | +use Illuminate\Cache\CacheManager as Cache; |
| 8 | +use Illuminate\Config\Repository as Config; |
| 9 | +use Illuminate\Container\Container; |
| 10 | +use Illuminate\Filesystem\Filesystem; |
| 11 | + |
| 12 | +class File implements ActivatorInterface |
| 13 | +{ |
| 14 | + public Cache $cache; |
| 15 | + |
| 16 | + public Filesystem $files; |
| 17 | + |
| 18 | + public Config $config; |
| 19 | + |
| 20 | + public array $statuses; |
| 21 | + |
| 22 | + public function __construct(Container $app) |
| 23 | + { |
| 24 | + $this->cache = $app['cache']; |
| 25 | + $this->files = $app['files']; |
| 26 | + $this->config = $app['config']; |
| 27 | + $this->statuses = $this->getStatuses(); |
| 28 | + } |
| 29 | + |
| 30 | + public function is(Module $module, bool $active): bool |
| 31 | + { |
| 32 | + if (! isset($this->statuses[$module->getAlias()])) { |
| 33 | + $this->setActive($module, $module->get('active', false)); |
| 34 | + } |
| 35 | + |
| 36 | + return $this->statuses[$module->getAlias()] === $active; |
| 37 | + } |
| 38 | + |
| 39 | + public function enable(Module $module): void |
| 40 | + { |
| 41 | + $this->setActive($module, true); |
| 42 | + } |
| 43 | + |
| 44 | + public function disable(Module $module): void |
| 45 | + { |
| 46 | + $this->setActive($module, false); |
| 47 | + } |
| 48 | + |
| 49 | + public function setActive(Module $module, bool $active): void |
| 50 | + { |
| 51 | + $this->statuses[$module->getAlias()] = $active; |
| 52 | + |
| 53 | + $module->json()->set('active', $active)->save(); |
| 54 | + |
| 55 | + $this->writeJson(); |
| 56 | + |
| 57 | + $this->flushCache(); |
| 58 | + } |
| 59 | + |
| 60 | + public function delete(Module $module): void |
| 61 | + { |
| 62 | + if (! isset($this->statuses[$module->getAlias()])) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + unset($this->statuses[$module->getAlias()]); |
| 67 | + |
| 68 | + $this->writeJson(); |
| 69 | + |
| 70 | + $this->flushCache(); |
| 71 | + } |
| 72 | + |
| 73 | + public function reset(): void |
| 74 | + { |
| 75 | + $path = $this->getFilePath(); |
| 76 | + |
| 77 | + if ($this->files->exists($path)) { |
| 78 | + $this->files->delete($path); |
| 79 | + } |
| 80 | + |
| 81 | + $this->statuses = []; |
| 82 | + |
| 83 | + $this->flushCache(); |
| 84 | + } |
| 85 | + |
| 86 | + public function getStatuses(): array |
| 87 | + { |
| 88 | + if (! $this->config->get('module.cache.enabled')) { |
| 89 | + return $this->readJson(); |
| 90 | + } |
| 91 | + |
| 92 | + $key = $this->config->get('module.cache.key') . '.statuses'; |
| 93 | + $lifetime = $this->config->get('module.cache.lifetime'); |
| 94 | + |
| 95 | + return $this->cache->remember($key, $lifetime, function () { |
| 96 | + return $this->readJson(); |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + public function readJson(): array |
| 101 | + { |
| 102 | + $path = $this->getFilePath(); |
| 103 | + |
| 104 | + if (! $this->files->exists($path)) { |
| 105 | + return []; |
| 106 | + } |
| 107 | + |
| 108 | + return json_decode($this->files->get($path), true); |
| 109 | + } |
| 110 | + |
| 111 | + public function writeJson(): void |
| 112 | + { |
| 113 | + $this->files->put($this->getFilePath(), json_encode($this->statuses, JSON_PRETTY_PRINT)); |
| 114 | + } |
| 115 | + |
| 116 | + public function flushCache(): void |
| 117 | + { |
| 118 | + $key = $this->config->get('module.cache.key') . '.statuses'; |
| 119 | + |
| 120 | + $this->cache->forget($key); |
| 121 | + } |
| 122 | + |
| 123 | + public function getFilePath() |
| 124 | + { |
| 125 | + return storage_path('module_statuses.json'); |
| 126 | + } |
| 127 | +} |
0 commit comments