From 811cb36a0300116933856c32b432db288662c681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Fri, 3 Nov 2023 17:05:40 +0300 Subject: [PATCH] fixed module installation --- app/Abstracts/Commands/Module.php | 24 ++++++---- app/Console/Commands/UninstallModule.php | 2 +- app/Utilities/ModuleActivator.php | 47 ++++++++++--------- .../Commands/DisableCommand.php | 4 +- .../laravel-module/Commands/EnableCommand.php | 4 +- .../Commands/InstallCommand.php | 1 + 6 files changed, 49 insertions(+), 33 deletions(-) diff --git a/app/Abstracts/Commands/Module.php b/app/Abstracts/Commands/Module.php index 4b2539536..910312dc6 100644 --- a/app/Abstracts/Commands/Module.php +++ b/app/Abstracts/Commands/Module.php @@ -10,13 +10,21 @@ use Symfony\Component\Console\Input\InputArgument; abstract class Module extends Command { + public string $alias; + + public int $company_id; + + public string $locale; + + public object|null $model; + + public int|null $old_company_id; + protected function prepare() { $this->alias = Str::kebab($this->argument('alias')); - $this->company_id = $this->argument('company'); + $this->company_id = (int) $this->argument('company'); $this->locale = $this->argument('locale'); - - $this->module = module($this->alias); } protected function changeRuntime() @@ -33,11 +41,11 @@ abstract class Module extends Command protected function revertRuntime() { - session()->forget('company_id'); - - if (!empty($this->old_company_id)) { - company($this->old_company_id)->makeCurrent(); + if (empty($this->old_company_id)) { + return; } + + company($this->old_company_id)->makeCurrent(); } protected function getModel() @@ -56,7 +64,7 @@ abstract class Module extends Command ModelHistory::create([ 'company_id' => $this->company_id, 'module_id' => $this->model->id, - 'version' => $this->module->get('version'), + 'version' => module($this->alias)->get('version'), 'description' => trans('modules.' . $action, ['module' => $this->alias]), 'created_from' => source_name(), 'created_by' => user_id(), diff --git a/app/Console/Commands/UninstallModule.php b/app/Console/Commands/UninstallModule.php index 2d2993d27..dff9f361d 100644 --- a/app/Console/Commands/UninstallModule.php +++ b/app/Console/Commands/UninstallModule.php @@ -45,7 +45,7 @@ class UninstallModule extends Command event(new Uninstalled($this->alias, $this->company_id)); // Delete files - $this->module->delete(); + module($this->alias)->delete(); $this->revertRuntime(); diff --git a/app/Utilities/ModuleActivator.php b/app/Utilities/ModuleActivator.php index 166b05207..0622906c4 100644 --- a/app/Utilities/ModuleActivator.php +++ b/app/Utilities/ModuleActivator.php @@ -36,26 +36,11 @@ class ModuleActivator implements ActivatorInterface return true; } - $alias = $module->getAlias(); - - if (! isset($this->statuses[$alias])) { - if (empty($this->company_id)) { - $company_id = $this->getCompanyId(); - - if (empty($company_id)) { - return false; - } - - $this->company_id = $company_id; - } - - $model = Model::companyId($this->company_id)->alias($alias)->first(); - $status = $model ? $model->enabled : $active; - - $this->setActive($module, $status); + if (! isset($this->statuses[$module->getAlias()])) { + return $active; } - return $this->statuses[$alias] === $active; + return $this->statuses[$module->getAlias()] === $active; } public function enable(Module $module): void @@ -72,15 +57,33 @@ class ModuleActivator implements ActivatorInterface { $this->statuses[$module->getAlias()] = $active; - Model::updateOrCreate([ + $this->flushCache(); + + if (empty($this->company_id)) { + $company_id = $this->getCompanyId(); + + if (empty($company_id)) { + return; + } + + $this->company_id = $company_id; + } + + $model = Model::companyId($this->company_id)->alias($module->getAlias())->first(); + + if (! empty($model)) { + $model->enabled = $active; + $model->save(); + + return; + } + + Model::create([ 'company_id' => $this->company_id, 'alias' => $module->getAlias(), - ], [ 'enabled' => $active, 'created_from' => 'core::activator', ]); - - $this->flushCache(); } public function delete(Module $module): void diff --git a/overrides/akaunting/laravel-module/Commands/DisableCommand.php b/overrides/akaunting/laravel-module/Commands/DisableCommand.php index d62d7414b..a2b69c7c8 100644 --- a/overrides/akaunting/laravel-module/Commands/DisableCommand.php +++ b/overrides/akaunting/laravel-module/Commands/DisableCommand.php @@ -30,13 +30,15 @@ class DisableCommand extends Command { $this->prepare(); - if (!$this->getModel()) { + if (! $this->getModel()) { $this->info("Module [{$this->alias}] not found."); + return; } if (!$this->model->enabled) { $this->comment("Module [{$this->alias}] is already disabled."); + return; } diff --git a/overrides/akaunting/laravel-module/Commands/EnableCommand.php b/overrides/akaunting/laravel-module/Commands/EnableCommand.php index 75a7f1510..7f0982066 100644 --- a/overrides/akaunting/laravel-module/Commands/EnableCommand.php +++ b/overrides/akaunting/laravel-module/Commands/EnableCommand.php @@ -30,13 +30,15 @@ class EnableCommand extends Command { $this->prepare(); - if (!$this->getModel()) { + if (! $this->getModel()) { $this->info("Module [{$this->alias}] not found."); + return; } if ($this->model->enabled) { $this->comment("Module [{$this->alias}] is already enabled."); + return; } diff --git a/overrides/akaunting/laravel-module/Commands/InstallCommand.php b/overrides/akaunting/laravel-module/Commands/InstallCommand.php index 65794c52d..8ac63b16c 100644 --- a/overrides/akaunting/laravel-module/Commands/InstallCommand.php +++ b/overrides/akaunting/laravel-module/Commands/InstallCommand.php @@ -33,6 +33,7 @@ class InstallCommand extends Command if ($this->getModel()) { $this->comment("Module [{$this->alias}] is already installed."); + return; }