@@ -11,144 +11,144 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | class FileActivator implements ActivatorInterface |
| 13 | 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 | - public function __construct(Container $app) |
|
| 60 | - { |
|
| 61 | - $this->cache = $app['cache']; |
|
| 62 | - $this->files = $app['files']; |
|
| 63 | - $this->config = $app['config']; |
|
| 64 | - $this->statusesFile = $this->config('statuses-file'); |
|
| 65 | - $this->cacheKey = $this->config('cache-key'); |
|
| 66 | - $this->cacheLifetime = $this->config('cache-lifetime'); |
|
| 67 | - $this->modulesStatuses = $this->getModulesStatuses(); |
|
| 68 | - } |
|
| 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 | - private function getModulesStatuses() |
|
| 77 | - { |
|
| 78 | - if(!$this->config->get('modules.cache.enabled')) return $this->readJson(); |
|
| 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 | + public function __construct(Container $app) |
|
| 60 | + { |
|
| 61 | + $this->cache = $app['cache']; |
|
| 62 | + $this->files = $app['files']; |
|
| 63 | + $this->config = $app['config']; |
|
| 64 | + $this->statusesFile = $this->config('statuses-file'); |
|
| 65 | + $this->cacheKey = $this->config('cache-key'); |
|
| 66 | + $this->cacheLifetime = $this->config('cache-lifetime'); |
|
| 67 | + $this->modulesStatuses = $this->getModulesStatuses(); |
|
| 68 | + } |
|
| 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 | + private function getModulesStatuses() |
|
| 77 | + { |
|
| 78 | + if(!$this->config->get('modules.cache.enabled')) return $this->readJson(); |
|
| 79 | 79 | |
| 80 | - return $this->cache->remember($this->cacheKey, $this->cacheLifetime, function () { |
|
| 80 | + return $this->cache->remember($this->cacheKey, $this->cacheLifetime, function () { |
|
| 81 | 81 | return $this->readJson(); |
| 82 | 82 | }); |
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Flushes the modules activation statuses cache |
|
| 87 | - */ |
|
| 88 | - private function flushCache() |
|
| 89 | - { |
|
| 90 | - $this->cache->forget($this->cacheKey); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Reads a config parameter |
|
| 95 | - * |
|
| 96 | - * @param string $key [description] |
|
| 97 | - * @param $default |
|
| 98 | - * @return mixed |
|
| 99 | - */ |
|
| 100 | - private function config(string $key, $default = null) |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Flushes the modules activation statuses cache |
|
| 87 | + */ |
|
| 88 | + private function flushCache() |
|
| 89 | + { |
|
| 90 | + $this->cache->forget($this->cacheKey); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Reads a config parameter |
|
| 95 | + * |
|
| 96 | + * @param string $key [description] |
|
| 97 | + * @param $default |
|
| 98 | + * @return mixed |
|
| 99 | + */ |
|
| 100 | + private function config(string $key, $default = null) |
|
| 101 | 101 | { |
| 102 | 102 | return $this->config->get('modules.activators.file.' . $key, $default); |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - /** |
|
| 106 | - * Reads the json file that contains the activation statuses. |
|
| 107 | - * |
|
| 108 | - * @return array |
|
| 109 | - */ |
|
| 110 | - private function readJson() |
|
| 111 | - { |
|
| 112 | - if(!$this->files->exists($this->statusesFile)) return []; |
|
| 113 | - return json_decode($this->files->get($this->statusesFile), true); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Writes the activation statuses in a file, as json |
|
| 118 | - */ |
|
| 119 | - private function writeJson() |
|
| 120 | - { |
|
| 121 | - $this->files->put($this->statusesFile, json_encode($this->modulesStatuses, JSON_PRETTY_PRINT)); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Get the path of the file where statuses are stored |
|
| 126 | - * |
|
| 127 | - * @return string |
|
| 128 | - */ |
|
| 129 | - public function getStatusesFilePath() |
|
| 130 | - { |
|
| 131 | - return $this->statusesFile; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * @inheritDoc |
|
| 136 | - */ |
|
| 137 | - public function reset() |
|
| 138 | - { |
|
| 139 | - if($this->files->exists($this->statusesFile)){ |
|
| 140 | - $this->files->delete($this->statusesFile); |
|
| 141 | - } |
|
| 142 | - $this->modulesStatuses = []; |
|
| 143 | - $this->flushCache(); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 105 | + /** |
|
| 106 | + * Reads the json file that contains the activation statuses. |
|
| 107 | + * |
|
| 108 | + * @return array |
|
| 109 | + */ |
|
| 110 | + private function readJson() |
|
| 111 | + { |
|
| 112 | + if(!$this->files->exists($this->statusesFile)) return []; |
|
| 113 | + return json_decode($this->files->get($this->statusesFile), true); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Writes the activation statuses in a file, as json |
|
| 118 | + */ |
|
| 119 | + private function writeJson() |
|
| 120 | + { |
|
| 121 | + $this->files->put($this->statusesFile, json_encode($this->modulesStatuses, JSON_PRETTY_PRINT)); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Get the path of the file where statuses are stored |
|
| 126 | + * |
|
| 127 | + * @return string |
|
| 128 | + */ |
|
| 129 | + public function getStatusesFilePath() |
|
| 130 | + { |
|
| 131 | + return $this->statusesFile; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * @inheritDoc |
|
| 136 | + */ |
|
| 137 | + public function reset() |
|
| 138 | + { |
|
| 139 | + if($this->files->exists($this->statusesFile)){ |
|
| 140 | + $this->files->delete($this->statusesFile); |
|
| 141 | + } |
|
| 142 | + $this->modulesStatuses = []; |
|
| 143 | + $this->flushCache(); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | 147 | * @inheritDoc |
| 148 | 148 | */ |
| 149 | 149 | public function enable(Module $module) |
| 150 | 150 | { |
| 151 | - $this->setActiveByName($module->getName(), true); |
|
| 151 | + $this->setActiveByName($module->getName(), true); |
|
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | /** |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | */ |
| 157 | 157 | public function disable(Module $module) |
| 158 | 158 | { |
| 159 | - $this->setActiveByName($module->getName(), false); |
|
| 159 | + $this->setActiveByName($module->getName(), false); |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | /** |
@@ -164,10 +164,10 @@ discard block |
||
| 164 | 164 | */ |
| 165 | 165 | public function isStatus(Module $module, bool $status): bool |
| 166 | 166 | { |
| 167 | - if(!isset($this->modulesStatuses[$module->getName()])){ |
|
| 168 | - return $status === false; |
|
| 169 | - } |
|
| 170 | - return $this->modulesStatuses[$module->getName()] === $status; |
|
| 167 | + if(!isset($this->modulesStatuses[$module->getName()])){ |
|
| 168 | + return $status === false; |
|
| 169 | + } |
|
| 170 | + return $this->modulesStatuses[$module->getName()] === $status; |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | /** |
@@ -175,7 +175,7 @@ discard block |
||
| 175 | 175 | */ |
| 176 | 176 | public function setActive(Module $module, bool $active) |
| 177 | 177 | { |
| 178 | - $this->setActiveByName($module->getName(), $active); |
|
| 178 | + $this->setActiveByName($module->getName(), $active); |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | /** |
@@ -183,9 +183,9 @@ discard block |
||
| 183 | 183 | */ |
| 184 | 184 | public function setActiveByName(string $name, bool $status) |
| 185 | 185 | { |
| 186 | - $this->modulesStatuses[$name] = $status; |
|
| 187 | - $this->writeJson(); |
|
| 188 | - $this->flushCache(); |
|
| 186 | + $this->modulesStatuses[$name] = $status; |
|
| 187 | + $this->writeJson(); |
|
| 188 | + $this->flushCache(); |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | /** |
@@ -193,9 +193,9 @@ discard block |
||
| 193 | 193 | */ |
| 194 | 194 | public function delete(Module $module) |
| 195 | 195 | { |
| 196 | - if(!isset($this->modulesStatuses[$module->getName()])) return; |
|
| 197 | - unset($this->modulesStatuses[$module->getName()]); |
|
| 198 | - $this->writeJson(); |
|
| 199 | - $this->flushCache(); |
|
| 196 | + if(!isset($this->modulesStatuses[$module->getName()])) return; |
|
| 197 | + unset($this->modulesStatuses[$module->getName()]); |
|
| 198 | + $this->writeJson(); |
|
| 199 | + $this->flushCache(); |
|
| 200 | 200 | } |
| 201 | 201 | } |