From d670fd7e794b3f071a1687df0f5ef13c61748f77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Wed, 25 Oct 2023 00:14:05 +0300 Subject: [PATCH] added activator --- src/Activators/File.php | 127 +++++++++++++++++++++++++++ src/Config/module.php | 10 +++ src/Contracts/ActivatorInterface.php | 20 +++++ src/Module.php | 47 +++++----- src/Providers/Laravel.php | 11 ++- src/Providers/Lumen.php | 9 +- 6 files changed, 196 insertions(+), 28 deletions(-) create mode 100644 src/Activators/File.php create mode 100644 src/Contracts/ActivatorInterface.php diff --git a/src/Activators/File.php b/src/Activators/File.php new file mode 100644 index 0000000..e5275e2 --- /dev/null +++ b/src/Activators/File.php @@ -0,0 +1,127 @@ +cache = $app['cache']; + $this->files = $app['files']; + $this->config = $app['config']; + $this->statuses = $this->getStatuses(); + } + + public function is(Module $module, bool $active): bool + { + if (! isset($this->statuses[$module->getAlias()])) { + $this->setActive($module, $module->get('active', false)); + } + + return $this->statuses[$module->getAlias()] === $active; + } + + public function enable(Module $module): void + { + $this->setActive($module, true); + } + + public function disable(Module $module): void + { + $this->setActive($module, false); + } + + public function setActive(Module $module, bool $active): void + { + $this->statuses[$module->getAlias()] = $active; + + $module->json()->set('active', $active)->save(); + + $this->writeJson(); + + $this->flushCache(); + } + + public function delete(Module $module): void + { + if (! isset($this->statuses[$module->getAlias()])) { + return; + } + + unset($this->statuses[$module->getAlias()]); + + $this->writeJson(); + + $this->flushCache(); + } + + public function reset(): void + { + $path = $this->getFilePath(); + + if ($this->files->exists($path)) { + $this->files->delete($path); + } + + $this->statuses = []; + + $this->flushCache(); + } + + public function getStatuses(): array + { + if (! $this->config->get('module.cache.enabled')) { + return $this->readJson(); + } + + $key = $this->config->get('module.cache.key') . '.statuses'; + $lifetime = $this->config->get('module.cache.lifetime'); + + return $this->cache->remember($key, $lifetime, function () { + return $this->readJson(); + }); + } + + public function readJson(): array + { + $path = $this->getFilePath(); + + if (! $this->files->exists($path)) { + return []; + } + + return json_decode($this->files->get($path), true); + } + + public function writeJson(): void + { + $this->files->put($this->getFilePath(), json_encode($this->statuses, JSON_PRETTY_PRINT)); + } + + public function flushCache(): void + { + $key = $this->config->get('module.cache.key') . '.statuses'; + + $this->cache->forget($key); + } + + public function getFilePath() + { + return storage_path('module_statuses.json'); + } +} diff --git a/src/Config/module.php b/src/Config/module.php index a6cd876..59d01bf 100644 --- a/src/Config/module.php +++ b/src/Config/module.php @@ -189,4 +189,14 @@ 'composer' => 'register', ], + /* + |-------------------------------------------------------------------------- + | Activator + |-------------------------------------------------------------------------- + | + | Here is the activator class. + | + */ + 'activator' => env('MODULE_ACTIVATOR', \Akaunting\Module\Activators\File::class), + ]; diff --git a/src/Contracts/ActivatorInterface.php b/src/Contracts/ActivatorInterface.php new file mode 100644 index 0000000..4374129 --- /dev/null +++ b/src/Contracts/ActivatorInterface.php @@ -0,0 +1,20 @@ +cache = $app['cache']; $this->files = $app['files']; $this->translator = $app['translator']; + $this->activator = $app[ActivatorInterface::class]; $this->app = $app; } @@ -347,56 +354,45 @@ public function __toString() /** * Determine whether the given status same with the current module status. - * - * @param $status - * - * @return bool */ - public function isStatus($status) : bool + public function isStatus(bool $status) : bool { - return $this->get('active', 0) === $status; + return $this->activator->is($this, $status); } /** * Determine whether the current module activated. - * - * @return bool */ public function enabled() : bool { - return $this->isStatus(1); + return $this->activator->is($this, true); } /** * Determine whether the current module not disabled. - * - * @return bool */ public function disabled() : bool { - return !$this->enabled(); + return $this->activator->is($this, false); } /** * Set active state for current module. - * - * @param $active - * - * @return bool */ - public function setActive($active) + public function setActive(bool $active): void { - return $this->json()->set('active', $active)->save(); + $this->activator->setActive($this, $active); } /** * Disable the current module. */ - public function disable() + public function disable(): void { $this->fireEvent('disabling'); - $this->setActive(0); + $this->activator->disable($this); + $this->flushCache(); $this->fireEvent('disabled'); @@ -405,11 +401,12 @@ public function disable() /** * Enable the current module. */ - public function enable() + public function enable(): void { $this->fireEvent('enabling'); - $this->setActive(1); + $this->activator->enable($this); + $this->flushCache(); $this->fireEvent('enabled'); @@ -417,11 +414,11 @@ public function enable() /** * Delete the current module. - * - * @return bool */ - public function delete() + public function delete(): bool { + $this->activator->delete($this); + return $this->json()->getFilesystem()->deleteDirectory($this->getPath()); } diff --git a/src/Providers/Laravel.php b/src/Providers/Laravel.php index 8d4c001..90e57ef 100644 --- a/src/Providers/Laravel.php +++ b/src/Providers/Laravel.php @@ -2,6 +2,7 @@ namespace Akaunting\Module\Providers; +use Akaunting\Module\Contracts\ActivatorInterface; use Akaunting\Module\Contracts\RepositoryInterface; use Akaunting\Module\Laravel\LaravelFileRepository; use Akaunting\Module\Support\Stub; @@ -39,7 +40,7 @@ public function setupStubPath() $this->app->booted(function ($app) { $repository = $app[RepositoryInterface::class]; - + if ($repository->config('stubs.enabled') === true) { Stub::setBasePath($repository->config('stubs.path')); } @@ -56,7 +57,13 @@ protected function registerServices() return new LaravelFileRepository($app, $path); }); - + + $this->app->singleton(ActivatorInterface::class, function ($app) { + $class = $app['config']->get('module.activator'); + + return new $class($app); + }); + $this->app->alias(RepositoryInterface::class, 'module'); } diff --git a/src/Providers/Lumen.php b/src/Providers/Lumen.php index ce94e64..eea0465 100644 --- a/src/Providers/Lumen.php +++ b/src/Providers/Lumen.php @@ -2,6 +2,7 @@ namespace Akaunting\Module\Providers; +use Akaunting\Module\Contracts\ActivatorInterface; use Akaunting\Module\Contracts\RepositoryInterface; use Akaunting\Module\Lumen\LumenFileRepository; use Akaunting\Module\Support\Stub; @@ -49,7 +50,13 @@ protected function registerServices() return new LumenFileRepository($app, $path); }); - + + $this->app->singleton(ActivatorInterface::class, function ($app) { + $class = $app['config']->get('module.activator'); + + return new $class($app); + }); + $this->app->alias(RepositoryInterface::class, 'module'); } }